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

April 23, 2019

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

https://www.npmjs.com/package/@types/ioredis

 

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.

base64 of png – iVBORw0KGgoAAAANSUhEUgAAACMAAAAjCAYAAAAe2bNZAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gUfFBM7nAl7igAAAPVJREFUWMNj1G/R/88wSAATwyACLMgcYzljojXefHmT4cvPLwzU1A93TJRpFEOZWxnRhj37+IzBa4oXA7n6b7y8wRAxJwK7Y3g5eBkYGBgYNl7ayPD843O8BmXYZjBI8UuhiMH0Ews0xDXwRxMDAwPDpoubGM4+OkvQMcM+AY86ZtQxo46hSXXAwMDA4Kfvx2AibzKwjvn84zMDAwMDg7+eP9HVATKA6SelOkEHjMhNCFJC5NmHZxgGUqqfcbQ9M9qeobQ9QyzA1Z4htj1EVHuGUkBJe2i0Ohh1zKhjRkZ7hlJASXuIhVrtEUrbQ6PtmdHcRA4AAAq6hb9r4YF9AAAAAElFTkSuQmCC

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

https://redis.io/topics/data-types-intro

No Comments

Leave a Reply

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