I don't know the optimal solution to deal with ISIS or Islamic terrorism in general, but I think one of the most crucial things is to avoid overreacting.
One of the worst things a country can do when it faces a few violent acts of terrorism is to totally alter its policies to the detriment of economic growth or other variables. Or elect crazy people or slide towards fascism or isolationism or racism.
In some countries, terrorism is a major problem, but in developed countries, terrorist events kill only a fraction of a fraction of a fraction of one percent of the population. And any given type of terrorism, like Islamic terrorism for example, only constitutes a fraction of the total terrorist events in a given year. In the west, right-wing or nationalistic terrorism is the second biggest terrorist group along with Islamic terrorism.
Trying to stop those events when possible is certainly important, but everything has to be kept in perspective.
Osama Bin Laden was quoted in Al Jazeera a decade ago as mocking the United States for falling into his trap, when we spent trillions of dollars and thousands of soldier lives to fight in the middle east after his terrorist hit on 9/11. He damaged us far more in the ten years after that event due to our overreaction, than he damaged us during that event itself.
Now if ISIS kills a few hundred people in Europe or the United States, or even more indirectly if a few lone wolves kill people due to being inspired by ISIS, and our countries all radically change our policies affecting millions of people by taking away privacies or freedoms of our own citizens, or going to war, then we fall into their trap again.