Learn more about Redis bloom filter commands. Our Redis Support team is here to help you with your questions and concerns.
Redis Bloom Filter Commands | An Introduction
Did you know that the Bloom Filter in Redis is an extension module for Redis? Like an in-memory data structure store.
It lets us check if an element exists in a set in a very small memory space that is of fixed size.
Furthermore, they test whether a given element is a member of a set. Although they may generate false positives, but never false negatives. But one out of every N positive answers will be wrong.
The smart move is to set up some rules, like checking IP ranges, before looking in the cache. If bad addresses keep getting checked, it is a good idea to save them in Redis with no value.
If we have a ton of invalid keys, our experts recommend using a Bloom filter.
In other words, the Bloom filter is implemented as a data type that can be added to a Redis server. It provides commands to create, manipulate, and query Bloom filters within the Redis database.
Rather than storing all elements in the set, Bloom Filters stores only the elements’ hashed representation. This is why Bloom Filters are fast and space-efficient.
- Dealing with Incoming Requests Using a Bloom Filter
- RedisBloom setup in a Docker container
- Build RedisBloom on our machine
- Exploring RedisBloom Commands
Dealing with Incoming Requests Using a Bloom Filter
- Storing All Valid Keys Upfront
- Add all valid keys to the Bloom Filter.
- When a request comes in, check the Bloom filter.
- If found in the Bloom Filter, it’s likely a valid key. Try fetching it from the database. If not in the database, it was a false positive.
- If not found in the Bloom Filter, it’s an invalid key.
- Storing Valid Keys On-the-Fly
- When a request is received, search the Bloom filter.
- If found in the Bloom filter, it’s likely a key seen before. Try fetching it from the database. If not in the database, it was a false positive.
- If not found in the Bloom filter, it might be a new valid key. Check and, if valid, add to the Bloom filter.
- Storing Invalid Keys
- When a request is received, search the Bloom filter.
- If found in the Bloom filter, it’s likely an invalid key. Note: it might be a valid key, but we’ll ignore it (a trade-off).
- If not found in the Bloom filter, it could be either a valid key or a new invalid key. Check and, if invalid, add to the Bloom filter.
RedisBloom setup in a Docker container
To easily test RedisBloom, start a Docker instance with the following command:
docker run -p 6379:6379 -it --rm redis/redis-stack-server:latest
Alternatively, we can opt to build RedisBloom on our machine as seen in the next section.
Build RedisBloom on our machine
- To begin with, make sure we have Redis installed.
- Then, Now, clone the RedisBloom repository and build it:
apt-get install -y git
cd ~/Redis
git clone --recursive https://github.com/RedisBloom/RedisBloom.git
cd RedisBloom
./sbin/setup
bash -l
make
To use a specific version of RedisBloom, e.g., 2.4.5, we can add -b v2.4.5 to the git clone command above.
- Now, exit bash.
- Next, edit the Redis configuration file:
apt-get install -y vim
cd ~/Redis/redis-stable
vim redis.conf - Then, run the following command and copy the full path of the RedisBloom executable:
make run -n
- Next, make sure Redis loads when started by adding the RedisBloom module to redis.conf.
apt-get install -y vim
cd ~/Redis/redis-stable
vim redis.conf
- Add the following line under the MODULES section (use the full path copied above):
loadmodule /root/Redis/RedisBloom/bin/linux-x64-release/redisbloom.so
- Save and exit vim.
Now, start Redis in the background and open the Redis CLI:
cd ~/Redis/redis-stable
redis-server redis.conf &
redis-cli
After setting up RedisBloom, we can interact with it using the Redis CLI.
- Create a new Bloom filter and add an item:
# 126.0.0.1:6468> BF.ADD newFilter foo
(integer) 1
- Check if an item exists in the filter:
# 127.0.0.1:6379> BF.EXISTS newFilter foo
(integer) 1
Here, a value of 1 indicates that ‘foo’ is likely in the set. However, remember that Bloom filters may produce false positives.
# 127.0.0.1:6379> BF.EXISTS newFilter car
(integer) 0
In the above example, the 0 indicates that ‘car’ is not in the set.
Exploring RedisBloom commands
RedisBloom comes with a set of powerful commands that boost its functionality. Let’s take a look at the essential commands and their usage.
- BF.ADD
Here is the syntax:
BF.ADD key item
This command adds an item to a Bloom filter.
For example:
redis> BF.ADD bf cat1
(integer) 1
redis> BF.ADD bf cat1
(integer) 0
- BF.CARD
Here is the syntax:
BF.CARD key
It returns the cardinality of a Bloom filter. It indicates the number of unique items added.
For example:
redis> BF.ADD bf1 item_foo
(integer) 1
redis> BF.CARD bf1
(integer) 1
redis> BF.CARD bf_new
(integer) 0
- BF.EXISTS
Here is the syntax:
BF.EXISTS key item
It determines if a given item was added to a Bloom filter.For example:
redis> BF.ADD bf item1
(integer) 1
redis> BF.EXISTS bf item1
(integer) 1
redis> BF.EXISTS bf item2
(integer) 0
- BF.INFO
Here is the syntax:
BF.INFO key [CAPACITY | SIZE | FILTERS | ITEMS | EXPANSION]
It returns information about a Bloom filter with details like capacity, size, number of filters, items inserted, and expansion rate.
For example:
redis> BF.ADD bf1 observation1
(integer) 1
redis> BF.INFO bf1
1) Capacity
2) (integer) 100
3) Size
4) (integer) 240
5) Number of filters
6) (integer) 1
7) Number of items inserted
8) (integer) 1
9) Expansion rate
10) (integer) 2
redis> BF.INFO bf1 CAPACITY
(integer) 100 - BF.INSERT
Here is the syntax:
BF.INSERT key [CAPACITY capacity] [ERROR error]
[EXPANSION expansion] [NOCREATE] [NONSCALING] ITEMS item [item ...]
It creates a new Bloom filter or adds items to an existing one. It allows specification of error rate, capacity, and expansion.
For example, we can add items to a filter, and create the filter with default parameters if it doesn’t exist:
BF.INSERT filter ITEMS foo bar baz
- BF.LOADCHUNK & BF.SCANDUMP
Here is the syntax:
BF.LOADCHUNK key iterator data
BF.SCANDUMP key iterator
BF.LOADCHUNK restores a Bloom filter previously saved using BF.SCANDUMP. It’s helpful for large filters that can’t fit into the DUMP and RESTORE models.
For example, to save and load a Bloom filter:
redis> BF.RESERVE bf 0.1 10
OK
redis> BF.ADD bf item1
(integer) 1
redis> BF.SCANDUMP bf 0
(integer) 1
"\x01\x00\x00..."
redis> BF.LOADCHUNK bf 1 "\x01\x00\x00..."
OK
RedisBloom offers several commands to help with flexibility and efficiency when working with Bloom filters.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
In brief, our Support Experts introduced us to Redis bloom filter commands.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments