Understanding the Differences Between Redis Bitmap and Bloom Filter
This article explains how Redis Bitmap stores binary states using bit arrays, details its commands and use cases, then introduces Bloom Filter's probabilistic set membership, compares their advantages, limitations, and typical scenarios such as cache penetration protection and blacklist filtering.
Bitmap
Redis Bitmap is a special data type built on top of the String type. It stores a sequence of bits, each bit corresponding to an offset, which makes it extremely space‑efficient for binary states (0/1).
Example: a quiz game where each user has a key answer:user:X. The question number is used as the offset; a correct answer sets the bit to 1, an incorrect answer leaves it at 0. To determine whether a user answered all 7 questions, the BITCOUNT command counts bits set to 1 and the result is compared with 7.
Operations
SETBIT: set a bit at a given offset (O(1)).
SETBIT answer:user:1 7 1 GETBIT: retrieve the bit value at an offset.
GETBIT answer:user:1 7 BITCOUNT: count bits set to 1 in a key.
BITCOUNT answer:user:1 BITOP: perform bitwise operations (AND, OR, NOT, XOR) on one or more bitmaps and store the result in a new key.
BITOP AND resultbitmap answer:user:1 answer:user:2Advantages
Very high space efficiency – e.g., 100 million bits occupy only about 12 MB.
Fast O(1) read/write and bit‑wise operations.
Simple API for setting, getting, counting, and logical operations.
Disadvantages
Only represents two states (0/1); unsuitable for multi‑state data.
When data is sparse, many unused bits are allocated, leading to wasted space unless a compressed representation such as Roaring Bitmap is used.
Application scenarios
User sign‑in status (continuous days).
Online‑user activity tracking.
Questionnaire or quiz answer recording.
Bloom Filter
When a bitmap is used to record element existence via a hash‑derived offset, hash collisions cause false positives. A Bloom filter mitigates this by mapping each element to multiple bits using several independent hash functions. If all mapped bits are 1, the element may exist; if any is 0, the element is definitely absent.
Operations
BF.RESERVE: create a Bloom filter with a specified error rate and capacity.
BF.RESERVE myfilter 0.000001 999999 BF.INFO: retrieve filter metadata.
BF.INFO myfilter BF.ADD/ BF.MADD: add a single element or multiple elements.
BF.ADD myfilter hello
BF.MADD myfilter item1 item2 BF.EXISTS/ BF.MEXISTS: test membership of one or many elements.
BF.EXISTS myfilter hello
BF.MEXISTS myfilter item1 item2Advantages
Extremely low memory footprint; only bits are stored.
Stable performance: insertion and query are O(k), where k is the number of hash functions, independent of set size.
Disadvantages
False‑positive rate is non‑zero; a negative result is always correct.
Deletion is difficult because clearing a bit may affect other elements that share the same position.
Application scenarios
Preventing cache penetration in Redis (e.g., checking non‑existent flash‑sale items).
Email blacklist filtering for spam detection.
Crawler URL deduplication.
Other large‑scale membership‑test use cases.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer XiaoFu
xiaofucode.com – a programmer learning guide driven by the pursuit of profit
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
