
I love designing, building, and managing reproducible, testable, and evolvable ML powered software.
Search for a command to run...

I love designing, building, and managing reproducible, testable, and evolvable ML powered software.
No comments yet. Be the first to comment.
Welcome to the second part of our blog series on gRPC using Python. In our previous blog, we talked about the basics of gRPC, its architecture, and its various components. In this blog, we will be going through how to write a gRPC client & server so ...

gRPC is a high-performance, open-source universal RPC (Remote Procedure Call) framework. It is developed by Google and provides efficient, low-latency communication between microservices. gRPC uses Protocol Buffers as the default data serialization f...

No not chemistry, this is about database systems.

Introduction Ever heard of a song and spent a few seconds remembering its name? Well, we made a game out of it 😎. Introducing Vinyl, a multiplayer song guessing game in which the first one (three people actually) to guess the name of the song from ...

Let's say you are building a sign-up service for an application. You need to ensure that the user doesn't choose an username that has already been taken by someone else.
You might think of a solution which looks like this:
/username?radioactive11radioactive11 already existsWhile this approach will work perfectly, it is very slow. Considering the popularity of a sign-up endpoint, this approach is highly unoptimised as you will be doing several read requests to the database for just one write request.
A better solution would be to introduce a caching layer, using some fast database like Redis. Here is what this solution will look like.
/username?radioactive11However, this approach too has it's problems. First, we increased our memory usage. Secondly, we need to ensure that the data in the cache is up to date with out primary database and not stale.
By definition, a bloom filter is a probabilistic data structure which tells us whether an element might be in a database or definitely isn't. They are only prone to false-positives, that is, searching for an element that does not exists might return incorrect results.
We take a Bit Vector of size m with all the bits set to false by default. in this example, let's take a vector with size m = 10.
Next, we need k efficient hash functions. In this example, let's take 3 hash functions, HF1, HF2 & HF3.
In order to insert an element in bloom filter, we hash it using multiple hash functions.
For example, we want to add radioactive11 using the 3 hash functions. Using our arbitrary hash functions, we get the following values:
HF1("radioactive11") = 1988HF2("radioactive11") = 2022HF3("radioactive11") = 2001Now we take mod(m) (10 in this case) of the values obtained by each hash function. We get
Now we set 1st, 2nd and 9th bit of our vector to True.

If we want to verify the existence of an item in the database, we hash it using the same hash functions and calculate the mod. If all the k indices in the vector are already set to true, the item might be already present in the database.
However, if all the k bits are not set to true, the item is definitely not in the database.
Redis provides a module called RedisBloom which provides an easy to use, scalable Bloom Filter.
You can run a local instance of Redis Stack using Docker by following these steps.
Once you have Redis Stack running, you can connect to Redis using the `redis-cli.
redis-cli
127.0.0.1:6379> PING
PONG
BF.ADD command. This will create a small bloom filter suitable for small number of items.127.0.0.1:6379> BF.ADD bloom taylorswift
(integer) 1
127.0.0.1:6379> BF.ADD bloom arianagrande
(integer) 1
127.0.0.1:6379> BF.ADD bloom hardwell
(integer) 1
127.0.0.1:6379> BF.ADD bloom avicii
(integer) 1
BF.EXISTS. If the output is 1, the element might exist. If the output it 0, the element definitely doesn't exist.127.0.0.1:6379> BF.EXISTS bloom selenagomez
(integer) 0
127.0.0.1:6379> BF.EXISTS bloom taylorswift
(integer) 1
127.0.0.1:6379> BF.EXISTS bloom edsheeran
(integer) 0
127.0.0.1:6379> BF.EXISTS bloom arianagrande
(integer) 1
Bloom filters are used in a variety of applications. Some of them are: