What is @redis/bloom?
@redis/bloom is an npm package that provides probabilistic data structures for Redis, such as Bloom filters, Cuckoo filters, Count-Min Sketch, and Top-K. These data structures are useful for efficiently handling large sets of data with minimal memory usage and are often used in scenarios like caching, deduplication, and approximate counting.
What are @redis/bloom's main functionalities?
Bloom Filter
This example demonstrates how to create a Bloom filter, add an item to it, and check for the existence of an item. Bloom filters are used to test whether an element is a member of a set with a possible false positive rate.
const { createClient } = require('@redis/bloom');
async function bloomFilterExample() {
const client = createClient();
await client.connect();
await client.bf.reserve('myBloom', 0.01, 1000);
await client.bf.add('myBloom', 'item1');
const exists = await client.bf.exists('myBloom', 'item1');
console.log(exists); // true
await client.disconnect();
}
bloomFilterExample();
Cuckoo Filter
This example shows how to create a Cuckoo filter, add an item, and check for its existence. Cuckoo filters are similar to Bloom filters but support deletion of items.
const { createClient } = require('@redis/bloom');
async function cuckooFilterExample() {
const client = createClient();
await client.connect();
await client.cf.reserve('myCuckoo', 1000);
await client.cf.add('myCuckoo', 'item1');
const exists = await client.cf.exists('myCuckoo', 'item1');
console.log(exists); // true
await client.disconnect();
}
cuckooFilterExample();
Count-Min Sketch
This example demonstrates how to initialize a Count-Min Sketch, increment the count of an item, and query the count. Count-Min Sketches are used for approximate frequency counting.
const { createClient } = require('@redis/bloom');
async function countMinSketchExample() {
const client = createClient();
await client.connect();
await client.cms.initByDim('myCMS', 2000, 5);
await client.cms.incrBy('myCMS', 'item1', 5);
const count = await client.cms.query('myCMS', 'item1');
console.log(count); // 5
await client.disconnect();
}
countMinSketchExample();
Top-K
This example shows how to create a Top-K data structure, add items to it, and retrieve the top K items. Top-K is used to maintain a list of the most frequent items.
const { createClient } = require('@redis/bloom');
async function topKExample() {
const client = createClient();
await client.connect();
await client.topk.reserve('myTopK', 3);
await client.topk.add('myTopK', 'item1', 'item2', 'item3', 'item1');
const list = await client.topk.list('myTopK');
console.log(list); // ['item1', 'item2', 'item3']
await client.disconnect();
}
topKExample();
Other packages similar to @redis/bloom
bloom-filter
The 'bloom-filter' package provides a simple implementation of Bloom filters in JavaScript. It is less feature-rich compared to @redis/bloom and does not integrate with Redis, making it suitable for in-memory operations only.
cuckoo-filter
The 'cuckoo-filter' package offers a JavaScript implementation of Cuckoo filters. Similar to 'bloom-filter', it does not integrate with Redis and is intended for in-memory use cases.
count-min-sketch
The 'count-min-sketch' package provides a JavaScript implementation of the Count-Min Sketch data structure. It is useful for approximate frequency counting but lacks the Redis integration provided by @redis/bloom.
@redis/bloom
This package provides support for the RedisBloom module, which adds additional probabilistic data structures to Redis.
Should be used with redis
/@redis/client
.
:warning: To use these extra commands, your Redis server must have the RedisBloom module installed.
RedisBloom provides the following probabilistic data structures:
- Bloom Filter: for checking set membership with a high degree of certainty.
- Cuckoo Filter: for checking set membership with a high degree of certainty.
- T-Digest: for estimating the quantiles of a stream of data.
- Top-K: Maintain a list of k most frequently seen items.
- Count-Min Sketch: Determine the frequency of events in a stream.
For some examples, see bloom-filter.js
, cuckoo-filter.js
, count-min-sketch.js
and topk.js
in the examples folder.