Introducing Dragonfly Cloud! Learn More

Question: Can I write to a slave in a Redis cluster?

Answer

In a standard configuration of a Redis cluster, all writes need to be made to the primary (master) nodes. This is because Redis uses a master-slave replication model where the data is consistently replicated from master nodes to slave nodes for high availability and failover purposes.

Although it's technically possible to write directly to a slave node, doing so is highly discouraged and generally not recommended. The reason is that any data written directly to a slave will be overwritten during the next synchronization with its master.

Let's take an example:

import redis # Suppose 'localhost' is a slave node. r = redis.Redis(host='localhost', port=6379, db=0) r.set('foo', 'bar') # This might be successful. print(r.get('foo')) # This might return 'bar'.

Even if you successfully write to a slave node and retrieve the value as shown above, this change won't propagate to other nodes in the cluster. Once the slave synchronizes with its master, your changes will be lost.

The idea behind this architecture is to maintain consistent data across the entire cluster. In case the master goes down, a slave can take over without any data loss, thereby providing fault tolerance and high availability.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.