Speaking at an Islamic university in Jakarta, David Cameron hailed Indonesia as a model democracy to be celebrated and emulated by countries across the Muslim world. The nation, Cameron claimed, showed that democracy could ensure prosperity, security and religious freedom, as an "alternative to the dead-end choice of dictatorship or extremism".

At first glance, the prime minister's comments deserve commendation, especially in light of the Arab spring. The success of parties representing the Muslim Brotherhood and puritanical Salafi Islam in Egypt's parliamentary elections, for instance, has produced an alarmist response in some parts of Washington DC, London and other western capitals. Islamism now haunts discussions of Egypt's future; fears of sharia law being extended, Coptic Christians facing state persecution, and the abrogation of the peace treaty with Israel are frequently cited alongside the suggestion that military rule would be a preferable alternative to democracy in Egypt. In fact, it is now often suggested that enthusiasm for democracy in the Muslim world should be tempered by concerns about "extremism" and threats to security and religious freedom.

Against this backdrop, Indonesia's transformation into a consolidated democracy does, as Cameron suggests, offer a salutary lesson. When Indonesia's long-time military strongman-president Suharto fell in 1998, his successor was the leader of a prominent Islamic association. New Islamic parties won unprecedented influence in the new parliament elected in 1999; inter-religious violence between Christians and Muslims claimed hundreds of casualties and led to the displacement of hundreds of thousands more in 1999-2001; and the Islamist paramilitary group Laskar Jihad emerged. By 2002, a shadowy network identified as Jemaah Islamiyah had initiated a terrorist bombing campaign that went on to claim hundreds of lives in Bali and Jakarta.

The alarmism turned out to be ill-founded. Islamic parties were drawn into the parliamentary process and soon abandoned calls for sharia to be inserted into the constitution, while inter-religious violence died out by the end of 2001, with the annual terrorist bombings of 2002-05 becoming increasingly infrequent and unsuccessful. Today, Indonesia, a vast, sprawling archipelago with a huge and diverse population, is a stable democracy.

Yet Cameron's shining example is hardly problem-free. The shift from centralised authoritarian rule to decentralised democracy in Indonesia saw businessmen, gangsters and corrupt politicians become entrenched in parliament and regional assemblies, which scholars have described as an oligarchy represented through "money politics" and "party cartels". Centralised corruption and cronyism, therefore, were only replaced by more decentralised corruption and cronyism, as countless reports by Transparency International have shown.

Religious freedom, human rights and civilian control over the military are not perfect either. Belief in a single deity is enshrined in the Indonesian constitution and all citizens are required to avow faith in one of the six recognised religions. Atheism is simply not allowed. And while Muslim-Christian violence and terrorist bombings have faded, the small Ahmadiyya minority has faced a sustained campaign of persecution.

In fact, Indonesia's trajectory might suggest a rather different message – and likely outcome – for countries like Egypt than that suggested in Cameron's speech. The Indonesian National Armed Forces has relinquished some of its considerable powers over internal security, but this has led to large-scale "judicial mafias" and rising human rights abuses by the police, as Amnesty International has documented, rather than effective democratic control over law enforcement.

The president is a retired army general; many other former military officers occupy powerful positions in the government and in parliament; and the army continues to enjoy impunity for human rights abuses (most notably in connection with counterinsurgency in West Papua). The National Armed Forces has resisted pressure to divest itself from its diverse legal business interests, and has retained considerable discretion over a military budget largely free from parliamentary and public scrutiny.

Indonesia is in the midst of a massive military spending spree, with $1.4bn allocated for major arms purchases through 2014. According to former defence minister Juwono Sudarsono (previously Indonesia's ambassador to the UK), military officers may be pocketing as much as 30-40% of the procurement costs in "mark-up fees". Perhaps this is the real context in which we should understand Cameron's glowing account of Indonesia's "inspiring" democracy, delivered against the backdrop of recent multimillion-pound deals for the Eads company Airbus, including a contract signed in February to supply military aircraft to the National Armed Forces.

Perhaps the lessons of Indonesian democracy should be viewed more critically and realistically, whether from Jakarta, London or Cairo.

• Follow Comment is free on Twitter @commentisfree