Hi! Today I’d like to talk a bit about a feature in Redis called bitmaps and how we can leverage that to build simple and efficient analytics. Say we have a web app and we’d like to measure user retention, that is “how often our users return to us”? One of the ways to measure that is to see the probability of a user staying with us two months in a row. So we can take the users that visited us in April see if they visited us in May and compute the retention percentage How can we design a system that supports that? One of the ways is to store for every user whether they have logged in this month. Whenever the user signs in we do an UPSERT to the login_stats table and then we’ll run a query to take all the users who have logged in both in April and May and count them. Another, much simpler way is to use Redis bitmaps If you haven’t use Redis, it is an in-memory data structure store it’s like a dictionary or a hash map that is stored completely in memory. In your infrastructure Redis runs as a separate service alongside your database and like your database it has a command-line interface that you can use to interact with it. With the command-line interface you can execute commands that read and manipulate the keys within Redis. The values for those keys have types and some commands are applicable only to certain types For example, you can use the SET command to set a certain key’s value as a string or you can use the LPUSH command to push some values onto a list There are also libraries in your favorite programming language and their API mostly resembles the command-line interface so we won’t cover them in this video. Back to bitmaps . Bitmaps are a data type used within Redis and represent a long list of bits that contain is 0 by default and we can use the SETBIT command to flip a certain bit to 1 or to 0. Nothing spectacular here. However a bitmap is very efficient and quite large. It can store up to 2 to power 32 bits which is about 4 billion items. That’s enough to store some event data for all of our users! So getting back to our initial example: How do we measure the user retention? Well we can create a bitmap for the month of April and when the user with ID is six signs in then we’ll run the SETBIT command and set the bit corresponding to their ID to 1. When another user signs in we set their ID bit to 1. To find out how many unique users did we have an April we can use the BITCOUNT command and pass it the key of our bitmap. If we run the same process for the month of May we get another bitmap. And when we want to find out which users signed in both in April and May we can use the BITOP command to issue a bitwise AND storing the result in a different bitmap and passing the bitmaps that we want to AND. Then we can compare the two counts and measure a monthly retention rate at a very small performance cost. We can extend this approach to cover more generic questions like “what percentage of users bought a product after they open our weekly newsletter”? Redis provides us with a simple way to measure these things without introducing additional load our main database and while keeping the code simple and easy to change. Thanks for watching!