Redis
master-slave replication
database synchronization
data replication
master update

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.

Practice system design

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

  1. 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.
bash
1   # On the current slave, run:
2   redis-cli SLAVEOF NO ONE
3
4   # Update all other instances to become slaves of the new master
5   redis-cli SLAVEOF <new-master-ip> <new-master-port>
  1. Manual Data Export and Import: Redis allows dumping data using the Redis DUMP command and importing it back with the Redis RESTORE command.
bash
1   # On the slave, dump the data to a file
2   redis-cli KEYS "*" | xargs -I '{}' redis-cli DUMP '{}' > dump.rdb
3
4   # Transfer dump.rdb to the master and import
5   redis-cli FLUSHALL
6   redis-cli --pipe < dump.rdb
  1. 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:

lua
1-- Example Lua Script
2local keys = redis.call('KEYS', '*')
3for i,k in ipairs(keys) do
4    local val = redis.call('DUMP', k)
5    redis.call('RESTORE', k, 0, val)
6end

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 FeatureDescription
Replication ModelMaster-Slave
Replication DirectionalityUnidirectional (Master to Slave)
Workaround for Slave to Master UpdatesPromote Slave or Manual Data Sync
Common Use CasesFailover Management, Data Redundancy, Scalability
Data Structures SupportedStrings, 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.