Top seed and defending champion pushed all the way by in-form Medvedev in the Chinese city
Shanghai Masters: Roger Federer battles into third round as Juan Martin Del Potro shakes off illness to advance
Roger Federer grafted his way into the Shanghai Masters third round with a 6-4, 4-6, 6-4 win over in-form Daniil Medvedev on Wednesday.
Federer, the defending champion in Shanghai, initially wasted little time stamping his mark on the tournament, breaking the 22nd-ranked Medvedev in the first game.
But Medvedev, fresh from winning the Japan Open, broke back and enjoyed even more luck on Federer's service in the second set.
Federer's large cheering squad - decked out in matching red T-shirts - looked on in disbelief as Medvedev grabbed the second set.
Shanghai Masters: Djokovic advances as Cilic and Thiem suffer shock defeats
Shanghai Masters talking points: Djokovic continues pursuit of Nadal
Roger Federer: Kyrgios must improve his work ethic to fulfil 'potential'
The 22-year-old Russian then put the Swiss under immediate pressure on his serve in the deciding set, only for an uncharacteristically flustered Federer to hold.
In what was the first meeting between the pair, Federer broke in the ninth game to finally destroy Medvedev's resolve in just under two hours.
With the exception of injured world No 1 Rafael Nadal, Federer's chief rivals are all still in the mix.
Juan Martin del Potro and Alexander Zverev both defied illness to join Federer and Novak Djokovic in the next round.
Del Potro, the third seed, said that he is suffering from a virus and reviewing his health "hour by hour", after labouring past France's Richard Gasquet.
The 30-year-old Argentine lost Sunday's final of the China Open in Beijing to unseeded Nikoloz Basilashvili and afterwards revealed that he had been unwell.
Three days on and Del Potro continues to be under the weather, but did enough to see off Gasquet 7-5, 7-6.
"It wasn't easy to play for me today, I did what I can. I didn't run too much," Del Potro said between coughs at his post-match press conference.
"I got lucky that I won, but tomorrow will be a different day," added Del Potro, who plays 13th seed Borna Coric of Croatia next in Shanghai.
"If I have a good night today and I have a good recovery tomorrow morning, maybe I will feel a little bit better for tomorrow night and play better than today."
Zverev, 21, fought off a cold and the raw power of Basilashvili, winning 7-5, 6-4. The fourth seed will play Australia's world-ranked 33 Alex de Minaur.
No 2 seed Djokovic took his place in the last 16 on Tuesday with a convincing victory over Jeremy Chardy.
The Serb, who is in sparkling form having won Wimbledon and the US Open this summer, plays 16th seed Marco Cecchinato on Thursday, aiming to gain revenge against the Italian, who claimed a surprise victory in the French Open quarter-finals in June.