In a nation wracked by drug violence, this sprawling capital city of more than 20 million has been an oasis of relative peace. But the key to that calm - an informal truce among rival gangs - may be cracking.

On a sunny afternoon this month, a group of gunmen drove into a slum in the north of Mexico City, the streets packed with shoppers and children leaving school. In plain sight, the killers lined three crack cocaine dealers against a wall and shot them in the head with AK-47 assault rifles. They then forced another two men into a black van and drove away past terrified onlookers.

The killings, allegedly carried out by the bloodthirsty La Familia cartel of the central state of Michoacan, were the latest sign that the drug violence raging across large swathes of Mexico is creeping into the capital.

The drug lords have long kept a lid on turf wars in Mexico City. But a generation of upstart gangsters has this year carried out a series of massacres and decapitations on the city edges. Cells of these newer cartels have also become more active in kidnapping and shaking down local businessmen.

In the greater Mexico City area, police have reported more than 300 gangland killings this year. The carnage includes the massacre of a family of five in the Tlalpan area, a decapitation close to the wealthy business district of Santa Fe, and two headless bodies hanged from bridge in Huixquilucan in the west of the city. The death toll is up from last year, when 260 murders in the area were blamed on rival gangs.

Mexico City includes the inner Federal District, home to almost 9 million people, and another 12 million in outer suburbs and slums governed by the State of Mexico.

A cartel crime wave here would be catastrophic, says Luis de la Barreda, head of ICESI, a Mexican think-tank on crime. Mexico City is not only the home of all the country's major institutions, it is an image that is constantly in everyone's minds.

The capital, to be sure, remains one of the safest parts of the nation. Ciudad Juarez on the U.S. border was last year the most murderous city on the planet. The tourist resort of Acapulco has been hollowed out by violence. Even the affluent business city of Monterrey has been ravaged. But the Federal District boasts a lower homicide rate than many U.S. cities.

Many wealthy Mexicans have retreated here from violent enclaves, setting up new businesses and helping to boost property prices. Poorer families have fled from the bloodshed around the country to shanty towns on the city edges.

But there are signs the capital could go the way of other regions. The Guadalupe Victoria neighbourhood - where gangsters shot dead the three alleged crack dealers in broad daylight - is typical of the slums the new cartels are moving into.

It is in the far north of the metropolitan sprawl, beneath shanty towns that spiral up dusty hills, a two-hour commute from the heart of the capital. The victims represented a problem relatively new to Mexico - a growing population of addicts and dealers who sell rocks of crack cocaine for as little as 30 pesos ($2.15).

Although the gunmen shot the alleged dealers right in front of a row of shops, store owners are too scared to talk about it. Most denied seeing anything, saying they were busy or their view was blocked.

TRUCE

Until a few years ago, when kidnappings and armed robbery were the biggest threats, Mexico City was seen as one of the most dangerous spots in the country. But it has enjoyed a relative calm while other regions were engulfed by turf wars triggered when President Felipe Calderon went after the cartels in late 2006.

The capital even seemed to be a safe place for the families of gangsters. Vicente Carrillo Leyva, son of the Juarez cartel founder, was arrested in 2009 as he exercised in the park of a plush suburb wearing an Abercrombie & Fitch jogging suit. Vicente Zambada, an heir to the rival Sinaloa cartel, was nabbed the same year driving through the upscale district of Lomas de Pedregal.

While cartel leaders kept money, houses and families in the capital, they were extremely cautious about unleashing violence on its streets. Security analysts say gangsters had a tacit understanding not to set off alarm bells in the heartland of Mexico's political power.

The murder rate tells the story. In the last three years, there were between 8 and 10 homicides for every 100,000 residents of the Federal District, police figures show. That is about half the national rate and much lower than U.S. cities like New Orleans, Baltimore and Detroit.

Meanwhile, Sinaloa state, the cradle of the drugs trade, had 81 murders per 100,000 last year. Ciudad Juarez, across from El Paso, Texas, had a horrifying rate of 272 homicides per 100,000.

The truce in the capital is now threatened by the intensity of turf wars elsewhere and the emergence of three cartels of a new ilk: La Familia, the widely-feared Zetas and a criminal cell called Mano con Ojos - or Hand With Eyes. These groups have all become major players, radicalized amid the fury of the drug war.

Many kingpins of these new cartels were once assassins and use violence as a basic form of communication instead of a last resort. The three groups have been fighting over turf in states surrounding the capital for years. Recent violence shows they may be spreading this war into the periphery of the city.

Police have hit back by raiding dozens of safe houses, busting gangsters holed up with guns, drugs and money. In some of these houses, usually rented properties in residential streets, agents have rescued terrified kidnap victims.

In one case, detectives arrested 14 men and women who allegedly formed a cell carrying out kidnappings for the Zetas cartel in the northern parts of the city. Detectives say the gangsters always demanded ransom payments in dollars and collected them at passenger bridges. One victim was a pregnant woman. The criminals cut off two of her fingers and sent them to her husband, in a box to pressure for the ransom.

Another gangster arrested in Mexico City was a leader of the Mano con Ojos gang called Oscar Osvaldo Garcia, nabbed by police in August. Garcia, a 36-year old former Mexican marine, allegedly spent many years working as a hit man for older kingpins of the Sinaloa Cartel.

But his bosses were taken down as part of Calderon's war on drugs and he began to head his own operations, recruiting young men from the Mexico City area to sell drugs, kidnap and kill.

TRAINED KILLERS

His career path underlines a central problem with Calderon's offensive. As the older kingpins are captured or killed, bloodthirsty lieutenants have risen up to fight over their empires.

I was trained to kill, Garcia told police in videotaped testimony. He acknowledged murdering not only rival gangsters but dozens of witnesses. They were innocent but they had seen too much. They had seen too many faces, and they had to go.

The attorney general of Mexico State, Alfredo Castillo, concedes the gangsters extort businesses in the area, a tactic of increasing concern across Mexico.

Rather than going after big companies or foreign ventures, they hit local vendors - taco stands, hardware stores and clothes stalls on the edge of the capital. Police arrested four such extortionists on December 12, alleging they were members of La Familia and shaking down businesses for 500 pesos ($36) a week each in the Cuautitlan area in the north of Mexico City.

Most of the affluent neighbourhoods have not been affected. In trendy areas such as La Condesa, residents enjoy cappuccinos, sushi restaurants and Irish pubs with no sign of gunmen or soldiers.

Hugh Carroll, an offshore investment banker from Scotland, has lived here almost 10 years and hasn't felt any personal effect from the drug war. I tend to operate in business areas, which are all very safe, Carroll says. The worst thing that ever happened to me is that I was mugged a few years ago, but that can happen anywhere in the world.

Mexico's biggest security company, Multisistemas de Seguridad, still considers Mexico City a relatively low risk area. In places close to the border such as Tamaulipas, there are real warlike conditions, but we have seen nothing like that here, says company spokesman Gabriel Avalos. The incursion of these cartels is worrying, but it hasn't yet had a major effect on violence in the city.

Avalos says the Federal District's government, led by Mayor Marcelo Ebrard, has helped keep wealthier neighbourhoods safe. After visiting London, Ebrard set out to install 8,000 cameras by 2012, when he leaves office. These eyes in the sky are on many street corners in plush districts and have been used to catch muggers and other criminals.

The Federal District's police officers are more effective than those in much of Mexico. While the rest of the country has different state and municipal police forces that often fight each other, the Federal District has a unified force.

If other Mexican police forces were to follow this model it would be a positive development, says Jon French, a former U.S. State Department official who runs a Mexico City-based security consultancy.

With the city still relatively secure, Mexicans continue to take refuge here.

Diego Viloro moved from his native town of Uruapan, Michoacan to settle here in February. Uruapan was the scene of one of the first high-profile atrocities of the war when thugs rolled five severed heads onto a nightclub dance floor in 2006.

Viloro owned a grocery store but fled when gangsters threatened to kill him in a row over extortion payments. He left a big house and decent living, he said, to rent an apartment and get by driving a taxi.

It worries me a lot when I see news about La Familia and Zetas on the edge of this city. That was how it started in Michoacan and it just got worse and worse, Viloro says.

I don't want my children growing up around that fear and bloodshed. That was why I moved here.

(Editing by Kieran Murray and Chris Kaufman)

(Editing By Chris Kaufman)