Quick revision
The short answer
We keep one geo key per city and vehicle type, such as geo:{nyc}:car:free, holding only available drivers, with busy drivers in a sibling key, and split very large cities into area cells so keys stay moderate and spread across cluster slots. Each location update is a pipelined GEOADD plus a ZADD into a last-seen sorted set, and a sweeper removes drivers not seen for 30 seconds, because members have no TTL. Riders search with GEOSEARCH BYRADIUS 3 km ASC COUNT 20, widening if needed, and a Lua script reserves the chosen driver atomically.
What to remember
In this answer, we designed driver locations as geo keys per city, vehicle type and, for big cities, zone, with hash tags so related keys share a slot and different cities spread across the cluster. Available and busy drivers sit in separate keys, updates are pipelined GEOADD and ZADD commands, and a sweeper script removes drivers not seen for 30 seconds because members have no TTL. Riders search with GEOSEARCH within 3 km, widening as needed, a Lua script reserves a driver atomically, several primaries carry 75,000 updates a second, and trip state stays in a durable database.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We keep one geo key per city and vehicle type, such as geo:{nyc}:car:free, holding only available drivers, with busy drivers in a sibling key, and split very large cities into area cells so keys stay moderate and spread across cluster slots. Each location update is a pipelined GEOADD plus a ZADD into a last-seen sorted set, and a sweeper removes drivers not seen for 30 seconds, because members have no TTL. Riders search with GEOSEARCH BYRADIUS 3 km ASC COUNT 20, widening if needed, and a Lua script reserves the chosen driver atomically.
Why this question is asked
Matching riders to nearby drivers is the heart of a ride-hailing product, and the location index is its hottest data: it changes 75,000 times a second and is read on every ride request. Interviewers ask this design to see whether we can turn a simple GEOSEARCH into a system that scales, forgets stale drivers, never offers a busy driver and survives node failures. In this answer, we will set out the requirements and numbers, design the keys, handle updates and staleness, describe search and reservation, and spread the load across a Redis Cluster, finishing with failure handling.
1Requirements and Rough Numbers
We have 300,000 active drivers, each sending a location every 4 seconds, which is 75,000 updates a second. Riders want the nearest available drivers within 3 km, quickly, say under 10 ms in Redis. A driver who stops sending, because the app was closed or the phone lost signal, should disappear from searches within about 30 seconds. A driver on a trip must never be offered to another rider.
Memory is not the problem: a sorted-set member costs on the order of 100 bytes, so 300,000 drivers take tens of megabytes. Write rate is the problem. Each update is at least two commands (position and last-seen), so 150,000 commands a second, plus searches. A single Redis primary can handle a lot with pipelining, but this load, with headroom for peaks, calls for several primaries, and that decides the key design.
2The Key Design
We split keys by city and vehicle type, for example geo:{nyc}:car:free and geo:{nyc}:car:busy. Splitting keeps each key moderate, so searches scan fewer members, and lets different cities live on different shards. The part in braces is a hash tag: Redis Cluster hashes only that part, so all keys for New York share one slot. That is deliberate, because the scripts below touch several of them at once, and a script may only use keys in one slot.
The cost of the hash tag is that a whole city lives on one shard. For most cities that is fine. For the largest, we split further by area cell, for example {nyc:7} for one of a dozen fixed zones, so one city spans several slots.
Available and busy drivers live in separate geo keys, so a rider's search never even sees a busy driver. A hash per driver, driver:4711, holds status, current key and details for the dispatch service.
| Key | Type | Holds |
|---|---|---|
| geo:{nyc}:car:free | geo (sorted set) | positions of available cars |
| geo:{nyc}:car:busy | geo (sorted set) | positions of cars on a trip |
| seen:{nyc} | sorted set | driver -> last update time in ms |
| driver:4711 | hash | status, city key, vehicle type |
3Location Updates and Stale Drivers
Drivers send locations to an ingestion service, not to Redis directly. It knows each driver's status, groups updates by city key and sends them pipelined, many commands per network round trip, which is what makes 75,000 updates a second affordable. Each update is a GEOADD to the free or busy key and a ZADD to the last-seen sorted set.
Geo members have no TTL, since expiry applies to whole keys. So we track freshness ourselves: seen:{nyc} scores each driver by the time of their last update. A sweeper runs every few seconds per city and removes drivers whose last update is older than 30 seconds, from the geo keys and from seen. It runs as a Lua script so that a driver who sends an update at the same moment is not removed by mistake between the read and the delete.
The LIMIT keeps each script run short, because Redis runs nothing else while a script executes.
4Search and Reservation
A ride request searches the free key around the rider: GEOSEARCH geo:{nyc}:car:free FROMLONLAT <lon> <lat> BYRADIUS 3 km ASC COUNT 20 WITHDIST. Because the key holds only available, recently seen drivers, the results are usable directly. If fewer than a handful come back, we widen to 5 km and then 8 km.
Then dispatch picks a driver, using distance, rating or estimated arrival, and must reserve them so that two riders cannot get the same driver. A Lua script does it atomically: if the driver is still in the free key, it copies the driver's geohash score to the busy key with ZADD, removes them from free, and returns 1; otherwise it returns 0 and dispatch tries the next candidate.
7 messages in order. Play them, or step through them one at a time.
5Spreading Load Across the Cluster
Because keys are tagged by city or zone, they spread across the 16,384 hash slots, and the cluster spreads slots across primaries. If New York is very busy, its zones land on different primaries. We size for peaks: if one primary comfortably handles, say, 50,000 pipelined commands a second with headroom, 150,000 writes plus searches need around four to six primaries, and we add more as cities grow. Searches can also go to replicas when slightly stale positions are acceptable, which they usually are for showing nearby cars on a map, but reservations always go to the primary.
6Failure Handling and Trade-Offs
If a primary fails, its replica is promoted and a few seconds of updates may be lost, because replication is asynchronous. We accept that: every driver reports again within 4 seconds, so the index heals itself. That is why positions belong in Redis, while trip state, who is driving whom and what they will pay, lives in a durable database. A reservation lost in failover is caught when dispatch writes the trip to the database with a unique constraint on the driver's active trip.
We trade some precision for speed throughout: positions up to 4 seconds old, a stale driver visible for up to 30 seconds, results that may miss a driver just across a zone boundary unless we query neighbours. All are acceptable for matching and are cheap to tune.
7Follow-up questions
How do you handle a driver crossing from one city key to another?
The ingestion service maps each location to its city or zone key, and the driver hash records the current key. When the mapped key changes, it adds the driver to the new key and removes them from the old one. The keys are in different slots, so this cannot be one script; for a moment the driver may be in both, which only means they could appear in two adjacent searches, and the reservation script, which checks the free key of the dispatching zone, still prevents double booking. The sweeper cleans any leftovers.
Why not store busy drivers in the same key and filter?
Filtering after the search wastes work: in a busy downtown many drivers are on trips, and the search would scan and return them only for the application to discard them. Separate keys make the search return only drivers we can offer, and moving a driver between keys is cheap because the geohash score can be copied.
8Conclusion
In this answer, we designed driver locations as geo keys per city, vehicle type and, for big cities, zone, with hash tags so related keys share a slot and different cities spread across the cluster. Available and busy drivers sit in separate keys, updates are pipelined GEOADD and ZADD commands, and a sweeper script removes drivers not seen for 30 seconds because members have no TTL. Riders search with GEOSEARCH within 3 km, widening as needed, a Lua script reserves a driver atomically, several primaries carry 75,000 updates a second, and trip state stays in a durable database.
Open with
I'd split the location keys by city and vehicle type, like geo:{nyc}:car:free, with the city in a hash tag so related keys share a slot and different cities spread over the cluster, and split the biggest cities into zones so no key or shard gets too hot.
Then, if they want more
Available and busy drivers go in separate geo keys, so searches only see drivers we can offer. An ingestion service receives the 75,000 updates a second and pipelines them: GEOADD to the driver's current key and ZADD to a last-seen sorted set with the timestamp.
Geo members have no TTL, so a sweeper script runs every few seconds, takes drivers last seen more than 30 seconds ago with ZRANGE BYSCORE, and removes them from the geo keys and the last-seen set atomically.
A ride request runs GEOSEARCH on the free key, BYRADIUS 3 km ASC COUNT 20 WITHDIST, and widens the radius if too few come back. Then a Lua script reserves the chosen driver: if they're still in the free key, it copies their score into busy and removes them from free, so two riders can't get the same driver.
For load, that's around 150,000 writes a second, so several primaries. On failover I accept losing a few seconds of positions, since drivers report again within 4 seconds, and keep trip state in a durable database.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the Redis data model for driver locations, including how stale drivers disappear...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/c7e9d94f10/