Table of contents
1.
Introduction
2.
Redis supports high availability and failover recovery
3.
Frequently Asked Questions
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Replication in Redis

Author GAZAL ARORA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Redis is an in-memory data structure used as a database, cache, message broker, and streaming engine. It offers built-in Replication, Lua scripting, LRU eviction, transactions, several levels of on-disk persistence, Redis Sentinel for high availability, and Redis Cluster for automatic partitioning. You can use Redis from most programming languages.

Redis is open-source (BSD licence). Redis allows data structures like strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams. 

You can install Redis from here.

A leader-follower (master-replica) replication is used and configured at the base of Redis replication (excluding the high availability features offered as an additional layer by Redis Cluster or Redis Sentinel). It enables replica Redis instances to be identical to master ones. When the link between the replica and the master breaks, the replica will automatically reconnect and seek to be an exact clone of the master, regardless of what happens to the master.

There are three primary mechanisms in Redis Replication.

  1. When a master and replica instance is well-connected, the master keeps the replica up to date by delivering a stream of commands to the replica to replicate the effects on the master dataset caused by: client writes, keys expired, evicted, or any other action modifying the master dataset.
  2. When the master-replica link is broken due to network troubles or a timeout in either the master or the replica, the replica reconnects and performs a partial resynchronization, which means it will only try to obtain the portion of the stream of commands it lost during the disconnection.
  3. If a partial resynchronization is not achievable, the replica will request a full resynchronization. The master creates a snapshot of all of its data, sends it to the replica, and then continues to deliver the stream of commands as the dataset changes.

Redis supports high availability and failover recovery

Redis uses asynchronous Replication by default, which is the most natural replication mode for most Redis use cases due to its low latency and great speed. On the other hand, Redis replicas asynchronously acknowledge the amount of data they got from the master regularly. As a result, the master does not have to wait for a command to be processed by the replicas every time, but it does know, if necessary, which replica has already processed which instruction. This enables optional synchronous Replication.
 

Clients can use the WAIT command to request synchronous Replication of specific data. However, WAIT can only ensure that the specified number of acknowledged copies are present in the other Redis instances; it does not transform a collection of Redis instances into a CP system with solid consistency: acknowledged writes can still be lost during a failover, depending on the Redis persistence configuration. However, using WAIT, the chances of losing a write after a failure event considerably decrease, especially for challenging failure modes to trigger.

 

Redis Sentinel provides high availability for Redis. Redis Sentinel also serves as a configuration provider for clients and does additional functions like monitoring and notifications.

  • Monitoring Sentinel monitors the health of your master and replica instances regularly.
  • Notification Sentinel can use an API to alert the system administrator or other computer applications when one of the monitored Redis instances has a problem.
  • Automatic failover: If a master fails, Sentinel can initiate a failover process in which a replica is promoted to master, the other extra replicas are reconfigured to use the new master, and the Redis server apps are told of the new address to use when connecting.
  • Configuration provider Sentinel: Clients connect to Sentinels to get the address of the current Redis master in charge of certain services. Sentinels will report the new address if there is a failover.

 

Check out the Redis Sentinel or Redis Cluster documentation for additional details on high availability and failover.

Frequently Asked Questions

1. What is a replication group?
Replication groups support Multi-data centre deployments and disaster recovery situations. A group ID is used to identify replication groups. A group ID is a unique identifier assigned to a duplicated domain (one group ID per replicated domain).

2. What is the difference between Redis replication and sharding?
Sharding, also known as partitioning, is the process of partitioning data by key; Replication, also known as mirroring, is copying all data. Sharding improves performance by lowering a single resource's hit and memory load. Replication is helpful in increasing readability.

Conclusion

In this article, we learned about Replication In Redis. We also learned about How Redis supports high availability and failover with replication.

Redis also allows data structures like strings, hashes, lists, sets, sorted sets with range searches, bitmaps, hyperloglogs, geographic indexes, and streams. Click here to know about data types in Redis.

Click here to learn more about Redis Configuration.

 

Happy Coding!.

You can use Coding Ninjas Studio to practice various DSA questions asked in the interviews. It will help you in mastering effective coding techniques, and you will also get interview experiences with people working in big companies.

Live masterclass