Looking across the gaping stone canyon at the ancient walled city of Kamyanets-Podilskiy in southwestern Ukraine, you can imagine what it must have felt like to ride up to this place at the head of an invading horde.

It would have been a sinking feeling, as deep as the gorge in front of you.

The old city is almost entirely enclosed by the canyon created by a loop of the Smotrych River. To get to the town, you would have to lead your men straight down a rock face more than 100 feet high, then across the river, which would have been flooded, thanks to some ingenious dams. If you managed to get to the far bank, you would have to scramble up another cliff. Even without the people inside shooting at you, your horde would be pretty grumpy when you got to the top. Oh, and then you'd be facing the city walls. (“One of you knuckleheads did remember to bring the ladder, didn’t you?”)

The other option would be to advance across the narrow isthmus at the mouth of the loop. But that was defended by a massive stone behemoth that looked like something out of a very dark legend — all turrets, gun ports and towers.

The walls are still there today, but now this enchanting city welcomes tourists. And if you want to get inside the ancient fortress, all you need is a ticket that costs less than $2.

Nobody knows when people first used this natural citadel to hold enemies at bay, but it was certainly occupied by the 11th century. Mongols overran the city in about 1240. After that, the defenses were stiffened.

The older of the two fortresses that guard the main approach to the town began to take shape in the 14th century. Its walls are 45 feet high and almost 15 feet thick. In 1621, a second fortress of massive earthen walls was built in front of the Old Fortress.

Over the years, the fortifications have repelled more than 50 assaults and sieges. According to legend, when the Turkish Sultan Osman arrived at the city in 1621, he asked who had created such a forbidding stronghold. “It was created by Allah himself,” came the answer. “Well then, let Allah storm it,” the sultan responded, before withdrawing.

The fortress has rarely been taken by direct attack.In 1393, a Lithuanian prince conquered the city thanks to dissension in the ranks of the defenders. And in 1672, a Turkish army outnumbered the garrison by a ratio of 60 to 1. After the city fell, Mehmed IV supposedly trampled looted icons as he rode into town.

You can clamber over the remains of the New Fortress without a ticket. Inside the Old Fortress, you’ll get an idea of what life was like for the soldiers, see the hole where debtors were flung if they couldn’t pay their bills, and examine the well in one of the castle towers from which soldiers hauled up water using a device that looks like a giant hamster wheel.

It may come as something of a surprise that such a massive and well-preserved bastion stands in Ukraine. But if Mesopotamia is the cradle of civilization, the lands of present-day Ukraine are probably the cradle of conquest. Much of the land is rich and flat, perfect for farming and war. Scythian, Mongol, Tartar, Cossack, Russian, Polish, Turkish, Lithuanian, Austrian, Hungarian, Swedish, English, Greek and German armies have all come this way.

Which is why people in Ukraine have spent a lot of time building walls. The country holds more than 300 citadels and castles. They range from walled monasteries along the Dnieper River to Genoese towers on the Black Sea, built to guard trading outposts.

Kamyanets-Podilskiy is one of the most impressive. The old part of the city — in the oxbow of the river — is not much more than half a mile long and is full of old buildings and historic churches. We hired an English-speaking guide for a 2 1/2-hour walking tour for about $20.

Wandering the cobbled streets, you can see the Windy Gate (remembered as the place where a breeze had the audacity to pluck off Peter the Great’s hat in 1711) and the partially reconstructed Polish Gate, which guards a ford in the river. There’s the old city hall, which now houses three small museums, and the ruins of a 15th-century Armenian church. The facade of the Cathedral of Sts. Peter and Paul cradles a 17th-century minaret built when the Turks ruled the city. When the Poles got the town back by treaty in 1699, they decided not to knock it down, but topped it with an 11-foot golden statue of the Virgin Mary.

Signs in Ukrainian and English point the way to the main attractions.

Several hotels have opened in the old city and several more are in the new city within easy walking distance. We stayed in one across the river, a 10-minute walk from the historic district.

The old city suffered heavy damage during World War II. Many of the drab postwar buildings have recently been replaced with modern buildings that look as if they could have been built centuries ago. Locals, such as Ukrainian castle expert Iryna Pustynnikova, find this quite disappointing, because drawings of what the old city looked like could have guided authentic restorations. Local authorities opted for plans more likely to attract investors. I didn’t find the new stuff offensively Disneyish, but more discerning people might.

For centuries, the city had a remarkably diverse population — Poles, Ukrainians, Russians, Jews, Armenians. But with Soviet rule came many deportations. And the Nazi occupation during WWII was followed by terrible massacres. More than 23,000 Jews were killed near the city in three days in August 1941.

That incident is not prominently mentioned in tours of the city, but it is the one that came back to me as I wandered the ancient fortifications. Today, we see such ramparts as quaint architectural marvels, almost like something from a fairy tale. But people built them out of fear — because conquerors exact a terrible price.

Pancake is a former editor at The Washington Post.