I don't think you understand what cities are for, or why they exist. There's no upper limit to how big a city can be. It's actually good to get as much people in as little area as possible. It has loads of synergistic environmental benefits. As well as synergistic economic benefits. There's a reason cities are magnets for people. As long as infrastructure keeps up. Which shouldn't be a problem. Because the more people, the cheaper maintenance will be, per person. Because of economic synergistic effects people who live in cities have a much easier job of making money. So not only is maintenance cheaper (per person) but also easier to finance (per person). The bigger the better.
The problem of Mogadishu isn't the high population. Neither the density. It's only 2 500 000 people, smack bang in sub-Sahara's most abundant farming land. The problem of Mogadishu is civil wars, unstable governments, poverty, piracy and so on and so forth.
Another problem large cities is often lack of planning. You've got to adjust the infrastructure to fit the population. Private initiatives just won't cut it. It needs plenty of government organisation and planning. Sydney is a good example of what happens when the government doesn't plan enough. There you have an extremely badly designed city. Well... it's not been designed at all. It just sort of happened. Istanbul suffers from that the various councils get paid depending on how many people have registered that they live there. The problem is that Turks, out of loyalty to their families back home, then register in that village. So Istanbul is disastrously underfunded. It's infrastructure is adapted to about half of it's current population. But that's just down to stupid design. Rio de Janeiro just don't have the funds. It's population are too poor, even though they live at the most lucrative spot for them.
The world is full of extremely well designed dense super cities. Tokyo, Shanghai, New York, Amsterdam Hong Kong, Seoul and so on. If they fix the roads Nairobi is another good one. These are just off the top of my head.
Nah, if we want a prosperous future we need to swell our cities. It's the simplest way to cut greenhouse emissions while simultaneously maximising economic growth. It's win-win. Living in the country is extremely wasteful of resources. And a luxury we're having less of an ability to afford.