How to create Bloom filters in Redis

A Bloom filter is a space-efficient probabilistic data structure used to test whether a given element is a member of a set. Redis, being a key-value store, doesn't have native support for Bloom filters.

canvasAnimation-image

Here, we can implement Bloom filters in Redis using commands like BF.ADD, BF.MADD, and BF.MEXISTS. These commands are described below:

Implementing the BF.ADD command

Let’s suppose we want to create a Bloom filter denoted as myfilter. To start off, we will use the BF.ADD command to create a Bloom filter with the name myfilter.

We need to add a couple of items to make this Bloom filter functional. Therefore, we’ll add two items item1 and item2 one after the other with the following commands:

BF.ADD myfilter item1
BF.ADD myfilter item2
Adding item1 and item2 to the Bloom filter myfilter

The output should return a boolean index 1, indicating that the addition of these items to the Bloom filter was successful.

Confirming that the addition of item1 and item2 was successful
Confirming that the addition of item1 and item2 was successful

Adding multiple items with BF.MADD

For adding multiple items in one command, we can use the BF.MADD command:

BF.MADD myfilter item1 item2
Adding multiple items in one command

Running this in the terminal should also return a boolean index 1 as the output for both items:

Confirming that the addition of item1 and item2 in one command was successful
Confirming that the addition of item1 and item2 in one command was successful

Note: When we try to add an item that is already in the filter using BF.MADD, it will return the boolean index 0 because it cannot be added again.

Checking for item membership

To verify whether these items are inside the Bloom filter myfilter, we can use the BF.EXISTS command with the name of the item we want to check:

BF.EXISTS myfilter item1
Checking if item1 exists in myfilter

Note: In the place of item1, we can write the name of any item we want to check for membership.

In this case, the output should be a boolean index 1, confirming that item1 is a member of the Bloom filter myfilter:

item1 existing in 'myfilter'
item1 existing in 'myfilter'

On the other hand, if an item (say item3) does not exist in the Bloom filter, a boolean index 0 will be returned in the output.

item3 not existing in 'myfilter'
item3 not existing in 'myfilter'

Verifying the membership of multiple items with BF.MEXISTS

Similar to the case where we added multiple items under one command, we can also verify their membership in one command via the BF.MEXISTS command:

BF.MEXISTS myfilter item1 item2 item3
Using BF.MEXISTS to verify the presence of multiple items in 'myfilter'

The output in the terminal will also return a boolean index 1 for an existing item in the Bloom filter and 0 otherwise:

Verifying the membership of multiple items in 'myfilter'
Verifying the membership of multiple items in 'myfilter'

Try it yourself

These commands can be tested with the terminal widget below. Have fun exploring!

Terminal 1
Terminal
Loading...

Conclusion

In conclusion, Redis provides a powerful and efficient implementation of Bloom filters through the RedisBloom module. By leveraging commands like BF.ADD, BF.MADD, and BF.MEXISTS, developers can easily create and manage Bloom filters for membership tests. Make sure to check out the Redis command documentation for further usage information.

Copyright ©2024 Educative, Inc. All rights reserved