This is article 41 in the Big Data series. Introduces three Redis advanced data types: Bitmap, Geo, and Stream.
Complete illustrated version: CSDN Original | Juejin
Bitmap
Core Concept: Bitmap is a data structure using binary bits, each bit can only be 0 or 1, used to represent element value or state.
Space Efficiency
Bitmap’s biggest advantage is extremely high space efficiency. Store 10,000 users’ sign-in status:
- Traditional (boolean in Set): ~40KB
- Bitmap: ~1.25KB (saves over 97%)
Common Use Cases
- User sign-in/check-in
- Online user statistics
- Bloom filter
- Permission management
- Deduplication counting
Core Commands
| Command | Description |
|---|---|
SETBIT key offset value | Set bit value at specified offset (0 or 1) |
GETBIT key offset | Get bit value at specified offset |
BITCOUNT key [start end] | Count bits with value 1 |
BITOP operation destkey key [key ...] | Bitwise operations on multiple Bitmaps (AND/OR/XOR/NOT) |
Practical Example: User Sign-in
# User 1000 signs in on 2024-01-01
SETBIT user:sign:1000 20240101 1
# Check if user 1000 signed in on 2024-01-01
GETBIT user:sign:1000 20240101
# Count total sign-in days for user 1000
BITCOUNT user:sign:1000
Geo Type
Redis Geo type internally uses Sorted Set, combining three core technologies: Z-order curve, Base32 encoding, and GeoHash algorithm.
Z-Order Curve
Z-order curve is a space-filling curve that maps multi-dimensional data to one-dimensional while preserving spatial locality—points close in geographic location are also close in one-dimensional encoding.
Encoding Process (example: coordinate (3, 5)):
- Convert X=3, Y=5 to binary:
011,101 - Interleave bits (Y first then X):
011011 - Convert to decimal:
27
Base32 Encoding
Base32 uses 32 characters (0-9, b-z minus confusing characters) to convert binary data to printable ASCII:
- Split binary data into 5-bit groups
- Map each group (0-31) to Base32 alphabet
- Pad with
=if less than 5 bits at the end
GeoHash Algorithm
GeoHash encodes any location on Earth into a compact string. Redis internally uses 52-bit integer stored in Sorted Set’s score, achieving efficient nearby queries through z-score sorting.
Core Commands
# Add geographic location (longitude latitude member name)
GEOADD user:addr 111.11 44.44 ww 112.22 43.33 kk
# Get GeoHash encoding
GEOHASH user:addr ww
# Get coordinates
GEOPOS user:addr ww
# Calculate distance between two points (unit: km)
GEODIST user:addr ww kk km
# Find nearby members (Redis 6.2+)
GEOSEARCH user:addr FROMMEMBER ww BYRADIUS 100 km ASC
Stream Type
Stream is a data structure introduced in Redis 5.0, providing persistent message queue functionality, filling the gap that Pub/Sub cannot persist.
Main Capabilities
- Auto-generate message IDs (format:
millisecond timestamp-sequence number) - Message traversal and range query
- Blocking/non-blocking read
- Consumer Group support
- Pending message acknowledgment mechanism
- Queue monitoring and message backtracking
Core Commands
# Write message (* means auto-generate ID)
XADD topic:001 * name wzk age 18
# Read range messages (- means minimum ID, + means maximum ID)
XRANGE topic:001 - +
# Consume message (start from ID=0, read 1 message)
XREAD COUNT 1 STREAMS topic:001 0
# Create consumer group
XGROUP CREATE topic:001 group1 0
# Consumer group read
XREADGROUP GROUP group1 consumer1 COUNT 1 STREAMS topic:001 >
# Acknowledge message
XACK topic:001 group1 <message-id>
Difference from Pub/Sub
| Feature | Stream | Pub/Sub |
|---|---|---|
| Message Persistence | Supported | Not supported |
| Consumer Acknowledgment | Supported (ACK) | Not supported |
| Consumer Group | Supported | Not supported |
| Historical Messages | Can retrieve | Cannot retrieve |
Stream is suitable for scenarios requiring reliable message delivery, while Pub/Sub is more suitable for real-time broadcasting where message loss is acceptable.