Civilians flee the Nairobi hotel complex where terrorists killed at least 14 people on Tuesday.Khalil Senosi / AP

NAIROBI—On a warm Tuesday afternoon here in Kenya’s capital, a suicide bomber blew himself up outside the Secret Garden Café, a restaurant within an upscale hotel and office complex. The blast shredded the bodies of customers sitting at tables on the restaurant’s ground-floor veranda, threw debris across the grassy courtyard, and shattered windows six floors up.

Minutes after the bombing, gunmen detonated more explosives at the security barrier leading into the development, leaving three cars engulfed in flames. Security-camera footage shows four black-clad men—one of them carrying a large backpack, all armed with assault rifles and wearing vests that held additional magazines of ammunition—walking purposefully into the complex, breaking stride only to aim occasional shots at unseen targets.

The attack is the latest in a series of terrorist incidents in Kenya in recent years that have shaken the country, and that offer lessons internationally, not just in the difficulty of containing the blowback from a decision to intervene in a neighboring conflict, but also in how a government can overhaul a security apparatus to better respond to such an assault.

When Tuesday’s attack began, shortly after 3 p.m. with the suicide bombing, Caroline Muhati was eating a late lunch by a balcony at one of the office buildings inside the complex. “I thought maybe it’s a fault in the electricity,” the 23-year-old, who was just a few days into her new job as an office cleaner, told me. “But then I heard gunshots.”

Muhati first hid in a storeroom, but finding herself alone in there and wanting desperately not to be by herself, she ducked out and ran to a nearby bathroom. Again, she found no one. “I was all alone. I was so afraid,” she said afterward as she crouched on a pavement, shaking and sobbing.

The attack, which left at least 14 people dead and others wounded, had immediate echoes of a similar terrorist assault here more than five years ago, on the Westgate shopping mall. Within hours, the same perpetrators of that incident—al-Shabaab, an al-Qaeda affiliate based in Somalia—had claimed responsibility for Tuesday’s killings.

For more than a decade, al-Shabaab has fought to overthrow successive, weak internationally backed governments in Somalia, with little success but much loss of life. When Nairobi deployed its army to southern Somalia in 2011 to fight the jihadists on its border, the terrorist group turned its attention to Kenya. The fallout has been costly: 67 people were killed in the Westgate attack in September 2013, a further 148 died in an assault on Garissa University two years later, and on Tuesday more perished at the “14 Riverside” development, including one American.

In Mogadishu, the Somali capital, al-Shabaab militants have frequently carried out complex attacks like the one on Tuesday, using suicide bombers and explosives to breach compounds before sending in gunmen to kill civilians. The group has even exported this methodology, to Uganda and Djibouti. Tuesday’s assault marked al-Shabaab’s first use of a suicide bomber in Kenya, though.

The terrorist group said it had targeted an “opulent area” housing “international representatives.” Nairobi is home to numerous multinational companies as well as large diplomatic and United Nations missions. It is a cosmopolitan place and a long-standing ally of the United States and other countries focused on fighting terrorism in the region. In many ways, 14 Riverside represents this international outlook. The mixed-use development includes the Thai-owned five-star hotel DusitD2, with its plush suites, expensive eateries, and trendy cocktail bars. The complex’s four office blocks house technology, health-care, leisure, and finance companies from around the world. In short, it is just the kind of place al-Shabaab abhors: one of liberal attitudes and Western-style affluence.

The similarities to the Westgate attack are stark—four gunmen raiding a prominent city landmark in a bid to inflict as many casualties as possible while attracting as much attention as possible. But there are differences, too, for better and worse.

Westgate was, in the end, defined by ineptitude and failures. The response was so slow that by the time security forces entered the mall, most of those shot were already dead. Turf wars between the army and police led to a botched rescue operation and a deadly friendly-fire incident.

But on Tuesday, the army stayed away, and command was handed to a specialist paramilitary police unit that arrived quickly and worked effectively. (Its efforts were complicated, however, by the attackers, who had left live grenades in their wake to slow the officers as they made their way through the office buildings and then to the hotel.)

In one case, a Dubai-based businessman who did not want to be identified said he and his colleagues had bolted for a hotel room when the attack began. Sometime later, they heard people moving along the corridor, kicking in doors, and shouting. “You don’t know if they’re the bad guys or the good guys,” the businessman said. “I just closed my eyes and prayed.” It turned out that the men who had stormed into the room had been police officers, who were part of the force that helped about 700 people escape the large complex by variously crouching, taking cover, and running until they reached the safety of the street outside.

The attack on Westgate lasted a few hours, and the ensuing security operation went on for days. With the mall looted, burned, and partially collapsed, the government eventually claimed victory. This time, by Wednesday morning, roughly 20 hours after the assault had begun, President Uhuru Kenyatta was able to declare the operation over and the terrorists dead.

The toll, however, was nevertheless considerable.

“They were this close to us,” said Kenneth Kirumba, a young software developer, his voice quivering as he held up his thumb and forefinger. Kirumba had huddled with his colleagues in the restrooms on the fifth floor of one block and locked the stall he was in. The gunmen, who were systematically making their way through the buildings, kicked in the door of the bathroom and fired a single shot before moving on.

When the police arrived, Kirumba emerged to see his friend and colleague lying on the floor lifeless, with a single gunshot to his head.

“We were a dozen in the toilets,” Kirumba said, wiping away tears. “Eleven made it out.”

We want to hear what you think about this article. Submit a letter to the editor or write to letters@theatlantic.com.