Experience Redis in 5 minutes

Mehmet Cambaz
4 min readJul 22, 2021

--

Redis is an in-memory database that persists on disk. The data model is key-value however many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps. (Even RedisJSON is a Redis module that provides native JSON capabilities)

I will use Ubuntu Desktop 20.04.02 LTS for this article.

Step 1: Install Redis Server

sudo apt install redis-server

Step 2: Start Redis Server and check its status

redis-server
redis-cli ping

As you can see from warning I did not create a config file it is using default settings. redis-cli is the command line interface tool for Redis Server, when we execute redis-cli ping we get a PONG a response from Redis Server stating server is up-and-running.

Step 3: Set and Get some data

redis-cli

This will open Redis command line interface, then we will try basic commands with basic data types.

set server:name cambaz
get server:name
get name
exists server:name
exists server:name2
set selim 16
get selim
incr selim
incr selim
set mehmet 16 ttl ex 120
ttl mehmet
ttl mehmet

This is a simple expiring data, ttl is decreasing time to live of the data

rpush names `mc`
rpush names `mc2`
lrange names 0 -1
lpush names mc16
lrange names 0 -1
lrange names 1 -1
lrange names 1 2
lrange names 1 1
lrange names 0 1

This is a sample for list data type, rpush adds to end lpush adds to beginning, lrange gets the relevant list items with given indexes, if you give index -1 it goes until the end of the list.

lpop names
rpop names
llen names

When you use lpop, it returns the first item as output and removes it from list. When you use rpop, it returns the last item as output and removes it from list. llen shows the list item count.

hset user:1 name mehmet
hset user:1 surname cambaz
hset user:1 color blue
hset user:1 job engineer
hgetall user:1
hset user:2 name selim surname cambaz color red job kid
hgetall user:2

Hashes, which are maps composed of fields associated with values. Both the field and the value are strings. You can set data members one by one or at one command.

hset user:1 visits 10
hgetall user:1
hincrby user:1 visits 100
hgetall user:1

You can update/add to the data by simply setting it, you can also use hincrby command to execute atomic increments.

Failover with Redis Sentinel

sudo apt install redis-sentinel

Redis Sentinel provides high availability for Redis. In practical terms this means that using Sentinel you can create a Redis deployment that resists without human intervention certain kinds of failures. You need at least three Sentinel instances for a robust deployment.

       +----+
| M1 |
| S1 |
+----+
|
+----+ | +----+
| R2 |----+----| R3 |
| S2 | | S3 |
+----+ +----+

Configuration: quorum = 2

Source: https://redis.io/topics/sentinel

Sample config: https://dltlabs.medium.com/how-to-ensure-a-high-availability-of-redis-with-redis-sentinel-438d1f76624

Cluster vs Sentinel discussion at Stackoverflow: https://stackoverflow.com/questions/31143072/redis-sentinel-vs-clustering

Other valuable sources that you can leverage:

--

--

No responses yet