Databases 15 min read

In‑Depth Analysis of Redis GEOADD and GEORADIUS Commands and Their Algorithmic Complexity

This article explains how Redis implements geospatial indexing with GEOADD and GEORADIUS commands, walks through their source‑code implementations, describes the underlying geohash calculations and nine‑cell grid search strategy, and derives the O(N + log M) time complexity for nearby‑people queries.

Architecture Digest
Architecture Digest
Architecture Digest
In‑Depth Analysis of Redis GEOADD and GEORADIUS Commands and Their Algorithmic Complexity

Redis provides geospatial indexing using six commands—GEOADD, GEOPOS, GEODIST, GEOHASH, GEORADIUS, and GEORADIUSBYMEMBER—allowing developers to build high‑performance “nearby people” features.

GEOADD adds a member (latitude, longitude, name) to a sorted set by converting the coordinates into a 52‑bit geohash score and invoking ZADD. The source code in geo.c validates arguments, extracts coordinates, encodes them with geohashEncodeWGS84 , creates score objects, and finally calls zaddCommand to store the member.

GEORADIUS (and its variant GEORADIUSBYMEMBER) queries members within a given radius around a point. The command parses optional flags such as WITHDIST, WITHCOORD, WITHHASH, ASC/DESC, COUNT, STORE, and STOREREDIST, then computes the search area by converting the radius to a geohash bounding box and selecting the nine surrounding geohash cells.

The core functions geohashGetAreasByRadiusWGS84 , membersOfAllNeighbors , and geoGetPointsInRange locate candidate members by performing range scans on the underlying sorted‑set skip‑list (or ziplist), followed by precise spherical distance checks using geohashGetDistanceIfInRadiusWGS84 .

Using a nine‑cell grid avoids boundary edge cases and dramatically reduces the number of elements examined, because each cell’s geohash range is contiguous and can be scanned efficiently. The overall time complexity of a radius query is O(N + log M), where N is the number of results returned and M is the number of elements examined in the nine cells, yielding very high performance in memory‑resident Redis.

performancealgorithmdatabaseRedisgeospatialgeoaddgeoradius
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.