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?
Other Common Redis Questions (and Answers)
- What is the default port used by Redis?
- How to start Redis server?
- Is Redis persistent?
- How fast is Redis?
- How to install Redis on Mac?
- How to check if Redis is running?
- How to restart Redis?
- Does Redis persist data?
- How to install Redis on Ubuntu?
- How to stop Redis server?
- How to see Redis version?
- Does Redis have tables?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost