REDIS

Redis in a Nutshell

All you need to know about Redis

Sarimkhan
CodeX

--

If you are looking for a basic and simplified understanding of Redis, then this article is just the right place for you.

Redis which stands for “Remote Dictionary Server”, is a key-value, fast, open source in-memory data store. The word in-memory means, it stores all the data in primary memory(i.e. RAM). This enables Redis to provide low latency and high throughput data access.

Redis is a NoSQL database. It is used as a database, queue, cache, and message broker.

Supported data structures in Redis

Redis supports strings, hashes, sets, sorted sets, lists, bitmaps, bitfields, hyperloglog, geospatial indexes, and streams

Accessing Redis

You can easily access Redis using the Redis-CLI server. It allows users to execute commands/queries for all available operations. The below command is used to access redis-cli:

redis-cli -h <your host ip> -p <your host port>
redis-cli -h 127.0.0.1 -p 8999

Redis Clients

Redis provides the extensibility to connect with almost any programming language using different clients. Click here for the list of clients available.

At this point, you must be wondering about how Redis provides persistence and replication options while being an in-memory database.

Now let’s jump-in to discuss the available options

Persistence

There are 2 persistence options available in Redis:

1- Redis Database File (RDF)

RDF is the default persistence option. It is similar to taking snapshots(point-in-time copies of the data). It provides flexibility to take snapshots after a certain duration or after certain keys change. To configure RDF, just access the Redis.conf file and use the below command:

save <seconds> <keychanged>
save 900 1 //after 900 seconds or atleast 1 key changed

2- Append-only file (AOF)

AOF is not a default persistence option. It logs every operation in a system file. When Redis is restarted, it reconstructs the dataset from this file. To configure AOF, just access the Redis.conf file and use the below command:

appendonly yes  //to enable AOF
appendfilename "appendonly.aof" //AOF filename

Replication

In the basic Redis replication(excluding the high availability features of Redis Cluster or Redis Sentinel) there is a master-replica replication mechanism. The replica nodes are exact copies of their master node. To configure replication on a slave node, just access the Redis.conf file and use the below command:

replicaof <masterip> <masterport>
replicaof 127.0.0.1 8999

Redis Pub/Sub

Redis also provides Pub/Sub mechanism, commonly known as Publish–subscribe pattern. To subscribe to a channel e.g. news , the subscriber will execute the below command:

SUBSCRIBE news

To publish any information on the channel news the publisher needs to execute the below command:

PUBLISH news "Good Day!"
Pubsub

Redis High Availability

There are 2 mechanisms for high availability in Redis. Let’s see some basic details about both.

Redis Cluster

It is a clustering solution. It splits your data into several servers and provides replication, failover, and automatic management. It’s great for large implementation for high availability with great speed of accessing data.

Sentinel

Sentinel requires at least 3 instances to run. If anyone of it fails, then you have a failover server ready to take over. Using sentinel is ideal when you need better protection and speed isn’t quite your major concern. It’s great for smaller implementations with high availability.

Redis Sentinel

That’s it!

If you like this article then please don’t forget to follow me on Sarim Khan.

Have fun, keep learning & always coding!

--

--

Sarimkhan
CodeX
Writer for

Software Engineer 💻. Documenting the experiences and sharing knowledge.