Viewed through a prism of headlines, Indonesia can easily appear to be an unstable nation being ripped asunder by radical Islamists. Four big terrorist attacks by locally recruited militants in three years - the 2002 Bali nightclub bombing, the 2003 bomb at the Jakarta Marriott hotel, the 2004 bomb at the Australian embassy in Jakarta and Saturday's second Bali bombing - suggest not only incompetent security forces but something profoundly wrong with society.

A further problem is the authorities' refusal to rein in orthodox Islamist groups that have bullied more than two dozen churches into closure over the past two years and repeatedly attacked the Indian-based Ahmadiyah sect's premises on the grounds of alleged deviancy, as well as a decision by the national ulemas council to ban pluralism and liberal teachings.

The most populous Muslim nation undoubtedly has its problems. Outposts of radicalism have taken root in much of the sprawling archipelago over the past seven years and militants continue to stoke communal conflict in the eastern islands of Sulawesi and the Moluccas.

Jemaah Islamiyah, the terror network linked to al-Qaida that wants to turn most of south-east Asia into a caliphate, has put down deep roots in the country and some leading members, such as Azahari Husin - the Malaysian being blamed for orchestrating the last three of the four attacks - have been forming their own organisations with even more radical agendas. Azahari's is called Thoifah Muqatilah (combat unit) and it is thought to want to escalate the struggle. Like the organisers of the London attacks, he uses fresh recruits unknown to the authorities who are willing to make martyrs of themselves.

Azahari and his cohorts are tapping into the resources of other radical groups, such as Kompak, based in Sulawesi; the Indonesian Mujahideen Movement, whose leader is Abu Bakar Ba'asyir, the alleged former head of Jemaah Islamiyah; and Darul Islam, a 55-year-old network that spawned most of the newer offshoots, including Jemaah Islamiyah.

Afraid of being seen as western pawns by the country's Muslim majority, the last four presidents have declined to crack down as hard as they could have on these radical groups, thereby allowing them to expand. The government and its people are now paying the price, having to quell extensive periods of unrest and prevent terrorist attacks with security and intelligence forces which, until very recently, were far from first-rate.

Having said all this, the radicalism must be placed in context. Despite its impact, the movement's numbers are tiny and not growing fast. And despite the perceived global assault on Islam - whether in Iraq, Palestine or elsewhere - the vast majority of Indonesia's 190 million Muslims remain moderate. Islam arrived in Indonesia through trade rather than conquest, so not only does it lack some of the characteristics prevalent elsewhere but it is also diluted by cultural traditions that predate its arrival. This is becoming manifest in domestic politics: Islamist parties are faring well but only because they espouse clean, well-run government and shy away from demanding an Islamic state.

And history cannot be ignored. Radical Islam was born during the colonial era but was violently repressed during the 32-year dictatorship of General Suharto, supported by the west. When his regime collapsed in 1998, it was as if the lid had been blown off a pressure cooker. Radicalism thrived on the oxygen it had been starved of.

The other major development in Indonesia since 1998 is that it has transformed itself into a flourishing democracy. Indonesians directly elected their president for the first time this year and a return to authoritarianism seems unlikely. A new respect for law and order means that Indonesia is not willing to copy Malaysia and Singapore - or the United States - by detaining alleged militants and terrorists indefinitely without charge.

The Bali bombings are undoubtedly a partial consequence of this openness and no one doubts there will be more attacks. While the great majority of the nation condemns them, there seems to be an acceptance that giving everyone a voice is part of the price of becoming a democracy. Indonesia has shown the world the world how a predominantly Islamic country can embrace democracy. Alas, it is also showing the world that the transition can be costly.