Redis Large Key & Multi-Key Splitting: 4 Practical Patterns for Big Values, Massive Collections, and Bloom Filters
The article details four Redis splitting strategies: sharding oversized single-key values via multiGet or hash fields, bucket-sharding large hash/set/zset/list collections using client-side hash modulo, consolidating hundreds of millions of string keys into hash structures to cut memory overhead, and partitioning large bitmaps or Bloom filters into independent shards while preserving false-positive rates.
Reading font
Background
Redis runs on a single thread, so operations on very large values or massive key counts can block the event loop and degrade latency. The article presents four common large-key/multi-key scenarios and practical splitting solutions.
1. Single Key with a Very Large Value
Two sub-cases:
Full read/write each time: Split the object into several key–value pairs and use MGET / MSET to spread I/O pressure across multiple Redis instances.
Partial access: Either split into multiple keys as above, or store the object in a single hash where each field represents an attribute. Use HGET / HMGET for reads and HSET / HMSET for partial updates.
2. Collections with Too Many Elements (Hash, Set, ZSet, List)
Apply client-side sharding with a fixed bucket count (e.g., 10,000). For a hash, the original HGET(hashKey, field) becomes:
bucket = hash(field) % 10000
newHashKey = hashKey + bucket
HGET(newHashKey, field)The same pattern works for sets, sorted sets, and lists. Caveat: operations that require global ordering (e.g., LPOP returning the earliest pushed element) need extra metadata or time-based key partitioning.
3. Cluster Holding Hundreds of Millions of Keys
Excessive key counts waste memory in two ways:
Per-key overhead (each key carries a category prefix).
Cluster-mode slot-to-key mapping pointers (significant in Redis ≤3.2; improved in 4.0).
Solution: convert many string keys into fewer hash structures.
Case A – Strongly Related Keys (Same Entity)
Original keys:
user.zhangsan-id = 123
user.zhangsan-age = 18
user.zhangsan-country = chinaConsolidated into one hash:
key = user.zhangsan
field:id = 123
field:age = 18
field:country = chinaCase B – Unrelated Keys
Pre-estimate total keys (e.g., 200 million), choose a target fields-per-hash (e.g., 100), yielding 2 million buckets (2M keys ≈ a few GB vs. ~20 GB for 200M strings). For each original key user.123456789:
bucket = hash(123456789) % 2000000 // ensure positive hash
HSET(bucketKey, originalKey, value)Read with HGET(bucketKey, originalKey). Important: handle negative hash values correctly; keep fields per hash ≤ 512 (≈100 recommended).
4. Large Bitmap or Bloom Filter Splitting
Example: a 512 MB Bloom filter for user-ID matching. Split into 1,024 independent 512 KB bitmaps. Critical rule: each key must map to exactly one shard. Do not treat the shards as a single logical bitmap where one key touches multiple shards—that would force multi-node lookups and kill performance.
Correct approach: hash the key to pick one shard, then operate on that shard only.
False-positive rate: Bloom filter error rate ≈ (1 - e^(-k*n/m))^k where k = hash functions, n = elements, m = bitmap bits. If sharding distributes elements uniformly, n/m per shard stays constant, so the error rate is unchanged. (Derivation: Wikipedia Bloom_filter .)
Client convenience: Redis ≥ 2.3.4 provides SETBIT / GETBIT (or SETBITS / GETBITS) for multi-bit operations on a single key.
Recommended defaults: k = 13 hash functions; keep each Bloom filter ≤ 512 KB.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.
Published
AI-assisted summary