Redis Tutorial Part 3 – Redis Data structures and working with Node.js ioredis

Redis Data structures

Key-value stores mostly support string keys and string data but Redis does a lot more than that. Redis is not just a simple key-value string store
It supports complex kinds of data structures, not just string. We will look more into this property of Redis in this tutorial and see how to utilize this using Node.js and ioredis module.

Get the ioredis module installed on your app from



1 String – Basic kind of string. Lets us say the name of a person. We will see how we can store this value using ioredis
We set value bar into key foo using set and get the value using get.

2 Binary Safe String – Can contain any type of data represented as a string: PNG images or serialized objects, for example.
Here we have a base64 encoded data of an image.We store this data in redis.Remember a value can’t be bigger than 512 MB.


We can store buffer data as well in redis..

3 Lists – Collections of elements ordered according to the order of insertion



4 Sets – Sets of data. Here we have a set of databases which we will add to Redis using sadd . smembers will give us the members in the set.

5 Ordered Sets – The number 3,2,2 gives the priority/score for the value that needs to be stored using the zadd function.

To get the data based on priority/score we use the zrangebyscore

We get ‘otherdbs’, ‘redis’ which have priority/score between 1 and 2 which will be obtained using zrangebyscore.

zcard gives all in the set

6 Hashes – Hashes are the kind with which we store objects in Redis. Hashes represent maps between the string fields and values

hset – Set the value for a field in key .this also allows multiple key-value pairs at once.

hmset – Set the whole object.Which means that hmset allows multiple key-value pairs at once.

hgetall – Get all fields and values of the hash stored at key

hmget – Get value stored for specific fields. The fields should be provided as an array.

7 Bit arrays – Bit maps or Bit arrays represent bits of data that are 0 and 1 (binary ) that can range up to  2^32. The bit can be also said to be stored in Redis as a string with each bit having an offset position.

For example alphabet A can be represented in bits as 01000001

The offset position 1 and 7 have value 1 and others by default value 0

So we set bits in offset positions 0 and 7 as 1 other will have value 0 by default. We use setbit to set bit value in offset position. On getting the mybitA we will get value as  A.

8 HyperLogLogs -HyperlogLogs is a kind of data structure used to get the cardinality. Finding the cardinality of a set means to find the number of unique elements among a set of values. This data structure is important if you want to get the count of something unique like views or number of visitors.

We add data to a Hyperloglog set using the function pfadd

The pfcount  will give the number of unique values in this case 4

9 Redis streams

Streams are one of the latest kinds of data structures introduced in the new versions of Redis. Stream basically is continuing data to which we can append new data as it comes in. In short, we can say streams are append-only data structure.

See more on Redis streams here https://redis.io/topics/streams-intro


See more on Redis data structures and abstraction from the official documentation here


One Comment

  • Amanda Mahaffey April 23, 2020 at 12:24 am

    Greetings, I was just taking a look at your website and submitted this message via your feedback form. The “contact us” page on your site sends you messages like this to your email account which is the reason you’re reading my message at this moment correct? This is the most important achievement with any kind of online ad, getting people to actually READ your advertisement and I did that just now with you! If you have something you would like to promote to lots of websites via their contact forms in the US or to any country worldwide let me know, I can even target specific niches and my prices are very affordable. Write a reply here: Phungcorsi@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *