iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Geospatial

Redis stores points (longitude, latitude, member) in a sorted set under the hood, encoded with a 52-bit geohash. That gives you O(log N) radius queries with no extra infrastructure. It is the right pick for delivery-radius checks, store locators, and proximity ranking — anything where the working set fits in memory.

Add points and run radius and nearest queries

EXAMPLE
# Add: longitude first, then latitude (a gotcha)
GEOADD couriers \
  151.2093 -33.8688 'sydney-cbd' \
  151.0830 -33.8467 'parramatta' \
  150.7710 -33.7480 'penrith' \
  144.9631 -37.8136 'melbourne-cbd' \
  153.0260 -27.4705 'brisbane-cbd'

# All couriers within 50 km of a Sydney customer pin, with distance
GEOSEARCH couriers \
  FROMLONLAT 151.2110 -33.8650 \
  BYRADIUS 50 km \
  ASC COUNT 10 \
  WITHCOORD WITHDIST WITHHASH

# Bounding-box query (faster than radius for rectangles)
GEOSEARCH couriers \
  FROMLONLAT 151.0 -34.0 \
  BYBOX 200 200 km \
  ASC

# Use an existing member as the centre
GEOSEARCH couriers \
  FROMMEMBER 'sydney-cbd' \
  BYRADIUS 30 km ASC

# Distance between two members
GEODIST couriers 'sydney-cbd' 'parramatta' km

# Store the result of a search in another key (for paging or further filtering)
GEOSEARCHSTORE nearby_sydney couriers \
  FROMLONLAT 151.2110 -33.8650 \
  BYRADIUS 50 km ASC COUNT 100

# Drop a courier when they go offline
ZREM couriers 'parramatta'

# In code (node-redis):
# const { client } = require('redis').createClient();
# const hits = await client.geoSearch('couriers',
#   { longitude: 151.211, latitude: -33.865 },
#   { radius: 50, unit: 'km' },
#   { SORT: 'ASC', COUNT: 10, WITHCOORD: true, WITHDIST: true });

Why it matters

Member identity matters: GEOADD updates the position of an existing member, it does not create a duplicate. Use a stable ID (driver UUID, store ID) as the member name and never the human label, otherwise a courier "Alice S" and "Alice Smith" become two ghost pins on your map.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
GEOADD shops 151.21 -33.87 "sydney" -0.13 51.51 "london"
GEODIST shops sydney london km   # ~16983
GEOSEARCH shops FROMLONLAT 151 -33 BYRADIUS 200 km ASC
Try it Yourself »

Discussion

Loading…