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 :
So open your Redis-server and let’s check what is behind these different data types, and test some great features like:
- PUB SUB
The different Data type
- Redis HyperLogLog
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%.
PFADDadds an item to a HyperLogLog.
PFCOUNTreturns an estimate of the number of items in the set.
PFMERGEcombines two or more HyperLogLogs into one.
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
SETBITsets a bit at the provided offset to 0 or 1.
GETBITreturns the value of a bit at a given offset.
BITOPlets 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.
- 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”.
- 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
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:
SUBSCRIBESubscribes the client to the specified channels.
UNSUBSCRIBEUnsubscribes the client from the given channels, or from all of them if none is given.
PUBLISHPosts a message to the given channel.
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.
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
127.0.0.1:6379> XRANGE Barcelona:Ronaldo:R9 1683834574565-0 + COUNT 3
- We can also check the length of the stream:
XADDadds a new entry to a stream.
XREADreads one or more entries, starting at a given position and moving forward in time.
XRANGEreturns a range of entries between two supplied entry IDs.
XLENreturns the length of a stream.
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.