Bitmap, HyperLogLog, and Geospatial

Introduction

Redis goes far beyond strings and lists. This chapter covers three specialized data types that solve surprisingly common problems with tiny memory footprints: Bitmaps for compact true/false tracking, HyperLogLog for approximate unique counts, and Geospatial indexes for location-based queries. None of them are everyday tools, but when you need them, nothing else is as simple or as fast.

Prerequisites

  • Familiarity with Redis Strings, Sets, and Sorted Sets (see Basic Data Types)
  • A running Redis server and redis-cli

Bitmap

A Bitmap is not a separate Redis data type. It is a String manipulated at the bit level. You can think of it as a very long array of zeros and ones, where each position costs exactly one bit. A single 512 MB String can store over 4 billion bits.

Setting and Reading Bits

bash
# Set bit 7 to 1 (returns the previous value at that position)
SETBIT user:42:signin 7 1
# (integer) 0
 
# Check if the user signed in on day 7
GETBIT user:42:signin 7
# (integer) 1
 
# Check day 3 (never set, defaults to 0)
GETBIT user:42:signin 3
# (integer) 0

Bitmaps are perfect for tracking daily habits. If you map calendar days to bit offsets, one small key records an entire year of attendance in just 46 bytes.

bash
# Mark attendance for days 0, 15, and 30
SETBIT attendance:2026 0 1
SETBIT attendance:2026 15 1
SETBIT attendance:2026 30 1
 
# Count how many days were marked
BITCOUNT attendance:2026
# (integer) 3

Bitwise Operations

You can run AND, OR, XOR, and NOT across multiple Bitmaps and store the result in a new key. This is useful for combining filters or calculating overlapping user segments.

bash
# Populate two Bitmaps
SETBIT campaign:email 1001 1
SETBIT campaign:email 1002 1
SETBIT campaign:push 1002 1
SETBIT campaign:push 1003 1
 
# Find users who received both email AND push
BITOP AND campaign:both campaign:email campaign:push
 
# Count the overlap
BITCOUNT campaign:both
# (integer) 1  (user 1002)

Finding Set Bits

bash
# Find the first bit set to 1
BITPOS user:42:signin 1
 
# Find the first bit set to 0
BITPOS user:42:signin 0

Tip

Bitmap vs Set Memory

Storing one million user IDs in a Set costs roughly 50 MB. Storing the same million as bits in a Bitmap costs about 125 KB. The trade-off is that Bitmaps only work when your IDs are compact integers. Sparse or random UUIDs waste space.

HyperLogLog

HyperLogLog is a probabilistic data structure that estimates the number of unique elements (cardinality) in a dataset. It uses a fixed 12 KB of memory regardless of how many items you add, and the standard error is about 0.81%. If you can tolerate a small margin of error, HyperLogLog replaces heavyweight deduplication Sets.

Adding and Counting

bash
# Add visitor IDs to a HyperLogLog
PFADD page:home:visitors "user_a"
PFADD page:home:visitors "user_b"
PFADD page:home:visitors "user_c"
 
# Add multiple items in one call
PFADD page:home:visitors "user_d" "user_e" "user_a"
 
# Estimate unique visitors
PFCOUNT page:home:visitors
# (integer) 5

Notice that "user_a" was added twice, but PFCOUNT still returns 5. HyperLogLog handles duplicates internally.

Merging Multiple HyperLogLogs

bash
# Track visitors for different pages
PFADD page:about:visitors "user_a" "user_f"
 
# Merge two pages into a combined estimate
PFMERGE site:total:visitors page:home:visitors page:about:visitors
 
PFCOUNT site:total:visitors
# (integer) 6

Warning

No Membership Test

HyperLogLog can tell you how many unique items exist, but not which items. If you need to check whether a specific user has visited, use a Set or a Bitmap instead.

When to Choose HyperLogLog

ScenarioStructureReason
Exact user list neededSetYou can enumerate members
Only a count, millions of usersHyperLogLog12 KB vs hundreds of megabytes
Daily UV with weekly rollupHyperLogLogPFMERGE combines days instantly

Geospatial

Redis Geospatial indexes store locations (latitude and longitude) inside a Sorted Set. You add coordinates as members and query by radius or bounding box, all with the performance you expect from Redis.

Adding Coordinates

bash
# Add cities with their longitude and latitude
GEOADD cities -74.0060 40.7128 "New York"
GEOADD cities 139.6917 35.6895 "Tokyo"
GEOADD cities -0.1278 51.5074 "London"
GEOADD cities 2.3522 48.8566 "Paris"

The command format is GEOADD key longitude latitude member. Longitude comes first, which is the opposite of casual "lat, lon" speech. Mixing the order is the most common beginner mistake.

Distance and Position

bash
# Distance between New York and London (default: meters)
GEODIST cities "New York" "London"
# "5570642.4547"
 
# Same distance in kilometers
GEODIST cities "New York" "London" km
# "5570.6425"
 
# Get the raw coordinates back
GEOPOS cities "Tokyo"
# 1) 1) "139.69171136617660522"
#    2) "35.68948797349564572"

Radius Queries

The WITHDIST flag includes distance in the result. You can also add WITHCOORD for coordinates and WITHHASH for the raw geohash, or COUNT 5 to limit results.

Tip

Real-World Pattern: Nearest Drivers

Store driver locations in a Geospatial key. When a rider requests a trip, run GEORADIUS with a small radius (for example, 3 km), then expand the radius if no drivers are found. Update driver positions with GEOADD periodically or on every GPS heartbeat.

Geohash

Redis exposes the internal geohash string for debugging or external use:

bash
GEOHASH cities "New York"
# 1) "dr5regw3pp"

Geohashes are hierarchical: longer strings represent smaller areas. This makes them compatible with other geospatial systems and URL-friendly location identifiers.

FAQ

How many bits can a single Bitmap hold?

A Redis String caps at 512 MB, which is about 4.2 billion bits. In practice, memory and use-case constraints kick in long before that theoretical limit.

Can I use negative offsets with SETBIT?

No. Offsets must be non-negative integers. If your IDs are large or sparse, consider whether a Set is more appropriate than a Bitmap.

Is HyperLogLog count guaranteed to be exact?

No. The estimate has a standard error of roughly 0.81%. For most analytics dashboards, that is invisible. For financial audits, use a precise counter instead.

Does Geospatial work across the international date line or poles?

Redis Geospatial uses the haversine formula on a sphere. It handles antimeridian crossings fine, but be cautious near the poles where longitude lines converge and distance calculations become less intuitive.

Can I update a member's coordinates without removing it first?

Yes. GEOADD overwrites the position of an existing member automatically. There is no need to run ZREM beforehand.

Are Geospatial commands available on Redis Cloud or managed providers?

Yes. Geospatial is part of the core Redis command set since version 3.2. Any standard Redis instance supports it.