Redis Replication vs Redis Sentinel vs Redis Cluster

Redis is a very fast In Memory NoSQL database that is commonly used for caching data for fast reads and writes with milliseconds latency.

It can reach > 1 million OPS (Operations Per Second) very easily and reduce the disk’s IOPS load (input/output = read/write operations per second) ,which heavily influences databases and applications performance.

It is being used very widely for web and mobile applications to store more static information like dimensions tables , user data and dynamic data like user logins and sessions to speed up application response time.

Redis uses key value records where the value can be anything text or binary.

For example:> SET key1 hello
OK> GET key1

Another use of redis can be for pub sub ( message queueing) where clients apps can write information into the channel and many subscribers can listen and ingest the data in realtime from it.

Redis has an enterprise commercial edition which contains support and additional tools and community free addition where you can build a robust Redis cluster with all the features and functionality applications and organizations need.

Redis Persistence

Redis is in the memory database; some may want to not lose data when redis service is down. Redis support data persistency by writing the data into disk in several methods which you need to use according to the application use:

  • RDB – Redis Database . This is the snapshot of the database that is being taken every interval of time and easy to restore on the same or different server  . Good For performance as another fork process will write on every interval of time the snapshot to the disk . Data is stored usually in a file called – dump.rdb
    Snapshot configuration examples can be – save 60 1000 – meaning every 60 seconds or 1000 keys have been changed .
  • AOF – Append only file – which logs every data change into an append file where these changes can be replayed later to get point in time recovery . good if there is a need not to lose a single write . On the other hand, it can slow down writes .
  • RDB + AOF – Both above methods are used
  • None – Disable persistency and use only caching
In order to do automatic failover Redis Sentinel should be used.

Redis Master Slave

Redis can be installed in the same server as the application which holds its own redis instance for local caching reducing the network speed and can achieve high availability in case the application server is down and other servers are still up and running (Can be good when the cache size is small).

It can also be installed on a dedicated server shared  between all application servers (good when caches data is big and should be shared between all app servers).

To avoid SPOF (single point of failure) when Redis Installed on a dedicated server – master slave deployment should be used.

In  this type of deployment any data that is written to the master node is replicated to the slave .Incase the master crash, manual failover should be done to clients direct connection to the new master and rebuild the old master when it’s up again.

The replicas are read only and can be used by the application to scale reads . Writes are not scalable and you need to use Redis Cluster for it .

Redis Sentinel

Redis Sentinel  supports automatic failover and close to 0 downtime, its additional service that can be  installed on another server (like app servers). Redis Sentinel  monitors the cluster, verify if the master is not active, decides which node should be the master and does the automatic failover .This topology  requires at least 3 instances or any other odd number of sentinel instances for quorum and voting to be able to achieve majority (when it’s even number of servers – you can get even results), elect the new master and avoid split brain (situation where more than 1 server can be the master)

The replicas are read only and can be used by the application to scale reads.

Writes are not scalable and you need to use Redis Cluster for it.

Redis Replication Data Consistency

The replication is being done in an asynchronous way and there can be data loss in case master crashes before replication was sent to the slaves.

To avoid it you can use the WAIT  where WAIT 2 0  causes writes to wait until 2 replicas are acknowledged. Using this method reduces write performance.

Redis has a client library for all major programming languages that also support sentinel clusters. The Sentinel library registers the Redis Cluster nodes  addresses and at connection time knows which  one is the active master, Here is Python Sentinel Client – so no need for Load balancer in front.

In order to scale writes and reads Redis Cluster should be used.

So minimal redis cluster topology will look like this

Redis Cluster

Redis Cluster is used to scale out writes and reads. It is combined from several masters minimum 3 where each master holds a portion of the data = sharding where the shard distribution is being done by the document key hashing. each master node responsible for a subset of hash slots. So in cluster of 3 nodes:

  • Node A contains hash slots from 0 to 5500.
  • Node B contains hash slots from 5501 to 11000.
  • Node C contains hash slots from 11001 to 16383.

Adding or removing nodes redistribute the hash slots between the nodes without downtime.

In order to have high availability every master node must have at least one replica so in case it fails the replica continues to operate.

Using the redis benchmark tool shows how you can reach 1.85 million writes per second In cluster of 9 masters.


SeaData logo

SeaData makes lots of high scale projects so if you need help with redis or any database to consult or to build and manage such clusters don’t hesitate to contact us , we will be happy to assist.