Tracking daily active users (DAU) with Redis

Daily active users, also known as DAU, is the number of unique users that have
visited a website or used an application in a given day.

We’re going to discuss how to track this metric, in a cross-language fashion. The concepts discussed and Redis commands used can be coded in the language of your choosing using the Redis commands and/or libraries of your choosing as well.

To track DAU, you will need to have a unique ID for every user on the site. This could be an integer if you’re using automatically incrementing keys in a RDBMS or a string like an email address or UUID.

Luckily, Redis has a few different data types which can be used to store a list of items. Because we want a unique list of items, a Set would be an ideal choice, A Redis Set by definition is a collection of Strings without any order.

Tracking unique users for the current day

Since the order of our data doesn’t matter if all we’re looking for is a total number of users for a given day, so a Set is sufficient instead of say a Sorted Set which would allow us to have a scored order to our data.

Then to group the data by a specific date, we can use the date as part of the Set’s key. Somewhere in our code, perhaps at time of login if users must log in daily, or as part of some authorization middleware, we will want to add the user’s unique ID to our Set for the day:

redis> SADD dau:todaysDate "uniqueID"
redis-cli

Let’s say we had 5 different users log into the site, each with their own UUID, the following commands would end up being executed:

redis> SADD dau:todaysDate "17d083d4-965f-4192-8233-0b7b9be8a8ab"
redis> SADD dau:todaysDate "c833e724-d9dd-4456-9df1-c41866ba19eb"
redis> SADD dau:todaysDate "a8adb1f7-a11e-485e-b213-ba97750f3177"
redis> SADD dau:todaysDate "2c3ef653-6a20-4524-834a-1c9260d858b1"
redis> SADD dau:todaysDate "e0e801e0-23f0-4fb6-9f9f-5dfe4b654c1b"
redis-cli

Now that will give us a Redis Set that looks like this:

redis> SMEMBERS dau:todaysDate
1) "e0e801e0-23f0-4fb6-9f9f-5dfe4b654c1b"
2) "2c3ef653-6a20-4524-834a-1c9260d858b1"
3) "a8adb1f7-a11e-485e-b213-ba97750f3177"
4) "c833e724-d9dd-4456-9df1-c41866ba19eb"
5) "17d083d4-965f-4192-8233-0b7b9be8a8ab"
redis-cli

Getting the total number of users for the day

Great, but what if we just want to know the DAU for the day? If we just want
that count, we can use the SCARD command to return the Set’s cardinality:

redis> SCARD dau:todaysDate
(integer) 5
redis-cli

That’s great just for the current day, but what if we want to grab the last 30
days worth of DAU values? Unfortunately Redis doesn’t support passing in
multiple keys to the SCARD command, but you could use Redis Pipelining to run multiple SCARD commands for the different dates of data you’re interested in.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.