Quick revision
The short answer
The query is slow because GEOSEARCH scans every restaurant in the geohash cells covering a 10 km circle, which downtown is tens of thousands of members, computes each distance and sorts them all before COUNT 50 trims the result; on Redis's single thread that blocks everyone. We fix it by adding ANY when exact nearest order is not needed, starting with a 1 to 2 km radius and widening only if needed, splitting the national key by city or region, caching results per cell during peaks, and keeping filters like open-now in separate keys.
What to remember
In this answer, we saw that GEOSEARCH cost depends on how many members sit in the cells covering the area, so a 10 km search downtown scans tens of thousands of restaurants and sorts them before COUNT 50 trims the result, blocking Redis's single thread. Adding ANY stops the scan after enough matches, an expanding radius keeps dense areas cheap, splitting the key by city spreads size and load, caching per geohash cell absorbs lunchtime repeats, and separate keys per filter value avoid scanning items we would discard.
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.
The query is slow because GEOSEARCH scans every restaurant in the geohash cells covering a 10 km circle, which downtown is tens of thousands of members, computes each distance and sorts them all before COUNT 50 trims the result; on Redis's single thread that blocks everyone. We fix it by adding ANY when exact nearest order is not needed, starting with a 1 to 2 km radius and widening only if needed, splitting the national key by city or region, caching results per cell during peaks, and keeping filters like open-now in separate keys.
Why this question is asked
Geo searches are fast in demos and slow at lunchtime, and the reason is almost always the same: the work grows with how many points sit in the area, not with how many results we ask for. Because Redis runs commands on one main thread, a 40 ms query does not just slow one user; it delays every command behind it, which is the CPU spike. Interviewers ask this to check that we know how GEOSEARCH does its work and can reduce it in several independent ways. In this answer, we will see why COUNT 50 does not help, then apply the fixes one by one, from a one-word change to a different key layout.
1Why COUNT 50 Does Not Reduce the Work
GEOSEARCH chooses geohash cells about the size of the search area, the cell containing the centre and its eight neighbours, and reads every member in them from the sorted set. For a 10 km radius, those nine cells cover a region considerably larger than the circle, and in a dense downtown they can hold 30,000 or more of the country's 2 million restaurants. For each of them Redis computes the distance, keeps the ones inside 10 km, sorts them all by distance because we asked for ASC, and only then returns the first 50.
So the cost is set by the number of members scanned, N, and the sort of the matches, not by COUNT. In the suburbs the same command scans a few hundred members and takes a fraction of a millisecond, which is why the problem appears only downtown at lunch. And at hundreds of requests per second, 40 ms each, the single thread is saturated.
SLOWLOG confirms it: the slow entries are all GEOSEARCH calls from dense areas, each taking about 40,000 microseconds.
2Fix 1: Add ANY When Exact Order Is Not Required
With COUNT 50 ANY, Redis stops scanning as soon as it has found 50 members inside the radius, instead of collecting all of them. In a dense area that is often after a few hundred members, so the search becomes dramatically cheaper. The trade-off is that the 50 returned are not guaranteed to be the closest 50, only 50 within 10 km; ASC then sorts just those 50.
For a 'restaurants near you' list, where the user scrolls and the app ranks by many factors anyway, 50 restaurants within 10 km is often good enough. For 'the nearest pharmacy', it is not, and we use the next fix instead.
3Fix 2: Start Small and Widen
A user downtown has hundreds of restaurants within 1 km. So we search 1 or 2 km first, and only if fewer than 50 come back do we search again at 5 km, then 10 km. Dense areas finish at the first, cheap step, and sparse areas, where every search is cheap anyway, still get complete results. The results remain correctly ordered by distance, because each step is a full search of a smaller area.
- Search 1.5 km, COUNT 50cheap downtown
- 50 or more results?YesReturn themNoWiden the radius
- Search 5 km, COUNT 50suburbs
- 50 or more results?YesReturn themNoSearch 10 km and return what exists
4 steps in order. Play them, or step through them one at a time.
4Fix 3: Split the Key and Cache Hot Cells
One key holding 2 million restaurants also concentrates all traffic on one shard and one thread, because a key lives in one hash slot. Splitting by city or region, such as geo:restaurants:nyc and geo:restaurants:chicago, makes each key smaller, lets us spread keys across shards so lunchtime in one city does not slow another, and makes the data easier to rebuild. A search near a city border queries two keys and merges the results, which the application can do cheaply.
During lunch peaks, many users stand in the same few blocks. We can cache the results for a small geohash cell, for example a precision-6 cell of about 1.2 km by 0.6 km, for 30 to 60 seconds. Every user in that cell gets the cached list, and the application computes exact distances for that user from the cached coordinates. Restaurants rarely move, so a one-minute cache is harmless.
5Fix 4: Keep Filters Out of the Hot Path
If the app filters by open-now or cuisine after the geo search, Redis does all the scanning and the application throws most of it away. It is better to search a key that only contains what we want. We keep separate geo keys per filter value, such as geo:restaurants:nyc:open and geo:restaurants:nyc:cuisine:thai, and move members between them as their state changes.
Moving a member between geo keys is cheap because the geohash score can be copied: ZSCORE from one key and ZADD the same score into another, or ZREM to remove it. A small scheduler runs at each opening and closing time and does that for the affected restaurants.
6Checking the Result
After the changes, we compare SLOWLOG and the p99 latency of the restaurants endpoint at lunchtime, and Redis CPU per shard. A healthy result is GEOSEARCH below 1 ms in all areas and no geo commands in SLOWLOG. We also check result quality with ANY by comparing, offline, the average distance of returned restaurants with and without it, to make sure users still see nearby places.
| Change | What it reduces | Trade-off |
|---|---|---|
| COUNT 50 ANY | members scanned | not guaranteed the nearest 50 |
| Start at 1.5 km, widen | area scanned in dense places | extra calls in sparse places |
| Key per city or region | key size, one-shard hotspot | merge results near borders |
| Cache per geohash cell | repeated identical searches | results up to 60 s old |
| Key per filter value | wasted scans for filtered items | keep keys in sync on changes |
7Follow-up questions
How would you serve open-now efficiently as opening hours change?
Keep a separate geo key per city for open restaurants. A scheduler holds each restaurant's opening and closing times in a sorted set scored by the next change time; every minute it takes the due changes and, for each restaurant, copies its geohash score from the main key with ZSCORE into the open key with ZADD, or removes it with ZREM. The search then queries only the open key, so closed restaurants are never scanned.
Would more Redis replicas help?
They would spread read load, since geo searches can run on replicas, and that is a reasonable short-term step. But each query would still cost 40 ms, so latency would not improve, and replicas add a little staleness. Reducing the work per query fixes the cause.
Is ANY with ASC contradictory?
No. ANY changes when the scan stops; ASC sorts whatever was collected. Together they return 50 restaurants within the radius, sorted by distance among themselves, but not necessarily the 50 nearest in the whole area.
8Conclusion
In this answer, we saw that GEOSEARCH cost depends on how many members sit in the cells covering the area, so a 10 km search downtown scans tens of thousands of restaurants and sorts them before COUNT 50 trims the result, blocking Redis's single thread. Adding ANY stops the scan after enough matches, an expanding radius keeps dense areas cheap, splitting the key by city spreads size and load, caching per geohash cell absorbs lunchtime repeats, and separate keys per filter value avoid scanning items we would discard.
Open with
GEOSEARCH does work in proportion to the members in the geohash cells it scans, not the COUNT. For a 10 km radius it reads the centre cell and its eight neighbours, which downtown can be tens of thousands of restaurants from the 2 million in that single key, computes every distance, sorts all the matches because of ASC, and only then takes 50.
Then, if they want more
That's the 40 ms, and since Redis runs commands on one thread, it delays everything else, hence the CPU spike. First, if we don't need the exact nearest 50, I'd add ANY, so COUNT 50 ANY stops scanning once it has 50 matches; that's typically well under a millisecond, though the results aren't guaranteed to be the closest.
Where nearest order matters, I'd start with a 1 to 2 km radius and widen only if fewer than 50 come back, so dense areas stay cheap and sparse areas stay complete.
Then I'd split the national key by city or region, which shrinks each search and spreads keys across shards, and cache results per small geohash cell for 30 to 60 seconds during lunch, since restaurants don't move.
Finally, filters like open-now or cuisine shouldn't be applied after a big geo search; I'd keep separate geo keys per filter value and move members between them with ZSCORE and ZADD as their state changes.
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 "Why is the query slow, and how do you fix it?": https://scrivio-interview-library.netlify.app/databases/redis-patterns/16c47c819f/