Over the past few weeks, dozens of people have been killed in terrorist attacks on Kenya's coastline. The latest incident, on 6 July, saw 29 people murdered by gunmen in the town of Gamba near the popular tourist resort of Lamu. In June, more than 60 people were killed in Mpektoni, a town in the same region, in the worst incident of terrorist violence in Kenya since the attack on the Westage centre in Nairobi in September 2013. The British government has warned against all but essential travel to much of Kenya's coastline.

The violence has been blamed on the Somali hardline militant group Al Shabaab, but this is just one latest element in a complex web of political and ethnic tension. The deteriorating situation is already hitting Kenya's tourist industry and undermining the country's status as a safe haven in the region. Here, David Anderson, professor of African history at Warwick University, explains exactly what is going on.

What is behind the recent spate of attacks on Kenya’s coast?

Kenya is at war. The invasion of southern Somalia in October 2011 was the first act in that war, and Kenya’s enemy, Al Shabaab, has now brought that war back to Kenya’s door. The securocrats who now run the Kenyan state grossly miscalculated the capacity of Al Shabaab, and this “blowback” has been vastly greater than they bargained for. Al Shabaab see all targets in Kenya as legitimate. There have now been more than 100 attacks in Kenya since the invasion, mostly targeting state infrastructure – police posts and government offices being the commonest target – but also many attacks in public spaces and especially against public transport.

Can it be entirely blamed on Al Shabaab and Kenya’s role in Somalia?

Al Shabaab had recruited a small number of followers from within Kenya prior to the invasion. That recruitment has increased in scale since October 2011. Moreover, several Al Shabaab cells have been established within Kenya. Some of these activists have styled themselves as Al Hijra, and use this name to denote the Kenyan struggle as distinct from the war within southern Somalia. Within Kenya, Al Shabaab is learning to exploit the local politics of Muslim communities, regardless of the ideology or theology of those groups. In short, Al Shabaab has given Kenya’s many Muslim dissidents and malcontents a focus and a structure.

Why has the Kenyan government denied that Al Shabaab are responsible for the coastal attacks?

President Uhuru Kenyatta’s explanation for the attacks at Mpeketoni provided the correct description of what had happened, but drew the wrong analytical conclusion. Mpeketoni was driven by a deeply contested local politics that pitted the Christian community settled at Mpeketoni since the late 1960s against local Bajuni Muslims. This dispute has nothing to do with Al Shabaab, but the organization has been able to exploit the situation for its own ends. Al Shabaab has become the mechanism at Mpeketoni, and elsewhere, through which local Muslims can fight back against a state they believe oppresses them and ignores their views. he politics at Mpeketoni have been bad for a long time; without Al Shabaab there would have been no attack of this kind.

After the 2007 election, the country erupted into ethnic violence. Is there a risk of the current tension reigniting this?

The comparison is misleading because the particular context of the election in 2007 was the driver of what took place in January and February 2008. But the underlying causes of that violence are structural, and remain in place. Despite its rhetoric to the contrary, the Kenyan state has not been successful in integrating its minorities or in securing the interests of groups who are marginal to the core of the state’s economic activities. Kenya’s history over the past 30 years is littered with examples local political violence: groups here resort to violence very readily in seeking to “settle” their problems, and the state has been unable to contain this or to effectively mediate it. Indeed, there is much evidence to suggest that state actors – politicians and, more importantly, officials - have themselves often been directly involved in such violence. In many communities, the state and its agents are viewed as legitimate targets because they are seen as representing specific political factions. This was the case at Mpeketoni. We will now see more attacks of the kind carried out at Mpeketoni, particularly targeting police stations and other places where arms can be pillaged, and where scores can be settled for ‘past wrongs”.The state is now reaping what it has sowed. Whether this spirals into a larger problem of an ethnic character really depends upon how the state responds. The likelihood is that it will be contained with the north and the coastal regions, where the vast majority of the Muslim population lives.

There have been reports of rising Christian-Muslim tension in Kenya. Why?

Many parts of Kenya experience no tensions around faith issues. In general, Kenya has been a tolerant country in this respect. But that should not disguise the fact that the Muslim community, numbering probably around 20% of the total population, has remained disadvantaged. North-East and Coastal Provinces have over several decades lacked infrastructural and economic development. Until 1991 the north had been under a prolonged period of military government, and as treated as a separate part of the country. Northerners do not feel that Kenya has wanted them – and only now that the north has oil and other precious resources does the Kenya state wish to show any real interest. At the coast, neglect there has led to the revival of secessionist politics. The invasion of southern Somalia set a match to the tinder of these long-standing troubles, and the conflagration we now see is its consequence.

Is there a history of religious tension in Kenya, or have ethnic divisions generally been more important?

Ethnic tensions have undoubtedly far outweighed matters of faith in determining patterns of conflict in Kenya. But the rise of evangelical Pentecostalism in Kenya since the 1990s has done nothing to placate the anxieties of Muslims. And the securitization of the Kenya state since 9/11, bringing with it the portrayal of the Islamic fundamentalist as an enemy, has only deepened divisions between the religious communities. Despite the efforts of some leaders on both sides to bring Christian and Muslim together, they now stand further apart than at any time in the last 50 years.

What can be done to calm the situation?

Kenya needs calm, placatory, well-informed and dispassionate government. Muslim leaders at the coast, and community leaders in the north, need to be reconciled with the state and its goals; they need to be embraced and brought inside the state. Kenya’s securocratic government, in which heavy-handed policing and military actions are justified in an atmosphere where xenophobia against Somalis and Muslims is being fostered, cannot achieve the political compromises that are needed to build these alliances. Moderate Muslims have been rapidly alienated by the actions of the government in the past two months, with round-ups of ‘suspects” in Operation Usalama Watch and the random harassment and beatings that went with it marking the watershed moment. Since then, several further ‘crackdowns’ have made matters worse, without improving the security situation in any respect. In short, Kenya is losing its war in Kenya. By invading southern Somalia it has opened the door to the regionalization of Al Shabaab. This will undermine Kenya’s security for years to come, and potentially the security of the rest of East Africa as well.