Redis Autocomplete Search
Autocomplete shows suggestions while a visitor types. After "red" appears in the search box, the site offers "redis", "redis cluster", and "redis tutorial." Redis can serve these suggestions in under a millisecond by using a Sorted Set with lexicographic range queries.
The Dictionary Analogy
A dictionary lists words in alphabetical order. To find every word that begins with "red," you open the page where "red" starts and read forward until the words no longer begin with "red." A Sorted Set with equal scores keeps words in alphabetical order, and Redis performs that same page flip for you.
Sorted alphabetically ┌───────────────────┐ │ rate limiting │ │ read replica │ │ redis │ ◄─┐ │ redis cluster │ │ all begin with "red" │ redis tutorial │ ◄─┘ │ replication │ └───────────────────┘
Step 1: Store the Phrases
Give every member the same score, usually 0. Redis then orders members by their text.
127.0.0.1:6379> ZADD suggest 0 "redis" 0 "redis cluster" 0 "redis tutorial" 0 "read replica" 0 "replication" 0 "rate limiting" (integer) 6
Step 2: Query by Prefix with ZRANGEBYLEX
ZRANGEBYLEX returns members between two text boundaries. A square bracket includes the boundary, and a round bracket excludes it.
127.0.0.1:6379> ZRANGEBYLEX suggest "[red" "[red\xff" LIMIT 0 5 1) "redis" 2) "redis cluster" 3) "redis tutorial"
The boundary "[red" starts at the first member that begins with red. The boundary "[red\xff" ends just after the last member that starts with red, because the byte \xff sorts after ordinary letters. LIMIT 0 5 caps the answer at five suggestions.
Rules for Clean Matching
- Convert every phrase to lowercase before storing and before searching.
- Trim extra spaces.
- Choose one language and script per key, or keep separate keys for each language.
Ranking Suggestions by Popularity
Alphabetical order serves a small catalog. Search boxes work better when popular phrases appear first. Store counts in a second Sorted Set, and let the app combine the results.
Option A: Rank within a prefix
For each prefix of a searched phrase ("r", "re", "red", "redi", "redis"):
ZINCRBY suggest:red 1 "redis tutorial"
Query the top suggestions for "red":
ZRANGE suggest:red 0 4 REV WITHSCORES
Option B: Lookup then sort Step 1: ZRANGEBYLEX suggest "[red" "[red\xff" LIMIT 0 50 → up to 50 candidates Step 2: ZMSCORE popularity (those candidates) → their counts Step 3: The app sorts candidates by count and shows the top 5
Option A uses more memory because each phrase appears under several prefixes. Option B stores each phrase once and sorts a short list in application code.
Recording a Search
127.0.0.1:6379> ZINCRBY popularity 1 "redis tutorial" "41"
Every completed search raises the phrase count by one.
Keeping Suggestions Fresh
- Run a nightly job that rebuilds the suggestion key from your product or article titles.
- Remove banned or outdated phrases with ZREM.
- Apply a decay step, such as halving all counts weekly, so new trends rise.
When to Choose Redis Search Instead
Sorted Set autocomplete matches the start of a phrase. Visitors often type a word from the middle, such as "tutorial" for "redis tutorial." The Redis Query Engine supports prefix search on words inside text fields and fuzzy matching.
FT.SEARCH idx:courses "@title:red*" LIMIT 0 5
Practical Example – Course Search Box
Visitor types: r → re → red Each keystroke sends the text to the server. Server runs: ZRANGEBYLEX suggest "[red" "[red\xff" LIMIT 0 5 Browser shows: redis / redis cluster / redis tutorial The server waits 150 ms after the last keystroke before it queries, so fast typing does not flood Redis with calls.
Key Points
- A Sorted Set with equal scores keeps phrases in alphabetical order.
- ZRANGEBYLEX with a prefix and \xff returns every phrase that starts with that prefix.
- Popularity counts in a second key rank the suggestions.
- Lowercase every phrase for consistent matches.
- Choose Redis Search for matches inside words and for typo tolerance.
