REDIS- WHAT IS REDIS, WHEN TO USE WITH COMMANDS AND EXAMPLES

M D PUNEETH REDDY
7 min readApr 7, 2021

OBJECTIVES:

  1. what is redis?
  2. what is the use?
  3. setup redis on windows?
  4. Play with some commands over data structures

WHAT IS REDIS?

Redis is a in- memory data store, where we can save the data which is processed. Redis supports good amount of data structures strings, lists, maps, sets, sorted lists etc.,

Redis stores the values as a key value pair, we will see some examples how it is going to store in this blog below. so basically we will use redis for cache the data.

Cache the data means to store the data which is processed or API calls response data etc., files, images all data that is stored is called Caching.

WHAT IS THE USE OF REDIS?

We can see above as it will store or cache the data and also it supports different types of data structures, we can reuse the data again without processing again.

so we can use this functionality when the parameters to process to get the data don’t change frequently, if it don’t change frequently, we can cache the data or store the data in some structure to reuse again, so here comes Redis server.

SETUP REDIS SERVER ON WINDOWS

you can download this windows version form here.

Download the zip and extract the folder, you can see 2 files in it redis.server and redis.client, open both the applications

By default Redis server run on localhost and port 6379

when you open both redis server and redis client we will see like this,

this is redis server , which wont stop it will accept the client connection

now we will see some operations to store data in redis server from redis client

This is how by default redis-client will look

COMMANDS OVER DATA STRUCTURES:

STRINGS:

SET AND GET:

Here in the below image we set the key value pair and get the value by key

for example: set is the command here, username is the key and puneeth is the value

next we can get the value by calling key using get command

DEL:

to delete the key we use “del” command to delete the key

APPEND:

This command will helps to append the string to existing value

MSET:

set multiple key value using “mset-multiple set”

mset followed by key and value pairs

you can get the value by specific key,

LISTS:

Redis can store the list values, here we will see the key words to set the list

LPUSH,RPUSH:

  1. lpush- push from left
  2. rpush- push from right

here we first push from left in the key usernames and the value is puneeth

next again we push from left to the same key and the value is midhun, now you might figure it out the order of usernames is “midhun” and next “puneeth”

next we will try to add from right side and check the order,

LRANGE:

to check the order we have a key to use command ” lrange- list range ” and next we need to set the key and the range of values to get “start and stop offset”, then we will get the values from the server

LINDEX:

If you want to get by index there is a command called “lindex — list index” , below screenshot will give you clear explanation

get list index of the list which is a key and the index on that list to retrieve the value.

LLEN:

To find the length of the list we can use the command “llen- list length”

LPOP,RPOP:

Remove the leftmost of the list we use “lpop-left pop” and to remove right most element of the list we use “rpop-right pop” and the output it gives the removed elements

LREM:

To remove the elements from the list we can use “lrem- list remove”, I inserted some elements into the list, and you can see below to see the elements we used lrange

so we will remove the puneeth from the above list using lrem

lrem-command

usernames-list or key

1- no of occurences to remove

puneeth-value of the occurence to remove

so basically it will remove 1 “puneeth” from left

So, now we will do another example

Now we removed 2 occurences of “puneeth” from left and the output left in the list ,

if you want to remove from right side we can just add negative value to the no of occurences then it will remove the elements from right side

RPOPLPUSH:

we have a commond rpoplpush to remove the right most element of one list and add to left side of another list

In the above example created the subjects list and used command rpoplpush , first it pop from username from right side and add to the left side of another list.

SETS:

Sets is a data structure to store the unique values with no order, We we will see some commands that will operate the sets

SADD:

“SADD-set add” to add the element into the set

SMEMBERS:

“SMEMBERS- set members” to check all the values in the set

SREM:

“SREM- set remove” to remove the value present in the key

SCARD:

set card is used to know number of elements in the set

SPOP:

set pop to remove an element from the key, it will remove any element as set usually dont have the order

HASHMAPS:

Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects.

HMSET:

hmset is the mapping the key value pairs and store in the key. Here in the below example mixthings is hashmap key to store all key value pairs like one-1,two-2, machine learning-AI

HGETALL:

hashmap get all command used to get all the key value pairs in the hashmap key, so for the above example we will get all the key value pairs from hashmap key

HGET:

hget to get the single keys value from the hashmap key

HEXISTS:

hexists to let us know that key is present in the hashmap or not, if it gives output as 1 then present, 0-not present

HKEYS:

hkeys to get all the keys present in the hashmap

HDEL:

hdel to delete the key from the hashmap

CONCLUSION:

Here we have seen what is redis, what is the use of using this redis-server and redis-client , how to setup redis both client and server and workout on some basic commands,

In the next we are going to have a small example with nodejs API https://mdpuneethreddy.com/blog/redis-how-to-use-in-nodejs-to-fast-the-response/, how redis helps to fast the response.

If you need to look into more technology blogs, check out https://mdpuneethreddy.com/.

If you like this blog, please share to others, If you have any suggestions to improve in my blog, please write down in the comments section below

--

--