Redis how to update master from slave?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction to Redis
Redis, an abbreviation for Remote Dictionary Server, is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, geospatial indexes, and streams. Its versatility and speed make it a popular choice for many applications that require high performance and low latency.
Redis Replication Overview
Redis supports a master-slave replication model. In this setup, one server acts as the master and others as slaves. The slaves are exact copies of the master and can be used to scale read operations across multiple servers and ensure data redundancy.
When data on the master server is updated, the changes are automatically replicated to the slave servers. However, there might be scenarios where data on a slave needs to update back to the master, which is typically not supported directly in Redis due to its replication architecture designed for unidirectional synchronization.
Technical Explanation: Updating Master from Slave
Understanding the Limitations
By default, Redis is designed to only replicate data from master to slaves. This means in scenarios where a slave's data is desired to be written back to a master—perhaps when a slave has been isolated and received write operations not immediately reflected in the master—a workaround must be implemented.
Workaround Approaches
- Promoting a Slave to Master: In scenarios where the slave has the most recent data due to network partition or other issues leading to a split-brain syndrome, promoting the slave to be the new master is a viable solution.
- Manual Data Export and Import: Redis allows dumping data using the Redis DUMP command and importing it back with the Redis RESTORE command.
- Custom Script: Using Lua scripts or external scripts in programming languages like Python can facilitate copying data from a slave node back to the master. This method might involve iterating over keys or datasets and pushing changes programmatically.
Using lua Script:
Best Practices and Considerations
- Consistency and Data Integrity: Ensuring data consistency across instances when transferring data back from slaves to master is crucial. Implement integrity checks during manual operations.
- Handling Conflicts: Anticipate and design a strategy for resolving data conflicts that might arise when a slave has diverged from the master.
- Regular Backups: Establish a routine for creating regular database snapshots to safeguard against unexpected data loss.
- Monitoring and Alerts: Employ robust monitoring to alert administrators when there is abnormal replication lag or suspected network partitions that could lead to inconsistencies.
Summary Table
| Key Feature | Description |
| Replication Model | Master-Slave |
| Replication Directionality | Unidirectional (Master to Slave) |
| Workaround for Slave to Master Updates | Promote Slave or Manual Data Sync |
| Common Use Cases | Failover Management, Data Redundancy, Scalability |
| Data Structures Supported | Strings, Hashes, Lists, Sets, etc. |
Conclusion
While Redis inherently supports a one-way replication model, understanding the architecture and implementing strategic workarounds can facilitate scenarios where updates from a slave may need to reflect in the master. Adopting best practices, monitoring systems, and regularly testing failover and recovery procedures ensures a robust Redis deployment.
Related reading
- Redis or Ehcache?
- Redis replication chain of slavesreplicas when intermediate replica crashes
- Regarding Apache nifi - Distrubuted Cache
- Relationship between primary-backup and state machine replication
- Redis is single-threaded, then how does it do concurrent I/O?
- Redis master/slave setup on Kubernetes throwing error BRPOPLPUSH ReplyError MOVED 2651
- Redux How to unit test an async action that dispatches another async action
- Ref in async Task

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.