Tracking daily active users (DAU) with Redis

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

We’re going to discuss how to track this metric, in a language agnostic fashion.
The concepts discussed and Redis commands utilized can be implemented 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 interacting with
the site. This could be an integer if you’re using auto-incrementing keys in a
RDBMS or a string like an email address or UUID.

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 an unordered collection of Strings.

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.

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"

If 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"

Which 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"

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

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.