Sandi Verdev

Case study

Rewriting core search with 98% test coverage, then switching with a fallback

FirstBeatMedia · Full Stack PHP Engineer · Oct 2012 – Jun 2014

Problem

Member search is the busiest feature of a dating site. In 2012 the Sphinx search code was spread over several places, and results were cached in the user's session.

Replacing it in place could change what millions of members saw on 1,300+ sites, with no easy way back.

What I did

  • Started with one model for all Sphinx searches, with its unit tests.
  • Built the new search next to the old one instead of editing it in place, so both could run and be compared.
  • Covered the new code with unit tests up to 98% before it took any traffic.
  • Moved cached results from the session to Memcached, with tags so a profile change clears the cached results that include it.
  • Before the switch, went through the old search feature by feature and fixed the gaps. Kept the old one as a fallback for a while, then removed it.
  • The comparison also found that sorting by distance was wrong in the old search, so I fixed it in both.
  • Also capped the city autocomplete: it could request 10,000+ rows (seen in the internal error logs, never by users). Now it returns 150, without duplicates.

Result

Search rewrite timelineOctober 2012: one model for all Sphinx searches with unit tests. July 2013: results cached in Memcached with tag-based clearing. August 2013: new search at 98% test coverage, built next to the old one. April 2014: city lookup limited from 10,000+ rows to 150 results; new search checked against the old one and switched on, old one kept as a fallback. June 2014: old search code removed.Oct 2012One model for all Sphinx searches,with its unit testsJul 2013Search results cached in Memcached;tags clear them when a profile changesAug 2013New search at 98% test coverage,built next to the old oneApr 2014City lookup: 10,000+ rows requested→ 150 results, duplicates removedApr 2014Checked against the old search, gapsfixed, switched; old one kept as fallbackJun 2014Old search code removed
From the commit and ticket history.

Stack

  • PHP
  • Sphinx
  • Memcached
  • MySQL
  • PHPUnit