Learn REDIS the easiest way in 5 Steps

What is REDIS?

Redis is an open-source BSD licensed advanced key-value store in-memory NoSQL database.  REDIS stands for REmote DIctonary Server. It is also referred to as a data structure server, since the keys can contain strings, hashes, lists, sets & sorted sets, etc. Redis can be used as Database (persist data), Memory cache (temporary store to improve performance) and also it can be be used as message broker.

redis tutorial

This a REDIS tutorial for beginners.

Redis has two-parts, Redis server, and Redis client (Redis CLI). Redis servers can be interacted using the command-line tool Redis CLI (Command Line Interface).

How to run REDIS on Windows 10

Step1:  Make Windows Power Shell as the default shell, then open Windows Power shell as Administrator to enable Windows System for Linux(WSL) Use below command to enable it:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Step2: Reboot the windows after making these changes.

Step3: Download and install one of the supported Linux distros from the Micro Soft Store (e.g. RAFT WSL)

Install And Run Redis:

Step 1: Launch the distros (RAFT WSL) using it install Ubuntu

Step 2: you’ll need to wait for initialization and create a login upon first use.

Step3: Run below set of command to install redis-server and redis-cli

> sudo apt-get update
> sudo apt-get upgrade
> sudo apt-get install redis-server
> redis-cli -v

Step4: To make sure redis-server is running

sudo service redis-server restart

Step5: Start redis-cli and run below commands.

$ redis-cli 
127.0.0.1:6379> set user:1 "JBT"
127.0.0.1:6379> get user:1
"JBT"

Redis Command

Redis command can be divided into different groups e.g. Connection, Geo, Hashes, Keys, Lists, etc. Each group contains the list of commands used for specific purposes. In this article, we will try to explore some mostly used command in Redis. And we will get to know the purpose of those commands and how they can be used in redis?

Redis Connection

AUTH (Authentication):

Once Redis server is up. It can authenticate the incoming request in any of the two different ways.

  • Redis ACL system (Not Discussed here)
  • Using requirepassword option to make Redis server authenticate incoming requests against a given password. And this can be done in two ways.
    • Set password from Redis Client using the command
    • Pass config file as a parameter while starting the Redis server which has a password in it.

Once connected to Redis Server, set value of key “requirepass” in config for enabling the authentication in Redis Server like below.

127.0.0.1:6379> config get requiredpass
(empty list or set)
127.0.0.1:6379> config set requirepass password
OK
127.0.0.1:6379> config get requiredpass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth password
OK
127.0.0.1:6379> set name javabeginnerstutorial
OK
127.0.0.1:6379> get name
"javabeginnerstutorial"

Or you can pass config file as a parameter while starting the Redis Server with below content.

requirepass password

Command to start Redis server

redis-server /path/to/configfile/redis.conf

CLIENT SETNAME:

Assigns name to the current connection and the assigned name is displayed in the output of CLIENT LIST command.

127.0.0.1:6379> client setname jbtclient
OK
127.0.0.1:6379> client list
id=3 addr=127.0.0.1:53500 fd=8 name=jbtclient age=395 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client

Open another redis-cli and run below commands to understand more about client name:

127.0.0.1:6379> client list
id=3 addr=127.0.0.1:53500 fd=8 name=jbtclient age=489 idle=94 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=client
id=4 addr=127.0.0.1:53662 fd=9 name= age=28 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client

CLIENT GETNAME:

Returns the current client name set by CLIENT SETNAME command.

127.0.0.1:6379> client getname
"jbtclient"

CLIENT ID:

This will return current connection client id.

127.0.0.1:6379> client id
(integer) 3

CLIENT LIST

127.0.0.1:6379> client list
id=3 addr=127.0.0.1:53500 fd=8 name=jbtclient age=879 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client

PING

Returns PONG if no argument is provided, otherwise return a copy of the argument as a bulk. This command is often used to test if a connection is still alive, or to measure latency.

127.0.0.1:6379> ping
PONG
127.0.0.1:6379> ping jbt
"jbt"

SELECT

Select the Redis logical database having the specified zero-based numeric index. New connections always use the database 0, this command cannot be used while using the Redis cluster because the redis cluster only supports zero databases.

127.0.0.1:6379> select 0
OK

Key Group

KEYS

This command returns all keys matching the given pattern. Use this command with care in production with a large database. As it will hamper the performance. (Use keys * to print available keys in REDIS.)

127.0.0.1:6379> keys n*
1) "name"
127.0.0.1:6379>

Supported glob-style patterns:

  • h?llo matches hello, hallo and hxllo
  • h*llo matches hllo and heeeello
  • h[ae]llo matches hello and hallo, but not hillo
  • h[^e]llo matches hallo, hbllo, … but not hello
  • h[a-b]llo matches hallo and hbllo

Use  to escape special characters if you want to match them verbatim.

EXPIRE

When you want a specific key to be expired after a set amount of time. You can use EXPIRE command. It sets a timeout on the given key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology. ttl (time-to-live) command can be used to know how long the given key will survive.

If you have set a timeout of 10 second on a key. You can check with ttl the remaining to live for the key.

Expire can returns any of the below value, which has different meaning .

  • 1 if the timeout was set.
  • 0 if key does not exist.
  • -1 never expires
127.0.0.1:6379> set name "Java Beginners Tutorial"
OK
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 7
127.0.0.1:6379> set name "Java Beginners Tutorial"
OK
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379> expire name2 10
(integer) 0

EXISTS

  • 1 if the key exists.
  • 0 if the key does not exist.
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists name2
(integer) 0
127.0.0.1:6379>

MOVE

Move key from the currently selected database to the specified destination database.

Returns:

  • 1 if key was moved.
  • 0 if key was not moved.
127.0.0.1:6379> move name 1
(integer) 1
127.0.0.1:6379> keys name
(empty list or set)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys name
1) "name"
127.0.0.1:6379[1]> get name
"Java Beginners Tutorial"
127.0.0.1:6379[1]>

RENAME

Renames key to new key. It returns an error when key does not exist. If new key already exists it is overwritten

127.0.0.1:6379> rename name jbt
OK
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> get jbt
"Java Beginners Tutorial"
127.0.0.1:6379> rename jbt name
OK

TTL

Returns the remaining time to live of a key that has a timeout

return value in case of error changed:

  • The command returns -2 if the key does not exist.
  • The command returns -1 if the key exists but has no associated expire.
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 7
127.0.0.1:6379> set name "Java Beginners Tutorial"
OK
127.0.0.1:6379> ttl name2
(integer) -2
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379>

String Group

GET

Get the value of key. If the key does not exist, the special value nil is returned. An error is returned if the value stored at key is not a string, because it only handles string values.

127.0.0.1:6379> get name
"Java Beginners Tutorial"
127.0.0.1:6379> get name2
(nil)
127.0.0.1:6379>

CONFIGGET

This command is used to read the configuration parameters of a running Redis server.

127.0.0.1:6379> config get *max-*entries
1) "hash-max-ziplist-entries"
2) "512"
3) "stream-node-max-entries"
4) "100"
5) "set-max-intset-entries"
6) "512"
7) "zset-max-ziplist-entries"
8) "128"
127.0.0.1:6379>

MGET

Returns the values of all specified keys. For every key that does not hold a string value or does not exist, the special value nil is returned.

127.0.0.1:6379> MGET name name2
1) "Java Beginners Tutorial"
2) (nil)
127.0.0.1:6379>

GETRANGE

Returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive).

127.0.0.1:6379> getrange name 0 3
"Java"
127.0.0.1:6379> getrange name -3 -2
"ia"
127.0.0.1:6379> getrange name -3 -1
"ial"
127.0.0.1:6379> getrange name 0 -1
"Java Beginners Tutorial"
127.0.0.1:6379> getrange name 0 25
"Java Beginners Tutorial"
127.0.0.1:6379> getrange name 10 23
"ners Tutorial"
127.0.0.1:6379>

APPEND

It will append given string to value of key and returns the length of string after appending.

127.0.0.1:6379> set framework "Spring"
OK
127.0.0.1:6379> get framework
"Spring"
127.0.0.1:6379> append framework " 5"
(integer) 8
127.0.0.1:6379> get framework
"Spring 5"
127.0.0.1:6379>

SET

Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type.

Options:

  • EX seconds — Set the specified expire time, in seconds.
  • PX milliseconds — Set the specified expire time, in milliseconds.
  • NX — Only set the key if it does not already exist.
  • XX — Only set the key if it already exist.
  • KEEPTTL — Retain the time to live associated with the key.
127.0.0.1:6379> set framework Spring EX 60
OK
127.0.0.1:6379> get framework
"Spring"
127.0.0.1:6379> ttl framework
(integer) 52
127.0.0.1:6379>

MSET

Sets the given keys to their respective values. It replaces existing values with new values.

127.0.0.1:6379> mset framework Spring language Java
OK
127.0.0.1:6379> mget framework language
1) "Spring"
2) "Java"
127.0.0.1:6379>

MSETNX

Sets the given keys to their respective values. It will not perform any operation at all, even if just a single key already exists.

127.0.0.1:6379> msetnx framework Spring frontend Angular
(integer) 0
127.0.0.1:6379> mget name framework frontend
1) "Java Beginners Tutorial"
2) "Spring"
3) (nil)
127.0.0.1:6379>

GETSET

Automatically sets key to value and returns the old value stored at key. Returns an error when key exists but does not hold a string value.

127.0.0.1:6379> set mykey hello
OK
127.0.0.1:6379> getset mykey world
"hello"
127.0.0.1:6379> get mykey
"world"
127.0.0.1:6379>

List Group

LPUSH

Insert all the specified values at the head of the list stored at key. If the key does not exist, it is created as an empty list before performing the push operations. When the key holds a value that is not a list, an error is returned. It returns the length of the list after a successful insert.

127.0.0.1:6379> lpush mylist "World"
(integer) 1
127.0.0.1:6379> lpush mylist "hello"
(integer) 2
127.0.0.1:6379> lpush mylist "JBT"
(integer) 3
127.0.0.1:6379> lpush mylist "Java"
(integer) 4
127.0.0.1:6379> lpush name Spring
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379>

LIDEX

Returns the element at index index in the list stored at key.

127.0.0.1:6379> lindex mylist 0
"hello"
127.0.0.1:6379> lindex mylist 1
"World"
127.0.0.1:6379> lindex mylist 5
(nil)
127.0.0.1:6379>

LLEN

Returns the length of list stored at key. If the key does not exist, it pretended as empty list and returns 0. It returns an error when the value stored at key is not a list.

127.0.0.1:6379> llen mylist
(integer) 4
127.0.0.1:6379> llen mylist2
(integer) 0
127.0.0.1:6379> llen name
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> get name
"Java Beginners Tutorial"
127.0.0.1:6379>

LPUSHX

Inserts specified values at the head of the list stored at key, only if key already exists and holds a list. In contrary to LPUSH no operation will be performed when key does not yet exist.

127.0.0.1:6379> LPUSHX mylist nodejs
(integer) 5
127.0.0.1:6379> lpushx mylist2 nodejs
(integer) 0
127.0.0.1:6379>

RPUSH

Insert all the specified values at the tail of the list stored at key. If the key does not exist, it is created as an empty list before performing the push operations. When the key holds a value that is not a list, an error is returned. It returns the length of the list after a successful insert.

127.0.0.1:6379> RPUSH mylist "Java Beginners Tutorial" "REDIS"
(integer) 7
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nodejs"
2) "Java"
3) "JBT"
4) "hello"
5) "World"
6) "Java Beginners Tutorial"
7) "REDIS"
127.0.0.1:6379> rpush name JBT
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> RPUSH mylist2 "JBT"
(integer) 1
127.0.0.1:6379>

RPUSHX

Inserts specified values at the tail of the list stored at key, only if a key already exists and holds a list. In contrary to RPUSH no operation will be performed when the key does not yet exist.

127.0.0.1:6379> rpushx mylist "Spring Boot" "Kotlin"
(integer) 9
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nodejs"
2) "Java"
3) "JBT"
4) "hello"
5) "World"
6) "Java Beginners Tutorial"
7) "REDIS"
8) "Spring Boot"
9) "Kotlin"
127.0.0.1:6379> RPUSHX mylist2 "Kotlin"
(integer) 0
127.0.0.1:6379>

LRANGE

Returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes.

127.0.0.1:6379> lrange mylist 0 4
1) "nodejs"
2) "Java"
3) "JBT"
4) "hello"
5) "World"
127.0.0.1:6379> lrange mylist 0 -1
1) "nodejs"
2) "Java"
3) "JBT"
4) "hello"
5) "World"
6) "Java Beginners Tutorial"
7) "REDIS"
8) "Spring Boot"
9) "Kotlin"
127.0.0.1:6379>

LINSERT

Inserts element in the list stored at key either before or after the reference value. No operation done when key does not exist.

Returns:

  • An error is returned when key exists but does not hold a list value.
  • the length of the list after the insert operation
  • -1 when the value pivot was not found
127.0.0.1:6379> linsert mylist after Kotlin Angular
(integer) 10
127.0.0.1:6379> lrange mylist 0 -1
 1) "nodejs"
 2) "Java"
 3) "JBT"
 4) "hello"
 5) "World"
 6) "Java Beginners Tutorial"
 7) "REDIS"
 8) "Spring Boot"
 9) "Kotlin"
10) "Angular"
127.0.0.1:6379> linsert mylist before JBT Blog
(integer) 11
127.0.0.1:6379> lrange mylist 0 -1
 1) "nodejs"
 2) "Java"
 3) "Blog"
 4) "JBT"
 5) "hello"
 6) "World"
 7) "Java Beginners Tutorial"
 8) "REDIS"
 9) "Spring Boot"
10) "Kotlin"
11) "Angular"
127.0.0.1:6379> linsert mylist after ANgular React
(integer) -1
127.0.0.1:6379> linsert name before JBT java
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379>

LREM

Removes the first count occurrences of elements equal to element from the list stored at key. The count argument influences the operation in the following ways:

  • count > 0: Remove elements equal to element moving from head to tail.
  • count < 0: Remove elements equal to element moving from tail to head.
  • count = 0: Remove all elements equal to element.

It returns no of elements removed.

127.0.0.1:6379> lrem mylist -1 hello
(integer) 1
127.0.0.1:6379> lrem mylist -2 World
(integer) 1
127.0.0.1:6379> lrem mylist 2 Ja
(integer) 0
127.0.0.1:6379> lrem mylis 0 Java
(integer) 0
127.0.0.1:6379> lrem mylist 0 Java
(integer) 1
127.0.0.1:6379>

LPOP & RPOP

  • LPOP removes and returns first element in list stored at key
  • RPOP removes and returns last element in list stored at key
127.0.0.1:6379> lpop mylist
"nodejs"
127.0.0.1:6379> rpop mylist
"Angular"
127.0.0.1:6379>

Conclusion:

So here we completed our Redis tutorial, how to set up Redis from the scratch. Looked in Redis networking, authentication overview with detailed steps. Comment below in case you want to discuss more about the Redis tutorial.

Leave a Comment