In-Depth Explanation of Redis Specialized Data Structures: Geospatial, HyperLogLog, and Bitmap

Geospatial Indexing

Redis models geospatial data using a sorted set under the hood. Each entry pairs a location label with its longitude and latitude, scored by spatial distance. Internally, positions are encoded via the GeoHash algorithm; the resulting hash becomes the member of the sorted set, while the distance metric serves as the score. Range queries for nearby points rely on matching GeoHash prefixes and radius constraints.

Example in Java:

Jedis client = new Jedis("127.0.0.1");
client.geoadd("cities", 121.4737, 31.2304, "Shanghai");
client.geoadd("cities", 104.0668, 30.5728, "Chengdu");
List<GeoRadiusResponse> nearby = client.georadius("cities", 121.4737, 31.2304, 300, GeoUnit.KILOMETERS);
for (GeoRadiusResponse item : nearby) {
    System.out.println(item.getMemberByString());
}

Approximate Cardinality with HyperLogLog

HyperLogLog provdies memory-efficient estimation of distinct element counts. Redis implements it using sparse representation and probabilistic counting. Each incoming item is hashed; the position of the first non-zero bit in the hash tail is tracked across regisetrs. Aggregating these positions yields an estimate that converges toward the true cardinality as more item are processed.

Example in Java:

Jedis conn = new Jedis("127.0.0.1");
conn.pfadd("visitors", "uid42", "uid73", "uid19");
conn.pfadd("visitors", "uid42", "uid88");
long uniqueEst = conn.pfcount("visitors");
System.out.println("Estimated unique visitors: " + uniqueEst);

Compact Boolean State Tracking with Bitmap

A bitmap stores a sequence of bits inside a Redis string, each representing a boolean flag. Bitwise operations enable setting, clearing, or testing individual bits with minimal overhead. This structure excels in scenarios requiring compact storage for massive on/off state tracking, such as feature flags or activity logs.

Example in Java:

Jedis store = new Jedis("127.0.0.1");
store.setbit("flags", 256, true);
bool status = store.getbit("flags", 256);
System.out.println("Flag at offset 256: " + status);

Tags: Redis geospatial HyperLogLog Bitmap Data Structures

Posted on Wed, 28 Oct 2026 16:36:37 +0000 by xangelo