Using RedisHyperLogLog

A HyperLogLog in Redis is an algorithm that allows you to count the number of unique items in a set without incurring significant memory usage. 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 we support. We can add each entry to the HyperLogLog as:> PFADD databases MySQL
(integer) 1> PFADD databases MongoDB
(integer) 1> PFADD databases PostgreSQL
(integer) 1> PFADD databases Oracle
(integer) 1


To see the number of databases supported, we can run:> PFCOUNT databases
(integer) 4


Let us also assume that we have a list of unsupported databases:> 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:> PFMERGE all databases unsupported

We can then count the number of databases in the new key is:> PFCOUNT all
(integer) 8


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

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list