Introduction:

Here is the second part of the Redis keys and features, we will see how to use, with a description from the official Redis site and some example for :

  • Hyperloglog
  • BITMAP

So open your Redis-server and let’s check what is behind these different data types, and test some great features like:

  • STREAMS
  • PUB SUB

The different Data type

  • Redis HyperLogLog

Description:

HyperLogLog is a data structure that estimates the cardinality or the size of a set. As a probabilistic data structure, HyperLogLog trades perfect accuracy for efficient space utilization.

The Redis HyperLogLog implementation uses up to 12 KB and provides a standard error of 0.81%.

Basic command:

  • PFADD adds an item to a HyperLogLog.
  • PFCOUNT returns an estimate of the number of items in the set.
  • PFMERGE combines two or more HyperLogLogs into one.

Example:

  • Limit

The HyperLogLog can estimate the cardinality of sets with up to 18,446,744,073,709,551,616 (2^64) members.

Also to have a better understanding of Hyperloglog usage I advice you to check this great video from a Redis master Justin Castilla

  • Bitmaps (BitStrings)

Redis bitmaps are an extension of the string data type that lets you treat a string like a bit vector. You can also perform bitwise operations on one or more strings.

It can be really useful for analytics and for counting / checking activity

Basic commands

  • SETBIT sets a bit at the provided offset to 0 or 1.
  • GETBIT returns the value of a bit at a given offset.
  • BITOP lets you perform bitwise operations against one or more strings.
  • BITPOS – Locate the first bit set to 1 or 0 in a string.
  • BITCOUNT – Count the number of bits set to 1 in a string.

Example:

Redis Features

  • PUB SUB ( publish and subscribe )

Redis allows to set up a Publish-subscribe system where a “publisher” will be able to send a message which can then be listened by different “subscribers”.

Example:

  • I will create a channel named NABIL_INFO using this command:

127.0.0.1:6379> PUBLISH channel “message”

127.0.0.1:6379> PUBLISH NABIL_INFO “Pele is the greatest”

  • I will add a new message
  • I will SUBSCRIBE to this channel by opening a new Redis window and the below syntax

127.0.0.1:6379> subscribe NABIL_INFO

127.0.0.1:6379> subscribe channel

Important note:

As the message are not persistent, you will only the receive the message published AFTER your subscription

I have subscribed to the channel on another Redis windows and I only receive the latest messages as you can see below:

Basic commands

  • SUBSCRIBE Subscribes the client to the specified channels.
  • UNSUBSCRIBE Unsubscribes the client from the given channels, or from all of them if none is given.
  • PUBLISH Posts a message to the given channel.
  • STREAMS

Definition:

A Redis stream is a data structure that acts like an append-only log. You can use streams to record and simultaneously syndicate events in real time. Examples of Redis stream use cases include:

  • Event sourcing (e.g., tracking user actions, clicks, etc.)
  • Sensor monitoring (e.g., readings from devices in the field)
  • Notifications (e.g., storing a record of each user’s notifications in a separate stream)

Redis generates a unique ID for each stream entry. You can use these IDs to retrieve their associated entries later or to read and process all subsequent entries in the stream.

Redis streams support several trimming strategies (to prevent streams from growing unbounded) and more than one consumption strategy (see XREAD, XREADGROUP, and XRANGE).

Example:

127.0.0.1:6379> XADD key [NOMKSTREAM] [MAXLEN|MINID [=|~] threshold [LIMIT count]] *|ID field value [field value …]
We will add the stats of Ronaldo R9 and update the stream each time he scores make an assist and make a dribble:
127.0.0.1:6379>XADD Barcelona:Ronaldo:R9 * GOALS 12 ASSISTS 09 SUCCESSFUL_DRIBBLE 46

Now we will Read the first three stream entries starting at ID "1683834574565-0":

127.0.0.1:6379> XRANGE Barcelona:Ronaldo:R9 1683834574565-0 + COUNT 3

  • We can also check the length of the stream:

Basic commands:

  • XADD adds a new entry to a stream.
  • XREAD reads one or more entries, starting at a given position and moving forward in time.
  • XRANGE returns a range of entries between two supplied entry IDs.
  • XLEN returns the length of a stream.

Conclusion:

We have tested Redis data type and also seen some examples on how to use it.Also you know now how to use features like PUB SUB and STREAMS but it can be a topic where we can go deeper in the future.

I forgot to talk about an interesting Redis data type which is Redis geospatial which is all about coordinate and location, I will make a post on it soon.

I invite you to share with us and to visit dbi bloggers, also don’t forget to take a look on my other blogs, stay tuned for another post 😉