Geospatial Data
Redis geospatial commands (GEOADD, GEOSEARCH, GEODIST) store lat/lon as sorted-set members scored by geohash — Uber-style driver dispatch queries nearby entities in milliseconds…
Introduction
Redis geospatial commands (GEOADD, GEOSEARCH, GEODIST) store lat/lon as sorted-set members scored by geohash — Uber-style driver dispatch queries nearby entities in milliseconds without scanning every pin in the city.
Under the hood GEO is a ZSET; Redis 7+ standardizes on GEOSEARCH (replacing legacy deprecated GEORADIUS) with geohash indexing for radius or bounding-box search. Distances are returned in m, km, mi, or ft with WITHDIST and WITHCOORD options.
App-side haversine over millions of points fails at scale — push location indexing to Redis or a dedicated geo service.
Understanding the topic
Key concepts
- GEOADD key lon lat member — add location (note lon before lat).
- GEOSEARCH key FROMLONLAT lon lat BYRADIUS radius unit — Redis 7+ unified nearby search (replaces deprecated GEORADIUS).
- GEOSEARCH ... BYBOX width height unit — bounding-box viewport search.
- GEODIST — distance between two members.
- Stored as ZSET — scores are geohash-encoded.
- GEOPOS — retrieve coordinates of members.
flowchart LR Mobile -->|lat lon| GEOADD GEOADD --> ZSet[Geohash ZSET] Dispatch -->|GEOSEARCH| ZSet ZSet -->|driver IDs| Matcher
Step-by-step explanation
- Lat/lon converted to 52-bit geohash score in ZSET.
- Radius search checks geohash cells overlapping circle.
- Results sorted by distance when requested.
- Updates via GEOADD replace member position.
- TTL applies to entire geo key like any key.
Syntax reference
Common commands
- Longitude first — common mistake is lat/lon swap.
- GEOSEARCH COUNT 10 ASC — return nearest 10 results.
- Shard keys by city/region.
GEOADD drivers:nyc -73.985 40.758 driver:42GEOSEARCH drivers:nyc FROMLONLAT -73.985 40.758 BYRADIUS 2 km ASC COUNT 10 WITHDIST WITHCOORDGEODIST drivers:nyc driver:42 driver:99 km
Informative example
Update driver location and find nearest five in Spring (Redis 7+ GEOSEARCH):
@Servicepublic class DriverGeo {private final StringRedisTemplate redis;public DriverGeo(StringRedisTemplate redis) {this.redis = redis;}public void updateLocation(String driverId, double lon, double lat) {redis.opsForGeo().add("drivers:sf", new Point(lon, lat), driverId);}public GeoResults<RedisGeoCommands.GeoLocation<String>> nearby(double lon, double lat) {return redis.opsForGeo().search("drivers:sf",GeoReference.fromCoordinate(lon, lat),new Distance(3, Metrics.KILOMETERS),RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().sortAscending().limit(5));}}
Expire stale drivers with separate TTL key or periodic ZREM. Active pins only — not historical trail (use streams for that).
Real-world use
Real-world use cases
- Ride-share driver matching — GEOSEARCH BYRADIUS 3 km.
- Store locator — nearest branches.
- Delivery fleet ETA input — distance sort.
- IoT asset tracking — last known position.
- Geofence entry — periodic radius check.
Best practices
- Partition by metro: drivers:{cityCode}.
- Remove inactive drivers — ZREM or key TTL.
- Limit GEOSEARCH COUNT to cap work.
- Validate coordinates before GEOADD.
- Use read replicas for heavy read radius traffic.
- Combine with Kafka for location event pipeline.
Common mistakes
- Lat/lon argument order swapped.
- Single global key for all drivers worldwide.
- No cleanup of offline drivers — stale results.
- Storing full trip history in geo key.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionGEOADD argument order?+
Answer
2BeginnerQuestionWhat structure backs Redis GEO?+
Answer
3IntermediateQuestionGEOSEARCH vs app-side loop?+
Answer
4IntermediateQuestionScale geo to 1M active drivers?+
Answer
5AdvancedQuestionDesign Uber dispatch cache?+
Answer
Summary
GEO = ZSET + geohash for radius search.