In the Middle Ages, people built highly fortified castles for royalty inside cities that were already fortified by walls. What was the reasoning behind that? Were they made to protect royal families from internal rebellions or as an additional defense against an enemy who had already taken control of the city? From my understanding, if enemy attackers were able to seize control of the entire city, they should be able to besiege/capture one little royal castle relatively easily. So why would people spend so many resources to defend it? Wouldn't it be better to put these efforts into building more fortifications for the city itself instead?
No, that was part of the defense strategy. Making the walls a little bit stronger over all wouldn't help as much as building a very strong keep. That's the general idea behind it.
You are absolutely right to assume that those heavily fortified castles within cities were there for a reason: usually to keep the citizens at bay. In many fortified cities those coercion castles were build to discourage citizens from revolting.
Another important reason was prestige. Not every noble could afford to build a stone manor, let alone a stone castle or many stone castles. Only the richest and most powerful nobles could, and they did. Mostly because they needed them, but also to show of their power.
There are two additional reasons. First, in principle, any fortification can be overcome, with enough time and resources. But a very important question is how long will it take. By prolonging the siege you may hope a) for reinforcement or b) that the enemy will withdraw for whatever reason, for example, insufficient food. Several layers of defense prolong the siege.
Second, do not forget that fortifications must be manned to be useful. If your walls are too long, a lot of people is needed to defend them, and these people may not be available in certain circumstances.
These were different lines of defense. A city was a relatively large area whose purpose was to maintain the political power of say, the king. As such, it had a large number of civilians, and provided a place of refuge for "some" people from the surrounding countryside. As such, a city was a better defended place than most of the of countryside, but its relative size made it vulnerable to attack at certain points.
A citadel was a last line of defense. It was the place to which the king or leader retreated with his army if it couldn't hold the city. A castle had a much smaller circumference and was built in such a way that it had (supposedly) NO weak points. The whole idea was have a relatively small area defended by a concentrated military force; no civilians were allowed. It was a place for a "do or die" stand. If the citadel were somehow breached or overrun, the defenders stood to be killed.
During the Second Punic War, Hannibal was able to capture the city of Taras (Taranto), but the Romans held the citadel guarding the port.
Note: Most city-castles weren't inside the medieval city/town but at it's edge this can give the castle two uses: First it reinforces the part of the walls it is located at, making the city harder to attack. On the other hand they were meant to defend the lords against their own people. They often had their own gates in the city-walls and sometimes even stronger defenses against the city than against the outside. A good example for this is the Munich Residence. Originally the Duke of Bavaria resided in a castle inside the town now called Alter Hof(Old Court). After an uprising they noticed that they were basically trapped inside their castle and relocated to a corner of the town and built a new castle that eventually became the Residence.
I one read somewhere that eastern Roman or "Byzantine" Emperor Nikephoros II Phocas (r. 963-969) or John I Tzimiskes (r.969-976) built a defensive wall at the Great Palace in Constantinople.
Not a defensive wall around the Great Palace. There already was one. But a defensive wall inside the Great Palace, separating the outer, older, and little used parts from the inner, newer, and often used parts.
So if an enemy broke into the outer parts of the Great Palace the inner parts of the Great Palace could still be defended by the wall.
The Greeks thought Ecbatana to be the capital of the Medes empire and credited its foundation to Deioces (the Daiukku of the cuneiform inscriptions). It is alleged that he surrounded his palace in Ecbatana with seven concentric walls of different colours. In the 5th century BC, Herodotus wrote of Ecbatana:
"The Medes built the city now called Ecbatana, the walls of which are of great size and strength, rising in circles one within the other. The plan of the place is, that each of the walls should out-top the one beyond it by the battlements. The nature of the ground, which is a gentle hill, favors this arrangements in some degree but it is mainly effected by art. The number of the circles is seven, the royal palace and the treasuries standing within the last. The circuit of the outer wall is very nearly the same with that of Athens. On this wall the battlements are white, of the next black, of the third scarlet, of the fourth blue, the fifth orange; all these colors with paint. The last two have their battlements coated respectively with silver and gold. All these fortifications Deioces had caused to be raised for himself and his own palace."
Ekbatana (forse), phraakates e musa, dracma, 2 ac-4 dc ca Herodotus' description is corroborated in part by stone reliefs from the Neo-Assyrian Empire, depicting Median citadels ringed by concentric walls. Other sources attest to the historical importance of Ecbatana based on the terms used by ancient authors to describe it such as Caput Mediae (capital of Media), the Royal Seat, and great City. It is said that Alexander the Great deposited the treasures he took from Persepolis and Pasargadae and that one of the last acts of his life was to visit the city.
From the 6th to the 8th centuries, the land of the Avars in modern Hungary was defended by ring fortresses, which in some accounts surrounded the entire country. The account by the Monk of St. Gall is summarized:
Before telling the story of the expedition, it behooves us to give some account of the country which the king of the Franks was about to invade, and particularly to describe the extraordinary defences and interior conditions with which it is credited by the gossipy old Monk of St. Gall, the most entertaining, though hardly the most credible, writer of that period. All authors admit that the country of the Avars was defended by an ingenious and singular system of fortifications. The account we propose to give, the Monk of St. Gall declares that he wrote down from the words of an eye-witness, Adelbart by name, who took part in the expedition. But one cannot help thinking that either this eye-witness mingled a strong infusion of imagination with his vision, or that the monk added fiction to his facts, with the laudable purpose of making an attractive story. Such as it is, we give it, without further comment.
Nine concentric circles of palisaded walls, says the garrulous old monk, surrounded the country of the Avars, the outer one enclosing the entire realm of Hungary, the inner ones growing successively smaller, the innermost being the central fortification within which dwelt the Chagan, with his palace and his treasures. These walls were made of double rows of palisades of oak, beech, and pine logs, twenty feet high and twenty feet asunder, the interval between them being filled with stone and lime. Thus was formed a great wall, which at a distance must have presented a singular appearance, since the top was covered with soil and planted with bushes and trees.
The outermost wall surrounded the whole country. Within it, at a distance of twenty Teutonic, or forty Italian, miles, was a second, of smaller diameter, but constructed in the same manner. At an equal distance inward was a third, and thus they continued inward, fortress after fortress, to the number of nine, the outer one rivalling the Chinese wall in extent, the inner one—the ring, as it was called—being of small diameter, and enclosing a central space within which the Avars guarded the accumulated wealth of centuries of conquest and plunder.
The only places of exit from these great palisaded fortifications were very narrow gates, or sally-ports, opening at proper intervals, and well guarded by armed sentinels. The space between the successive ramparts was a well-wooded and thickly-settled country, filled with villages and homesteads, so close together that the sound of a trumpet could be heard from one to the other, and thus an alarm from the exterior be conveyed with remarkable rapidity throughout the whole land.
This and more the veracious Monk of St. Gall tells us. As to believing him, that is quite another matter. Sufficient is told by other writers to convince us that the country was guarded by strong and singular defences, but the nine concentric circles of breastworks, surpassing the Chinese wall in length and size, the reader is quite privileged to doubt.
These examples show that many ancient and medieval persons believed that it was better to be defended by two concentric walls than by one, and better to be defended by three concentric walls than by two, and so on, and that they also believed it would be natural for the leader of their society to have their stronghold within the innermost wall.
And the example of the wall built by Nikephoros I or John I inside the Great Palace at Constantinople shows that sometimes rulers also believed that and sometimes they spent a lot of money on such projects.
It may also be noted that many ancient and medieval cities expanded and contracted in population and area over time, so that sometimes new walls were built to defend the smaller or larger area currently inhabited. Thus a city could gain concentric walls, although using only one line of walls would be maintained at a time.
And from the high middle ages onward to the present, most European cities have constantly expanded their areas, and thus sometimes built outer defensive walls as their areas expanded, at least until the building of city walls ceased. So many castles built in the suburbs of tiny medieval cities later became swallowed up inside those cities and their new city walls as the cities expanded to become giant modern cities.
For example, the church and fortress of the Knights Templer was built in 1240 just outside the walls of Paris. Eventually that region was enclosed within a newer outer wall of the expanding city.