SD System Design In Depth
0%
design system-designsearchspelling

Design: did-you-mean and spell correction

Module 5.4: Information Retrieval & Search Engines Updated Sep 17, 2026
📄 Cheat Sheet
The Algorithm Behind Spell Checkers
b001 The Algorithm Behind Spell Checkers Watch on YouTube ↗

Beautifully animated explanation of edit distance and candidate generation, the foundation of did-you-mean.

0:00 / --:--

Under the Hood: Real-Time Spelling Correction

Approximately 10% to 15% of all web and e-commerce search queries contain typos (e.g. "appple iphone", "teh lord of the rings"). When a query produces zero or poor results, the search engine must instantaneously evaluate edit distance candidates and suggest corrections.

The SymSpell Algorithm (Symmetric Delete Spelling Correction)

flowchart TD Dict["Dictionary Word: 'apple'"] --> Precompute["Precompute Deletes (Distance 1 & 2)"] Precompute --> HashLookup["In-Memory Hash Table: 'pple' -> 'apple', 'aple' -> 'apple'"] UserTypo["User Input: 'appple'"] --> GenDeletes["Generate Deletes of Input: 'apple', 'ppple'"] GenDeletes --> HashLookup HashLookup --> Match["Hash lookup per delete variant, then edit-distance check: Correct to 'apple'"]

Under the Hood: SymSpell vs Traditional Levenshtein Distance

  • Standard Levenshtein Distance: Calculating Levenshtein matrix distance between an input typo and every word in a 500,000-term dictionary requires billions of operations, taking seconds per query.
  • SymSpell Breakthrough: Rather than testing insertions, deletions, substitutions, and transpositions against the entire vocabulary, SymSpell precomputes all K-distance deletions for dictionary words and stores them in a hash table. At query time, only deletions of the misspelled input are looked up in the hash table, achieving sub-millisecond execution. The original benchmark (Wolfgarbe, 2012) reports up to 1,000x speedup vs. standard Levenshtein on a 500K dictionary, but actual speedup depends on edit distance threshold and dictionary size.
  • The Noisy Channel Model: Scores candidate corrections using Bayesian probability: P(Word | Typo) ∝ P(Typo | Word) × P(Word), balancing keyboard typo proximity with unigram/bigram word frequencies from search logs.

Worked example: noisy channel scoring

User types teh. Candidates within edit distance 1 or one transposition include "the", "ten", "tea". Illustrative probabilities:

CandidateP(word) from query logsP(teh given word), error modelProduct
the0.050.001, adjacent transposition5 x 10 to the minus 5
ten0.00050.0002, h for n substitution1 x 10 to the minus 7
tea0.00030.0001, h for a substitution3 x 10 to the minus 8

"The" wins by more than two orders of magnitude. A real system also includes the typed word itself as a candidate, so correctly spelled rare words are not "fixed".

Worked example: SymSpell index size

With maximum edit distance 2, a word of length 8 has up to 8 single deletes and 28 double deletes, 36 variants. For a 500,000-word dictionary, that is on the order of 10 to 20 million delete-keys after deduplication, which is why SymSpell trades memory for speed. At query time, the typo's own deletes are looked up, and every hit is verified with a true Damerau-Levenshtein distance before scoring.

Candidate generation trade-offs

MethodQuery speedMemoryNotes
Brute-force edit distanceVery slow on large vocabulariesMinimalFine for tiny dictionaries
BK-treeModerateLowPrunes using the triangle inequality
Levenshtein automaton over an FSTFastLowWhat Lucene fuzzy queries use
SymSpell symmetric deleteVery fastHighPrecomputed deletes in a hash map
Neural or LLM correctionSlowestModel sizeBest for context-heavy errors; cache results

Production pattern: run correction only when the original query returns few or low-quality results, or when a correction has much higher probability; mine correction pairs from query reformulations in logs (a user types "recieve", then immediately "receive" and clicks), which is how large engines learn domain-specific corrections.

Failure modes

Over-correction is worse than no correction: replacing a valid product code or surname destroys trust. Guard rails include never correcting tokens that match catalogue entities, requiring the corrected query to return clearly more or better results, and measuring the rate at which users click "search instead for" the original, which directly measures bad corrections.

Check yourself Question 1 of 3
Symmetric delete recall
From: Under the Hood: SymSpell vs Traditional Levenshtein Distance

What does SymSpell precompute to make spelling correction fast?

Key Takeaways

SymSpell achieves sub-millisecond spell correction by precomputing word deletions into an in-memory hash table.
The Noisy Channel Model combines keyboard typo likelihood with language unigram frequency to pick the best correction.
Spelling correction runs transparently during query understanding to rescue zero-result typo queries.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue