Tunisia is considered the lone survivor of the Arab Spring, the only nation to build a real democracy out the revolutions that swept the Middle East in 2011.

But its borders with war-torn Libya and Algeria – which has long battled its own al-Qaeda insurgency – have always made its stability precarious. At the same time the tiny country has seen up to 4,000 of its young men join Islamic State (IS) to fight in Iraq, Syria and Libya.

That face of Tunisia, which was visited by 400,000 British tourists last year, was shockingly exposed yesterday when armed gunmen stormed the Bardo Museum in downtown Tunis and murdered 20 European tourists. It has since emerged that the gunmen Yassine Labidi and Saber Khachnaoui were Tunisian nationals and may have trained in eastern Libya.

The attack was the worst to hit Tunisia in over a decade and recalled the spate of violent attacks on tourists that rocked Egypt in the late 90s and early 2000s, including the massacre of over 62 people – 58 of them tourists – at Luxor in 1997.

"Such an attack was nearly inevitable given the disproportionately large number of Tunisians who have travelled to fight with the Islamic State, as well as being squeezed in a vice of extremism with Libya to the east and Algeria to the west," Soufan Group, an intelligence consultancy, said in a briefing note on Thursday.

Soufan claimed that IS and al-Qaeda in the Islamic Maghreb (AQIM) is battling to take advantage of regional instability, and pointed to unconfirmed reports that one of the attackers had an Ansar al-Sharia flag in his pocket. Meanwhile pro-IS Twitter users had been warning of an attack on Tunisia in recent weeks.

Ansar al-Shariah was founded by a Tunisian, Abu Ayadh al Tunisi, after his release from prison following the overthrow of dictator Zine El Abidine Ben Ali in January 2011. Tunisia designated the group a terrorist organisation in August 2013 and implicated it in the murders of Chokri Belaid, the secular politician, who was shot dead in February of that year.

The attack is all the more tragic, argued Human Rights Watch Middle East and North Africa director Sarah Leah Whitson, because of Tunisia's status as the one democratic state to come out of the Arab Spring without either descending into authoritarianism or war. The challenge for the government now will be continuing that legacy in light of the attacks.

"The test is going to be how to balance liberal democratic freedoms that they have struggled so hard to achieve with the need to have enough security to give people assurances that they are safe and bring tourists back," said Whitson.

"This is the most unfair challenge that has been thrust on Tunisia. They deserve a break, they deserve support. [That] a few guys could tear it all down that is what is so devastating about all this."

Tunisia is the only Arab Spring country to go through a full democratic cycle – with an Islamist post-revolution government fairly elected and replaced by a secular government last year. This contrasts with Egypt, where a post-revolution Islamic party was overthrown by a military coup in 2013 and the former president is now in prison along with thousands of his supporters.

But it could be that this success helped make Tunisia a target, said Susi Dennison, a senior fellow at the European Council on Foreign Relations.

'The fact that it is a working democracy makes it a target for the groups that see that as a manifestation of being too closely allied with the West," she said.

In having to deal with young Tunisians travelling to the Middle East to join ISIS, Tunisia shares a challenge faced by European nations, such as France and UK. Four years after its revolution, poverty and economic malaise continues to be a challenge for Tunisia and tourism – the one industry that it needs more than ever – will be hit hardest.

"It is hard to see how this won't have an effect on tourism figures, the fact that it happened at a major cultural site and the fact that a high proportion were foreign tourists. Inevitably that will gave a negative impact on numbers coming in," she said.

There is a concern that President Beji Caid Essebsi invocation of a "war on terror" following the attacks could lead to an erosion of freedoms that Tunisia has fought so hard to maintain, but in reality there is only so much that the country can do alone. With Libya descending further into chaos and Algeria showing no signs of reining in Al Qaeda, a regional response will be needed.

"We have to keep in mind the neighbourhood that Tunisia is sitting in... The government has a significant challenge [ahead]," said Dennison.