It's easy to forget, wandering through Nairobi, that this is a nation at war. There are a few clues – the mirror checks for car bombs, the metal detectors in supermarkets and public buildings, the odd grenade outside a nightclub – but the battlefield is far away and the fighting sporadic. In this year's presidential election, the war wasn't even a campaign issue, with candidates preferring instead to bicker about parliamentarians' salaries and the International Criminal Court.

There's no forgetting now.

With at least 68 people dead in the Westgate shopping mall and more than 175 others nursing bullet injuries, the war has arrived in Nairobi in the most tragic possible way.

Kenya can't say it wasn't warned. Ever since October 2011, when 4,000-odd Kenyan troops were summarily dispatched across to the border into Somalia with a mandate to hunt down and destroy al-Shabaab, the Islamist militant group has been promising a massive, bloody revenge. Although it was always tempting to dismiss al-Shabaab's hyperbole as empty, Comical Ali-style bluster, the group has form when it comes to revenge.

Remember it is only three years since the last major terrorist attack in Africa, when 76 died in twin bombings in the Ugandan capital Kampala as they watched the World Cup final. This was in direct response to Uganda's military intervention in Somalia, involving thousands of Ugandan soldiers operating under the aegis of the African Union Mission in Somalia.

Of course, being warned is not the same as being able to prevent these kinds of attacks. Nairobi's gunmen were clearly inspired by the Mumbai terror attacks in 2008, which analysts said at the time could be the template for terrorism of the future. Easier and cheaper than bombs, requiring just a handful of machine guns, plenty of ammo and a few men (and, in Nairobi, at least one woman) willing to die for their cause. And without going on full, permanent lockdown, what can cities do to prevent such an attack?

Still, in the light of this weekend's tragic scenes, it is worth revisiting Kenya's sudden decision to get itself involved in Somalia. Unlike Uganda's internationally approved military support for Somalia's fragile central government (along with troops from Burundi, and more recently Djibouti and Sierra Leone), Kenya's was a unilateral intervention that took everyone by surprise. And their goal was less about restoring stability in Somalia and more about wiping out al-Shabaab and establishing a de facto buffer state between the two countries, a buffer state it hoped would keep Somalia's instability from spilling over its borders and threatening Kenya's vital tourism and shipping industries.

Before Kenya went to war that is exactly what had been happening. In the absence of any effective government, piracy in Somalia flourished, as did kidnappings for ransom. As pickings grew slim in Somalia itself, the pirates, kidnappers and militant groups trying to fund their operations looked further afield, with Kenyan tourist resorts – flush with wealthy western tourists – identified as perfect targets. Two foreigners were taken from two different coastal resort towns, while another two foreign aid workers were abducted from a refugee camp near the Somali border. The future for Mombasa port was also looking grim, with shipping companies looking for alternative destinations that would take them out of pirate range. Along with the flood of Somali refugees into Kenya, this was all too much for the country's unpopular government of national unity (who would also have welcomed the distraction from their own governance record and the popular approval boost that so often accompanies major military action).

And so Kenyan troops were sent in to one of Africa's most intractable conflicts. Al-Shabaab, then in control of most of Somalia (and alleged to have participated in some of the Kenyan kidnappings) was the obvious target.

Kenya's involvement was welcomed cautiously by the African and international diplomatic community, and has fundamentally altered the balance of power in Somalia. Whereas Amisom's approach was essentially defensive, Kenya went straight on the attack and ousted al-Shabaab from most of its important strongholds, including its de facto capital in the port city of Kismayo. It allowed the federal government in Mogadishu to extend its reach and gave Amisom the encouragement it needed to secure Mogadishu properly, which explains the mini renaissance some say the city is currently enjoying (including hip restaurants, ice cream shops and TEDx talks).

The pressure has hit al-Shabaab hard. Divisions in the group deteriorated into infighting, with several prominent defections. It has lost plenty of territory it once controlled, although it still remains in charge of vast swathes of the country. This pressure, ironically, may explain the timing and the spectacular nature of the Nairobi attack this weekend. In the face of fading influence, it is fighting to stay relevant and remain in the headlines.

Subsequent to the unilateral invasion, Kenyan troops were folded into Amisom and given a seal of international approval. Yet this retrospective legitimisation could not disguise that their intervention was in fact an invasion, and that the risk of blowback was always going to be high, especially when it became clear that Kenya's involvement was seriously hurting al-Shabaab.

On that front, al-Shabaab has delivered. Kenya's leaders now have some serious, difficult questions to ask themselves. Why is Kenya in Somalia? Is it worth staying? How likely are repeat attacks? Crucially, with President Uhuru Kenyatta already promising to "punish" those responsible, they need to ask whether sending even more troops in Somalia, with an even more aggressive mandate, is the best way to protect Kenyan citizens in the long run.

Also important for everyone to remember is that al-Shabaab's actions do not define Somalia, or Somalis (after all, al-Shabaab's primary target has always been Somalis themselves). Kenya has a sizeable Somali population and an entire suburb (Eastleigh) that is known as the Somali district. This has previously been the site of xenophobic violence, with Kenyans targeting Somalis in response to other attacks attributed to al-Shabaab. Kenya's leaders have a duty to remind their citizens that this is not acceptable.

There is also a worry that this tragedy will be used as a bargaining chip in the trials of Kenyatta and his deputy, William Ruto, at the International Criminal Court. Already Ruto has requested that the court delay his trial so that he can deal with the aftermath of the massacre and there are rumours that Kenyatta wants to use it to bolster his argument that he is unable to attend his trial in person.

In the middle of this ongoing tragedy, Kenya needs good leadership more than ever. This is Kenyatta and Ruto's chance to show they can provide it, even if they haven't in the past. They owe the Westgate victims at least that much.