Write Loss in Synchronous write in Redis Cluser
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Redis, an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker, is widely distributed across multiple nodes in what's known as a Redis Cluster. It offers various advantages such as automatic partitioning and increased availability. However, one of the complexities it contends with is the phenomenon known as "write loss" occurring during synchronous writes.
Understanding Synchronous Writes in Redis Cluster
In a typical Redis setup, write operations are immediate and blocking, which means the server processes a write operation entirely before moving on to the next operation. But in the context of a Redis Cluster, which is designed to handle failures and provide continuous availability, writes have to be synchronized across multiple nodes to ensure consistency.
When a client writes data to the cluster, the node receiving the write (the master node for the specific shard handling the data) needs to replicate this operation across the slave nodes. Only after the replication acknowledgment from a majority of the nodes involved in the shard, the operation is acknowledged back to the client as a successful write.
Write Loss Scenarios
Write loss primarily occurs when there is a failure detecting whether all required nodes have applied a write operation. This can happen in several scenarios, such as:
- Network Partitions and Delays: If there is a network issue causing delays or partitions, the master node might not receive timely acknowledgments from the replica nodes even if they've received and applied the write.
- Node Failures: If a node crashes or is otherwise inaccessible after receiving the write command but before it can replicate it, the write could be lost.
- Race conditions: In scenarios where failovers (automatic promotion of a replica to a master) are involved, race conditions might lead to write losses if writes occur during the transition phase.
Technical Mechanisms to Mitigate Write Loss
Redis Cluster employs various mechanisms to reduce the risk of write loss:
- Synchronous Replication: Ensuring that each write is acknowledged by a majority of nodes before it is considered successful.
- Persistence: Use of RDB snapshots and AOF (append-only file) logs to ensure data is not entirely lost in case of failures.
- Failover mechanisms: Well-defined failover procedures that minimize the risk and impact of node failures.
Examples of Write Operations
Consider the following example to understand the synchronization:
Assuming this command is sent to a Redis Cluster having three master nodes each having two replicas, the cluster will handle it as follows:
- The command is sent to the master node responsible for the hash slot of 'key'.
- The master node writes the key locally and sends the write to its replicas.
- Once a majority (in this case, at least two nodes out of three) of the nodes in this shard (including the master) confirm the write, the master acknowledges the write back to the client.
Summary Table
Here's a table summarizing the key points about handling write operations in Redis Cluster:
| Factor | Detail | Impact on Write Loss |
| Network issues | Delays or partitions can delay replication acknowledgments. | Increases risk of write loss. |
| Node failures | Crashes or inaccessibility can prevent replication. | Critical writes might get lost if not yet replicated. |
| Race conditions | Failovers during writes can lead to discrepancies. | Potential write loss during node transitions. |
| Synchronization | Using majority acknowledgment ensures consistency. | Reduces the likelihood of write losses. |
Conclusion
Writing data in a Redis Cluster environment involves a trade-off between performance and reliability. Although mechanisms like synchronous replication and persistence are in place, the scenarios mentioned still pose a risk for write losses. Understanding these dynamics helps in designing more robust systems and choosing appropriate settings (like replication configurations and timeouts) to mitigate potential data losses in production environments.
Related reading
- Writing object as key in Gemfire Cache
- Xcode 5 and iOS 7 Architecture and Valid architectures
- Yarn Distributed cache, no mapper/reducer
- ZeroMQ Publish and Subscribe concurrently
- Write to two Kafka topics in a single transaction using Spring Kafka
- Writes on Cassandra Network Partitioned Nodes
- Zookeeper-Kafka and Consistent hashing
- ZooKeeper - clients co-ordination after one or more client lose connection with ZooKeeper

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.