Development

Redis HyperLogLog

A HyperLogLog in Redis refers to an algorithm that allows you to count the number of unique items in a set without incurring significant memory usage.
Captain Salem 1 min read
Redis HyperLogLog

It works closely similar to a Redis filter bloom but with a different implementation.

There are three main commands when working with Redis HyperLogLogs. These commands include:

  1. PFADD command
  2. PFCOUNT command
  3. PFMERGE command.

Let us take an example.

PFADD Command

Assume we have a database that holds the type of databases that we support. We can add each entry to the HyperLogLog as:

127.0.0.1:6379> PFADD databases MySQL
(integer) 1
127.0.0.1:6379> PFADD databases MongoDB
(integer) 1
127.0.0.1:6379> PFADD databases PostgreSQL
(integer) 1
127.0.0.1:6379> PFADD databases Oracle
(integer) 1

PFCOUNT Command

To see the number of databases supported, we can run:

127.0.0.1:6379> PFCOUNT databases
(integer) 4

PFMERGE Command

Let us also assume that we have a list of unsupported databases:

127.0.0.1:6379> PFADD unsupported Elasticsearch Solr Neo4j Memcached
(integer) 1

To create a union of both supported and unsupported databases, we can use the PFMERGE command as shown:

127.0.0.1:6379> PFMERGE all databases unsupported
OK

We can then count the number of databases in the new key as:

127.0.0.1:6379> PFCOUNT all
(integer) 8

Closing

This tutorial covered how to work with Redis HyperLogLog using PFADD, PFCOUNT, and PFMERGE commands.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.