Scrivio Interview library
ConceptFoundations7 min read · updated 13 November 2026

Redis geospatial commands store points by longitude and latitude and answer radius and box searches, such as finding stores within 5 km of a user.

How are geo points stored internally, which commands add and search them, and what are the limits on accuracy and coordinates?

GEOADD stores each point as a member of an ordinary sorted set whose score is a 52-bit geohash, an interleaving of longitude and latitude bits, so nearby points usually have close scores. GEOSEARCH (Redis 6.2+) finds members within a radius or a box around coordinates or another member, replacing the deprecated GEORADIUS commands, and costs O(N + log M) where N is the number of members in the cells it scans. Distances assume a spherical Earth and can be off by up to about 0.5 percent, latitudes are limited to about plus or minus 85.05 degrees, and there is no per-member TTL or polygon search.

Why this question is asked

'Find the stores within 5 km' sounds like it needs a spatial database, but Redis answers it in well under a millisecond with a structure we already know. Interviewers ask about the internals because they explain the behaviour we see in production: why some searches are slower than others, why coordinates come back slightly changed, and why the poles are off limits. In this answer, we will see how a geohash turns two coordinates into one number, add and search points, understand the cost of a search, and go through the limits on accuracy, coordinates and features.

1How Two Coordinates Become One Score

A sorted set orders members by a single number, but a location has two: longitude and latitude. A geohash combines them. Redis splits the longitude range from -180 to 180 in half and records one bit for which half the point is in, does the same for latitude, then halves again within the chosen half, and so on, 26 times for each. Interleaving the 26 longitude bits and 26 latitude bits gives a 52-bit number, small enough to be stored exactly in the sorted set's double-precision score.

Each extra pair of bits shrinks the cell by half in each direction, so the 52-bit value pins a point to a cell well under a metre across. And points that share a long prefix of bits are in the same larger cell, so nearby points tend to have close scores. 'Tend to' matters: two points on either side of a cell boundary can be a metre apart and have very different scores, which is why searches look at neighbouring cells too.

2Adding Points and Seeing What Is Stored

GEOADD takes longitude first, then latitude, then the member name. Because the key is a sorted set, every sorted-set command works on it too.

Text
GEOADD stores -0.1276 51.5072 store:charing-cross -0.0754 51.5055 store:tower-hill # (integer) 2
TYPE stores # zset
ZSCORE stores store:charing-cross # "2163557740185296": the 52-bit geohash
GEOPOS stores store:charing-cross # 1) "-0.12760102748870850" 2) "51.50720076722284"
GEOHASH stores store:charing-cross # 1) "gcpvj0e5mt0" standard 11-character form
GEODIST stores store:charing-cross store:tower-hill km # "3.6203" (approximately)
ZREM stores store:tower-hill # (integer) 1: removing a point is plain ZREM

Notice that GEOPOS returns coordinates slightly different from what we stored: they are decoded from the 52-bit cell, so they are accurate to well under a metre but not identical. If exact original coordinates matter, we store them separately, for example in a hash. The score and GEOHASH values above are shown for illustration; the exact digits depend on the input.

3Searching by Radius or Box

GEOSEARCH, available since Redis 6.2, is the single search command. The centre is either coordinates (FROMLONLAT) or an existing member (FROMMEMBER), and the shape is a circle (BYRADIUS) or a rectangle (BYBOX). ASC sorts by distance, COUNT limits the results, and WITHDIST and WITHCOORD add details. GEOSEARCHSTORE writes the results into another key. The older GEORADIUS and GEORADIUSBYMEMBER still work but are deprecated.

Text
GEOSEARCH stores FROMLONLAT -0.12 51.50 BYRADIUS 5 km ASC COUNT 20 WITHDIST
# 1) 1) "store:charing-cross" 2) "0.9583"
# 2) 1) "store:tower-hill" 2) "3.1607"
GEOSEARCH stores FROMMEMBER store:charing-cross BYBOX 4 2 km ASC # 4 km wide, 2 km tall

Results come back nearest first because of ASC. Without it, the order is whatever the scan produced.

4What a Search Costs

Redis does not compare the centre with every member. It picks a geohash precision whose cells are about the size of the search area, finds the cell containing the centre and its eight neighbours, and for each of those nine cells reads the range of scores that cell covers, which is a range query on the sorted set costing O(log M) to find, where M is the size of the key. Then it computes the real distance for every member found in those cells, drops those outside the shape, sorts if asked, and applies COUNT.

So the cost is O(N + log M), where N is the number of members inside the cells scanned. A 5 km search in a dense city centre may scan tens of thousands of members; the same search in the countryside scans a handful. That is why big radii and dense areas are slow, and why COUNT alone does not make a search cheap: it trims the result after the scan.

Step throughInside GEOSEARCH
  1. Choose a cell size from the radiusgeohash precision about the search size
  2. Centre cell and 8 neighbours9 ranges of scores
  3. Range-read members in each cellO(log M) to find each range
  4. Compute distance for each memberN members scanned
  5. Drop members outside the shaperadius or box
  6. Sort (ASC) and apply COUNTCOUNT trims after the scan

6 steps in order. Play them, or step through them one at a time.

5Limits on Accuracy, Coordinates and Features

Distances are computed with the haversine formula on a perfect sphere, while the Earth is slightly flattened, so distances can be off by up to about 0.5 percent. Over 5 km that is up to 25 metres, which is fine for 'stores near me' and not fine for surveying.

Coordinates are limited to the range used by common web maps: longitude from -180 to 180 and latitude from about -85.05112878 to 85.05112878. GEOADD rejects points closer to the poles, so polar research stations need a different tool.

Finally, the geo commands are a thin layer on a sorted set. There is no TTL per member, because expiry works on whole keys, so stale points, such as a driver who went offline, must be removed by our own code. And there are no polygon searches in the core geo commands: delivery zones shaped like a neighbourhood need another tool, such as the query engine's geo shape fields in recent Redis versions (check your version) or a spatial database.

6Follow-up questions

How would you store the results of a search for reuse?

With GEOSEARCHSTORE, which runs the same search and writes the results into a destination key, as another geo sorted set by default or, with STOREDIST, as a sorted set scored by distance. We then give that key a short TTL, for example 30 seconds, and serve repeated requests for the same area from it. The destination must be in the same slot as the source in Cluster, so we use a shared hash tag.

Why does GEOADD take longitude before latitude?

Because it follows the x, y order used by the geohash and many geographic formats such as GeoJSON. Passing them the other way round is a common bug: for most cities the swapped pair is still valid, so the point silently lands somewhere else, often in the ocean.

Can I search across several geo keys at once?

Not in one command. We search each key and merge the results in the application, or keep one key per area and choose the key from the user's location. That is also how we keep each key, and each search, smaller.

7Conclusion

In this answer, we saw that GEOADD stores points in a sorted set scored by a 52-bit geohash that interleaves longitude and latitude bits, so nearby points tend to have close scores and ZREM and other sorted-set commands still work. GEOSEARCH searches by radius or box around coordinates or a member, replacing the deprecated GEORADIUS commands, and costs O(N + log M) because it scans the members of the nine cells around the area. Distances assume a sphere and can be off by about 0.5 percent, latitudes stop at about 85.05 degrees, and there is no per-member TTL or polygon search.

What to say in the interviewAbout 90 seconds aloud

Open with

Redis stores geo points in an ordinary sorted set. GEOADD takes longitude, latitude and a member, and the score is a 52-bit geohash: it interleaves 26 bits of longitude and 26 bits of latitude, each bit halving the range, so nearby points usually have close scores.

Then, if they want more

Because it's a sorted set, ZREM removes a point and other sorted-set commands work, and GEOPOS gives back coordinates decoded from the cell, accurate to well under a metre but not identical. For searching I use GEOSEARCH, from Redis 6.2, for example GEOSEARCH stores FROMLONLAT with a longitude and latitude, BYRADIUS 5 km, ASC, COUNT 20, WITHDIST.

It can also search from a member with FROMMEMBER or by a rectangle with BYBOX, and GEOSEARCHSTORE saves results. GEORADIUS and GEORADIUSBYMEMBER are deprecated.

Internally it picks a cell size matching the radius, range-reads the centre cell and its eight neighbours, computes distances and filters, so the cost is O(N plus log M), where N is the members in those cells, which is why dense areas and big radii are slower.

The limits: distances assume a spherical Earth, up to about 0.5 percent off; latitude is limited to about plus or minus 85.05 degrees, so no poles; there's no TTL per member, and no polygon search in the core commands.

About this answer

Last updated
13 November 2026
Versions it names
Redis 6
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 "How are geo points stored internally, which commands add and search them, and what are...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/80fdafe5fb/