I have a walled city in a medieval RPG setting. It's out in the middle of a desert and has an impossibly efficient means of acquiring water and producing food off of farmland inside the walls (magic is involved), so its geographical location and internal self-sufficiency make it a very daunting target to attack. However, because of the monsters that infest the world, the walls need to be patrolled during times of peace in order to keep the monsters out; and during the rare times of war, there need to be enough soldiers to effectively defend the walls against determined human attackers.
The walls are between 20-30 feet (6-9 meters) tall, there's no moat or ditch outside the walls; and the walls are, at a bare minimum, 12 miles (20 kilometers, 20,000 meters) long. Assume that the existence of magic doesn't dramatically change the realities of combat, and that you need roughly the same number of defenders to defend the same chunk of wall against the same number of attackers.
With this in mind, how many soldiers would be needed to patrol the walls, and how many would be needed to defend the walls?
EDIT: I suppose I should clarify what they would be defending against. Assume that the defenders have to protect against an army of, say, 20,000 men, or one man per every meter of wall. The walls provide an advantage, but that's a lot of wall to target for various tricks, ruses and attacks.
EDIT2: The attackers are actively attacking the walls, and there is nothing of value outside them, so the defenders have no reason to give up the advantages of the walls. Since the city is self-sufficient, trying to starve out the defenders won't work. The only option for the attackers, then, is to storm the city, either through brute force or trickery.
EDIT3: Of the monsters that try to get in, the ones that are most able to scale the walls are probably about human sized, and have comparable levels of stealthiness, and aren't much more dangerous than an armed soldier... but they operate exclusively at night.