Let's say we want to build the strongest possible fortress¹ in a medieval (1300 CE) setting while having access to any modern technology and material.
Said technology can only be used during the construction of the fortress and not afterwards, so e.g. a solar powered device is allowed to continue functioning after the construction is complete, but the denizens of the fortress will not be able to repair it in case of damage.
Soldiers will only have access to regular weapons and armor from their time, not modern weaponry (otherwise we would just build a wooden house equipped with M60s and be done with it).
¹This fortress will have to endure:
It is assumed that the inhabitants will learn perfectly how to operate every device/building/etc., but will not be able to understand how/why they work.
This means they won't be able to repair or replicate anything beyond their level of technology. For example, if a carbon fiber wall gets damaged, they will repair it with stones/bricks/concrete.
Location constraints: any location is allowed, provided it is suitable for a medieval population (e.g. an underwater city would be infeasible). It also needs to function as any "regular" castle (allow movement of troops and people in and out, allow counter attacks and so on)
Population: as many people as possible as long as it doesn't compromise security and provisions. At least 200; the more, the better. Let's assume that everyone is completely loyal and would die sooner than "betray the castle".
Resource and time limit: no time limit, and suppose the area isn't attacked or sabotaged in any way during the construction. The resource limit is what realistically could be done by an entity in our society, so obviously no "just build an actual mountain the size of China, made of concrete and steel and hate". Let's say something around $5B just to give you an idea. Of course, good and cheap is better than good and expensive; but good and expensive is much better than crappy but cheap.
What are the materials and techniques best suited to withstand the problems listed?
If "impregnable" is impossible, then "as strong/durable as possible".
Bonus points if the answer manages to give a great solution to the problem without needing overly complicated technology to keep working (e.g. by focusing on the materials and construction techniques rather than on particular devices)
This is the problem you risk if you make a fortress that's truly impregnable, it could become the prison you spend the rest of your life in. One of the key features of castles is the ability to counter attack. Whether to sally forth with troops, or simply to shoot from the relative safety of the battlements, a castle is more than just a reinforced concrete box.
Castles are a statement of control as well as a tool of war, you see it dominating a landscape and it tells the world that there's someone here willing to put up a fight to keep a region. It allows a smaller force to hold a territory against much larger numbers until reinforcements can be gathered. It also acts as garrison, armoury, residence, administrative centre, and food store. Castles are not the purely functional buildings that modern bunkers tend to be.
A castle didn't have to hold out forever when under siege, it just had to hold out for long enough. Directly assaulting a castle is fairly rare, it's expensive and risky. Far cheaper to sit out the siege, which could last years (the siege of Donnington castle lasted from July 1644 to April 1646). If your attackers are in a hurry and going for the attack you're probably in luck as defenders, castles were good, well designed and well built they gave significant advantage to the defending force. The ones that survive intact into the modern age were the very best, Dover castle lasted long enough to be hardened against cannon fire with the best technology of the age.
One of the greatest vulnerabilities of a castle was being undermined. This could bring down even the strongest tower. What we're going to do to compensate for this is dig down a couple of storeys, drill down and put in reinforced concrete foundations. While we're down here we'll also create some basements with water storage tanks and cool food storage areas all surrounded by double layered reinforced concrete walls, infilled with soil and rubble, similar to the main castle structure but underground and concrete. We may leave some secret tunnels down here just in case we need to make a discrete exit. Ideally there will also be a well or similar underground fresh water supply.
For the superstructure I'm not going to change much, the people of the day knew what they were doing and most castles would easily resist a direct assault on the walls. This leaves them in a good position to repair any damage that may be done during the average battle.
Inside the main outer door of the barbican we're going to build a bank vault type door, a simple conical section, smooth on the outside, it can only be controlled from the inside. The corridor between the outer door and this second door is a straight up kill zone, oil holes in the ceiling, spear holes in the walls all the usual "kill anyone in here" stuff, nothing special and fairly standard for the day.
Don't underestimate how good castle builders were at their job. Most castles never fell to assault, if they fell it was normally a surrender after a siege. The real change is better understanding of sanitation and food storage. Castles could survive sieges for years as they were.
The secret lies in Agriculture and Sanitation
Most medieval constructions before the advent of cannon/gunpowder were actually sufficient to prevent entry against a determined adversary. Most of these sieges ended through starvation, disease, an insider threat, or a counterattack by the defender's allies.
First of all, let's just make sure that your castle is truly impregnable. To do this, we are interested in two things: height (which means range) and the impregnability of the construction materials. We make our walls higher than any siege engine can launch an object. This will also prevent siege towers from being a factor. Now we can't have outside fire or disease (or deadly projectiles) launched in to our city. We also want to place footers under our walls deeper than can be possibly undermined. Current technology allows for both of these feats. The wall construction will be made of a composite lasting the centuries... likely reinforced concrete, layered with impact absorbers, covered with a durable, yet thin, metal. This will absorb anything thrown against it.
Now, the gate/entry way is the weakest physical assault point. This means that we will want multiple layers of security here and a precarious approach. A narrow, and easily protected, walkway covered by multiple towers on our skyscraper walls will do the trick, we'll just make several of these to be sure (and remove the idea that one person could open the single gate to our city. Also, a system could be developed where only one of the three inrow city gates can be opened at a time, along with much more secure (while still being simple) locking mechanisms.
So what's left... fire? All structures inside of our city are made of steel reinforced concrete. Disease? Sanitation is the name of the game here for the most part and a sewage system is put in place to deal with this. Romans did it with older technology, we can do it as well. Fresh water is provided by placing the castle over a large reservoir and drilling to this point. Modern technology isn't needed to lift water out of this in the event that pumps break. Impart as much medical knowledge as feasible (just the idea of infection and how to prevent it should safe thousands).
Now we're neatly sealed up inside of our city, with plenty of water and housing. We need food. And a way to always have food. You simply enlarge the area enclosed by your massive fortress walls to grow ample food to supply the city. It is likely best to make this area dispersed and divided to prevent disease/fire from spreading among the farms. The area required can be greatly reduced over typical medieval farming practices due to introducing modern agricultural techniques and grains. We also introduce modern grain storage techniques which allows for massive hoarding of food.
As for weapons to defend ourselves, sure, you could introduce all manner of fancy modern weaponry, but given our height and armored supremacy, traditional siege weapons mounted 100m off of the ground can outreach any adversary, if they even bother to assault you, which they never would.
Build one of these AA-Towers the Nazis build in WW2. The Allied Forces tried to destroy them after the war (with explosives from the inside) and ultimately gave up after having to use 40 tons of TNT on one of them in Berlin.
From KMJ, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=968914
It sure can withstand anything any medieval weapon has to offer. Don´t forget to bore a well in the middle!
The equivalent to fortresses in modern times are bunkers. Dig down deep, have a huge sturdy steel gate in a tunnel and that will be the only vulnerable spot - which should be indestructible for medieval level of technology. Consider creating a huge hole in front of the gate with water at the bottom. In order to have a battering ram roll to the gate, which wouldn't be effective anyway, they'd need to construct a lot in order to even get there.
Also they wouldn't be able to dig through mountains and ferroconcrete in any reasonable time and expense, so the gate is the only way in.
The best defence against a battering ram is of course to make it really difficult to get a ram up to the gate, but that does not depend on tech level. What you can do is to make a solid steel door. (Easy with modern tech, well understood but very very expensive for a person in the middle ages)
Also construct hinges and fittings carefully. Place some cushioning material (rubber will do nicely) behind the gate post to absorb impact from the ram.
You can certainly build strong walls with reinforced concrete. Also add steel plates and a crumble zone in front of the walls to greatly reduce impact from any boulders hurled your way.
Modern tech would not add much here. The key is to have a reliable well inside your fortress and stock up on food beforehand. However, you could definitely build some better facilities for hygiene and sanitation, reducing the risk of your soldiers succumbing to diseases.
Using reinforced concrete instead of stone makes it easier to build really high walls, which naturally improves your defence against ladders and towers. Also, you can build some nice defensive constructions, having protruding structures high up from which your defenders can easily cover the walls with arrows (and nastier stuff) making any attack over the walls a suicide mission.
The favourite means of breaching a castle wall was to tunnel under it and set fire to the (wooden) support beams. With modern equipment and machinery, you can extend your wall all the way down to bedrock, or at least well below groundwater level, making tunneling very hard.
I think the previous answers overlook one possibility: the question does not specify that modern weapons can't be included in the fortress design; only that no maintenance can be performed on those weapons.
I'm surrendering the bonus points here because I'm employing devices and not construction methods, but the best approach would probably be:
Build this fortress on flat land with clear and unobstructed lines of sight that extend 2x the maximum range of the ranged weapons of your specific era. If the useful range of the catapults of your era is 500 meters (for example), make sure you have sight lines extending a full kilometer.
Include pillboxes on your fortress that cover 360 degrees of your frontage. Have overlapping coverage wherever possible.
Put automatic weapons emplacements in your pillboxes - one main, and three backups.
Store as much ammunition as is feasible given the size of your fortress. Include as many basement levels as you think is feasible and fill them all with ammunition.
No medieval force would be able to even approach this fortress for the foreseeable future, let alone invest or take it. Sapping operations or trench operations wouldn't be feasible, either - the logistical effort it would take to cover a kilometer of ground using such methods would be beyond even the best-organized medieval military. I doubt even the Roman or Han armies could have done it - certainly not while under hostile fire.
Eventually the weapons would wear out, or expire due to lack of maintenance. Or you'd run out of ammo. But any of those events would either take a very, very long time, or would require your enemy to take losses that no medieval power could realistically bear.
So you want the medieval castle build with modern methods to withstand:
Battering rams on the gate
Balistas, catapults and trebuchets. No cannons.
Starvation by means of cutting out supply lines.
Possibly, ladders and siege towers. (I say "possibly" because it would be a nice addition, but as long as the other 3 problems are covered, this can be handled by the soldiers.)
No medieval castle was totally impregnable. No medieval castle was 100 percent guaranteed to totally withstand any possible medieval assault, or starvation, or treachery, or surrender of the garrison after they had held out for a long time but saw no possibility of being relieved.
The facts are that medieval sieges - both successful and unsuccessful, lasted for days, or weeks, or months, or years, and sometimes over a decade, before they either succeeded or were given up.
So it is a good idea to study the longest lasting medieval sieges and see what made them last so long before they either succeeded or failed. Part of the reason why some medieval sieges last so long was because the attackers had the will and the resources to keep on attacking or besieging for months or years instead of giving up after a few weeks. This means that some of the short sieges may be worth studying too, to learn why the besiegers gave up after a short time.
Here is a link to a long, long, long list of sieges:
Note that most of them, even in the medieval period, were sieges of towns and cities. But there are many sieges of castles listed during the medieval period.
So you can learn a lot from the designs of fortresses, castles, and cities that withstood long sieges.
And you can also try to find out which castles (like Chateau Gaillard, Krak des Chevaliers, Coucy, Caernarvon, etc., etc.) and cities (like Constantinople, etc.) were considered to be the strongest in their times.
So after your research identifies the features of the strongest castles and cities and other fortifications, you can hope to duplicate those features in your fictional castle, city, or fortification, only more so, of course, using the superior modern technology of 2018, or of 3018, or of 201,800, or of whenever your time travelers come from, or the superior technology of an alien civilization that might be millions of years more advanced than 13th century Earth.
A number of highly impressive structures were build by manpower alone in ancient times, and the main superiority of ancient societies over medieval societies was that ancient societies were were better organized and could better organize the efforts of hundreds and thousands of laborers on projects.
So a futuristic society with time travel, or an alien society advanced enough to reach Earth from another star system in our middle ages, would have two possible advantages when building even superior castles, cities, or fortifications than medieval society could.
1) They could use modern earth moving, quarrying, and construction equipment and power tools - only far more advanced than those of 2018 since a society capable of time travel or interstellar travel would be far more advanced than Earth in 2018.
2) They could use the same primitive tools and methods that ancient and medieval societies did, but replace the human muscle power used by ancient and medieval societies with the power of humanoid robots. Tens of humanoid worker robots, or hundreds, or thousands, or tens of thousands, or hundreds of thousands, or millions of worker robots.
Problem number 1:
Battering rams on the gate
Modern day police and military special forces still sometimes use battering rams to break into buildings. But the modern ones are pretty small and look like only two men can swing them at a time. Thus they can batter down ordinary modern house doors but would probably be useless against bank vault doors or medieval castle gates.
I believe that very long battering rams (and also picks to pick apart castle walls) swung by many men were used in medieval times.
And one defense against that was to avoid straight, perpendicular, level approaches to the gates. Instead the approach to the gate would be landscaped or built so that attackers with battering rams would have to approach and swing their rams diagonally, and/or uphill, and/or around a sharp right angle turn (or two sharp right angle turns), or else use very short battering rams which only a few men could swing, thus without enough force to batter open the gates.
And of course many gatehouses were highly defensible miniature castles. Many gatehouses had an inner and an outer gate separated by a passage, which of course could zigzag to prevent using long battering rams. And the passage was usually a killing zone with arrow slits and murder holes in the ceiling.
And one idea to make the inner passage of a gatehouse even more of a killing zone would be to put it over a deep pit with wooden stakes at the bottom. The floor would be a wooden trapdoor or drawbridge keep horizontal. When attackers were on the drawbridge floor it would be released dropping the attackers dozens of feet to be impaled on the spikes or stakes below. Then the drawbridge or trapdoor would be drawn back up to a horizontal position for the next bunch of attackers.
Another idea to make a gatehouse stronger would be to have emergency gates that could be lowered behind the inner wooden but metal reinforced gate. Each gate that would be slowly dowered down behind the inner gate would be a single giant slab of stone, or a masonry wall made of many stones, or a hollow metal gate filled with concrete. And these emergency gates would be slotted in so that each fitted against the back of the previous one. When they were all lowered into position they would form a solid wall behind the inner gate that could be as thick as the outer walls of the castle.
Once the siege or attack was over the castle defenders would no longer be able to use the inner gate to get in or out until they slowly winched up the many emergency gates behind the inner gate, which depending on how much modern power equipment they had, might take months.
So in the meantime they would have to use a weaker side gate in the inner passage of the gatehouse.
If attackers broke down that side gate they would find themselves in a space between inner and outer walls of the castle. The outer wall would have crenellations on the inner side as well as the outer side so that archers on it could turn around and shoot down at any attackers between the walls. The inner wall would be a lot higher than the outer wall so archers on it could shoot down at any attackers who got on top of the outer wall.
The space between inner and outer walls would run all the way around the castle but it would be divided into perhaps a dozen smaller spaces by cross walls with gates in them, so attackers would have to fight their way all around the castle and batter down maybe a dozen gates before coming to a space which had a gate leading to a gate in the inner wall.
And just to make things harder for the attackers there could could be deep pits with stakes outside each gate, or maybe filling the entire space between walls, and trapdoors to drop attackers into those pits.
So once the attackers battle their way through a dozen gates around the castle and batter down the gate in the inner wall, they can enter the ward of the castle - the outer ward of the concentric castle. And they see another gatehouse and another set of double walls ahead of them.
And know they have to repeat the previous process to get through the next two walls and reach the inner ward of the castle. Or possibly the middle ward of the castle if the concentric castle has three wards.
Some castle experts would say that such elaborate plans would be unnecessary gilding the lily since many medieval gatehouses were already the strongest and least vulnerable parts of the castles, so that it was common for it to be easier to breakdown and enter the walls of a castle instead of the gatehouse(s).
So the question should really be how to defend the walls against battering rams.
Many medieval castle walls were not built very well. They would have inner and outer walls made of large rough stones with a lot of mortar between the stones, and the space between the two two walls would be filled with smaller stones, pebbles, and dirt. Such walls were comparatively easy to battler down with rams or picks aimed at the mortar between the stones. That may be why many medieval castle walls were covered with stucco and whitewash, to hide the gaps and week spots between stones.
Defenses against attacks on the wall were:
a) Very sturdy and well built (and very expensive) walls. With the advanced technology of 2018, to say nothing of the far more advanced future or alien technology available to your castle builders, it should be comparatively easy to build walls out of vast quarried blocks of stone or poured concrete slabs, with the total widths and heights of the walls hundreds or thousands of feet or meters.
b) Passive prevention of enemies from reaching the walls of a castle to attack it. Thus there could be a steep slope outside a castle wall making it difficult to pick or batter at the wall, or a deep wide moat outside a castle wall making it difficult to pick or batter at the wall, or a moat with a slope inside it, or a moat with a slope outside it, or a moat with slopes both outside and inside it. Many castles were built on steep hills or mountains, and many had deep wide moats or lakes, sometimes concentric moats, as water defenses, like those of Kenilworth castle.
c) Active prevention of attackers from reaching the walls. The defenders would shoot at attackers to keep them from reaching the walls to try to break down the walls. The goal is to design the castle so that the number of defenders it can hold can defend it against tens, or preferably hundreds, or preferably thousands, of times as many attackers.
Defense against the second threat:
Balistas, catapults and trebuchets. No cannons.
Discussing defending the walls against battering ram attacks, possibility a) was: Very sturdy and well built (and very expensive) walls. With the advanced technology of 2018, to say nothing of the far more advanced future or alien technology available to your castle builders, it should be comparatively easy to build walls out of vast quarried blocks of stone or poured concrete slabs, with the total widths and heights of the walls hundreds or thousands of feet or meters.
Build the walls many tens of feet thick, or hundreds of feet thick, and balistas, catapults, and trebuchets will be unable to seriously damage such thick walls. This will also be a pretty good defense against any cannons likely to be invented in the next few centuries.
Possible defense b)
Also use advanced technology to build more advanced balistas, catapults, and trebuchets than medieval persons can build. Balistas, catapults, and trebuchets so advanced that ideally they can shoot farther than medieval built ones stationed a hundred feet higher than they are. Then build the walls of the the castle hundreds of feet higher than the highest position close enough to reach the castle with medieval balistas, catapults, and trebuchets.
Station your advanced balistas, catapults, and trebuchets on top of those walls to rain death upon anyone who sets up their balistas, catapults, and trebuchets in range of the castle and to slaughter the crews of the medieval balistas, catapults, and trebuchets and wreck those devices. If the attackers pull back their medieval balistas, catapults, and trebuchets to get them out of range of your advanced balistas, catapults, and trebuchets they will have to pull them so far back that they will no longer be able to reach the castle.
Note that women and children can fire balistas, catapults, and trebuchets from the walls of castles and cities and have done so in many sieges, so that refugees can become defenders during sieges.
Defense against the third threat:
Starvation by means of cutting out supply lines.
Many castles and cities were built in places where the irregular lay of the land dictated the irregular outline and layout of the defensive walls. But many other castles and cities were built where the builders were free to choose the exactly layout of the defense walls and outline of the castle of city.
And when castle and city builders had free choice of what plan to use, some chose less wisely and some chose more wisely.
If the methods of defending your castle require one defender for every X feet of outer curtain wall, you better make certain that your castle has no more feet of outer curtain wall than X times the expected number of defenders. The maximum total number of defenders your castle could possibly hold, or the usual number of defenders your castle usually holds, will depend on part on the accommodations for defenders within the castle, which will depend in part on the square footage within the castle.
Suppose that a walled enclosure has 10,000 square feet of ground and is 1 foot by 10,000 feet. it will have 20,002 feet of wall for 10,000 square feet, or 0.4999 square feet for every foot of wall.
Suppose that a walled enclosure has 10,000 square feet of ground and is 2 feet by 5,000 feet. it will have 10,004 feet of wall for 10,000 square feet, or 0.9996 square feet for every foot of wall.
Suppose that a walled enclosure has 10,000 square feet of ground and is 10 feet by 1,000 feet. it will have 2,020 feet of wall for 10,000 square feet, or 4.950 square feet for every foot of wall.
Suppose that a walled enclosure has 10,000 square feet of ground and is 20 feet by 500 feet. it will have 1,040 feet of wall for 10,000 square feet, or 9.615 square feet for every foot of wall.
Suppose that a walled enclosure has 10,000 square feet of ground and is 40 feet by 250 feet. it will have 580 feet of wall for 10,000 square feet, or 17.241 square feet for every foot of wall.
Suppose that a walled enclosure has 10,000 square feet of ground and is 80 feet by 125 feet. it will have 410 feet of wall for 10,000 square feet, or 24.390 square feet for every foot of wall.
Suppose that a walled enclosure has 10,000 square feet of ground and is 100 feet by 100 feet. it will have 400 feet of wall for 10,000 square feet, or 25 square feet for every foot of wall.
So, out of all possible rectangles, a square shape gives the least amount of wall to be defended for the same square footage inside. But a square is not only a type of rectangle, it is also a type of regular polygon. A regular polygon is a many sided geometric figure where all of the sides and all of the angles are identical.
Many other types of regular polygons have shorter perimeters than a square for the same area enclosed. The extreme form of a regular polygon is a circle. A circle with a radius of about 56.4189 feet and diameter of about 112.83796 feet would have an area of 10,000 square feet and a circumference of about 354.49 feet, and so would have a ratio of about 28.2095 square feet of area for every foot of perimeter wall to be manned.
And that is important because one way to avoid having your castle or city starved out is to grow sufficient food within the castle or city to feed the defenders.
Suppose that each person in the city or castle needs an average of one acre to grow food for himself and one tenth acre for other purposes. Suppose that a million people live in a city. They will need 1,100,000 acres of land to grow good and for other purposes.
Therefore, with 43,560 square feet in an acre, a total of 47,916,000,000 square feet would be needed, or a square 218,897.23 feet, or 41.4578 miles, on each side. It would have a total of 875,588.92 feet on all four sides. If there were 1,000,000 people in the city, and 0.25 of the were adult males, there would be a total of 250,000 defenders, so each side would have 62,500 defenders spread over 218,897.23 feet, or about one defender every 3.5023 feet, which seems adequate to defend a wall ten feet high, let alone one hundreds of feet high.
A circle with an area of 47,916,000,000 square feet would have a radius of 123,498.98 feet or 23.3899 miles, and a circumference of about 776,000 feet, with about 3.104 feet per defender. If the walls are hundreds of feet high and thick one defender should be enough to defend hundreds or thousands of feet of wall, so each fighting man could spend a few weeks a year training and stationed at the walls, and the rest of the year tending his farm which could be miles deep within the vast fortress.
Of course medieval methods of farming might require several acres to feed one person. But if the time travelers or aliens introduce more modern methods of agriculture, or hydroponics, or aeroponics, or food synthesizers, they may be able to feed many tens or hundreds of people per acre, and a fortress with a population of a million and 250,000 fighting men might be very tiny compared to one 46.7798 miles in diameter.
Or the super advanced aliens or time travelers might use their high technology to dig vast tunnels through the rocks for tens and hundreds of miles to various locations where their exits might be very strongly fortified. So supplies purchased in distant locations could be secretly brought in to the castle deep underground without besiegers knowing or being able to stop it.
Defense against the fourth threat:
Possibly, ladders and siege towers.
Making the walls hundreds of feet thick and tall would be a perfect defense against ladders and siege towers.
Putting concentric rings of steep slopes and moats around the castle, fortress, or city would also help defend against ladders and siege towers by keeping them away from the walls.
As other have noted, castle designs were actually already pretty impressive.
However, two significant things modern technology could enable that medieval technology could not replicate would be:
It would be really quite wonderful to build a castle high in some strategic mountain pass, but alas, medieval engineers lacked the means to do so. No longer! With modern advances in engineering, materials, architecture, and logistics, you can now seal that dirty mountain pass as tight as a drum. And to boot, rather than terribly drafty stone, wood, and cladding, you have solid concrete, proper insulation, and an HVAC system with climate control. Watch your enemies freeze to death outside your walls as your people sit happy, warm and safe inside the walls of your modern fortress.
In addition to being able to build a walled structure in some high mountain pass, you can also use modern engineering to build a wall over the ENTIRE mountain pass. The kinds of increases of scale enabled by modern technology could make a structure so incredibly large that it is it's own geographical feature. In addition to providing unassailable dominance over terrain, the sheer size and scope of your structure could serve as it's own kind of deterrent. Could you imagine being a medieval soldier and trying to lay siege to a castle wall the size of the hoover dam? Don't worry, your enemy can't either, which is why their army routed after a mere week into the siege of your mountain fortress.
Any reasonable medieval castle had stockpiles lasting for months and many had tunnels to enable restocking the castle during siege (or escape). Those castles are already nearly good enough for your purpose. Now take one at a suitable location and it is essentially impregnable as is, without any need for modern stuff - for example https://en.wikipedia.org/wiki/Predjama_Castle would be an existing castle that could be suitable for your purposes (the original one from 13th century would do as well). If you want some "modern magic": add lasers, long lasting batteries and burn wood/fat in a steam engine to charge batteries (or put it somewhere else with suitable natural resources and burn oil or gas). Everything is better with lasers. Also stuff the place with tons of cans to have food for decades. Maybe even reinforce walls with thick concrete.
But if you want the castle to be self sufficient, this won't do - the mentioned castle doesn't have means to generate its own food. Unless you place the same castle to guard the only passage to the other side of impassable mountains with a large enough fertile land. Easy enough, you as the boss could even chill in the palace in the middle of that land in complete safety, while the peons work the fields and defend the castle. It could last for centuries, until finally the explosives get good enough the thing gets slowly blasted apart.
Now, the most interesting thing would be to have a castle that needs to be 100% self-sufficient, yet defendable. But... this isn't possible. The big problem is you require about 2*10^4 m2/person to be self-sufficient (based on a quick search, numbers vary wildly). Now, assume a small garrison of 200, and you are looking at 4*10^6 m2 = square with side of 2km! (obviously you would make it a circle, but it doesn't change much, and it is simpler to calculate with squares). This is enormous already and a tad problematic to defend with mere 200 people, each will be responsible for 40m of the wall. Day and night. And work the fields as well. So, this is clearly impossible to defend, even if you give every one of them machine guns that never run out of ammo or break down. The nice thing for you is that with 20k people, you will have 20km sides of the square, but now 10 will be defending the same 40m section of the wall and do all the rest - so it is doable ... but at that point, you are not looking at a castle, but an island. Just build some guard posts around it put lasers there and just burn all the ships approaching. Think upgraded classical era mirrors :)
In summary, just how good do you want the castle to be? Castles were good enough already, add some modern magic and they are better. Put them in perfect location and you could have them last for centuries under siege. But to make them perfectly self-sufficient, nah, you can't feasibly do that with a castle; while islands don't even require much of a castle to achieve that.