It was his first meeting with the 21-year-old who is being touted as one of the next stars of the circuit.
Federer – commonly lauded as the greatest of all time – broke the Russian at 5-4 in the first set and served out for one set lead after 33 minutes.
The Swiss engineered a break to lead 5-4 in the second. But instead of wrapping up the match, was broken. He was nearly made to pay for his profligacy when Khachanov held two set points on Federer’s serve.
But the former champion battled back to hold serve and won the ensuing tiebreak seven points to five.
“I was in a defensive position too much,” said Federer after the 85 minute match. “We hadn’t played before so it was difficult to know where he would put the ball.”
Federer is looking for a fourth title this year after hoisting his 18th Grand Slam trophy at the Australian Open in January. He followed up that triumph with the “sunshine double” of titles in Indian Wells and Miami.