AT ISTANBUL’S naval museum, around the corner from President Recep Tayyip Erdogan’s residence, reminders of one of Europe’s biggest geopolitical rivalries are everywhere. A bust commemorates Hasan Pasha of Algiers, a commander in a battle in which the Russian fleet burned the Ottoman one to a crisp. The remnants of the Mahmudiye, a galleon that led the siege of Sevastopol during the Crimean War, overlook rows of gilded boats used by the sultans. Such flare-ups are not just things of the distant past. In 2015 Turkish pilots shot down a Russian warplane, and the two powers appeared on the brink of another war. It would have been their 18th.

Instead, the two countries resorted only to insults and sanctions. Since then tensions have ebbed: in June last year Mr Erdogan apologised for the incident. Two weeks later, Russia’s president Vladimir Putin rushed to condemn a violent coup attempt against Turkey’s government. The two countries have subsequently signed a big gas pipeline deal, agreed to resume work on a nuclear plant in southern Turkey and pledged to increase bilateral trade by more than fivefold, to $100bn a year.

Even more strikingly, as European and American diplomats watched from the sidelines, in December the pair brokered a ceasefire in battle-scorched Aleppo and agreed on a plan to stop the fighting in the rest of Syria the following month. At the height of their dispute, Mr Putin and Mr Erdogan were accusing each other of supporting the so-called Islamic State (IS). Today, the two autocrats are coordinating airstrikes against it in Syria.

The speed and the scale of the rapprochement between the two countries, which was unruffled even by the assassination of the Russian envoy to Ankara by a Turkish policeman in December, is startling. Yet the two strongmen have different and contradictory expectations. Whereas Mr Erdogan appears to see his relationship with Mr Putin as a way to extract concessions from his Western allies, Mr Putin wants to loosen what he sees as one of the weakest links inside NATO—Turkey. One of the two is likely to be disappointed.

Mr Erdogan decided to make friends with Mr Putin partly because having him as an enemy was so painful. After Turkey shot down that Russian plane in 2015, Mr Putin cut Turkey off from the Middle East. His fighter jets bombed Turkey’s proxies inside Syria, including its ethnic cousins, the Turkmen, with impunity. Russia’s missile defences denied Turkey access to the airspace over Syria. Russian sanctions cost Turkey at least $10bn in tourism and trade revenue.

Russia remains the stronger partner. Mr Erdogan’s government has offered Rosatom, the Russian company building Turkey’s first nuclear plant, sweeteners worth billions of dollars. It has endorsed Turkish Stream, a gas pipeline that would allow Russia to extend its grip over Turkey’s and Europe’s energy markets. (Turkey already depends on Russia for 55% of its natural gas imports.) Most importantly, Mr Erdogan has reversed course on Syria, abandoning his dream of ousting its blood-drenched president, Bashar al Assad.

In exchange, Russia has allowed Turkey’s army to set up a buffer zone inside Syria. Turkey has seized the chance to push IS back from its last border strongholds and stem the advance of American-allied Kurdish insurgents, known as the People’s Protection Units (YPG). Mr Putin has been slow to reciprocate in other areas, however. Most of the sanctions Russia imposed on Turkish food products in 2015 remain in place. “It seems as if they’re still rubbing our noses in it,” says Cenk Baslamis, a veteran Russia observer.

Turkey is not about to trade in NATO membership for an alliance with Russia. But Turkey’s reliability as a Western partner increasingly looks in doubt. Rumours abound that some of Mr Erdogan’s associates inside the ruling Justice and Development (AK) party favour reneging on some NATO commitments. The same goes for the army. The sweeping purges that followed July’s coup were ostensibly directed against followers of the Gulen movement, an Islamic sect suspected of leading the mutiny. But they have also claimed the careers of thousands of pro-Western officers, clearing the way for those more sympathetic to Russia.

Kerim Has of Moscow State University points to the growing influence inside the army of a group inspired by Dogu Perincek, an ultranationalist ideologue. Mr Perincek, who also heads a small political party, insists there is no room for any political divisions in the armed forces. But he rejoices that the purges have weakened Western influence. “A large share of America’s power in the military and the police has been crushed,” he gloats.

Authoritarian pillow talk

The anti-Western hysteria that swept through Turkey in the wake of the coup has dimmed slightly over the past couple of months, partly because of the hopes Mr Erdogan places in Donald Trump. Mr Erdogan and many in his government expect Mr Trump to extradite the presumed mastermind of July’s coup, Fethullah Gulen, and to sever links with the Kurdish YPG, which the Obama administration considered an effective force against IS, but which Turkey considers a terrorist group. Mike Pompeo, the CIA’s new chief, was in Ankara to discuss these issues on February 9th.

If Mr Trump disappoints, however, the relationship between Mr Putin and Mr Erdogan looks likely to get closer. Russia needs Turkey to speed up the political process in Syria by bringing anti-regime forces to the negotiating table. Turkey needs Russian tourists, gas supplies and help rebuilding ties with Mr Assad. But when another crisis strikes, Mr Putin will try to push the wedge between Turkey and NATO deeper. As a former Turkish president put it, “building relations with big states is like getting into bed with a bear.” When that bear is Russia, it is best to stay wide awake.