Distributed Systems
Data Synchronization
Master-Slave Architecture
Node Data Management
Network Administration

How to sync data between master and slave node in distributed systems

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In distributed systems, syncing data between a master and slave node is crucial for redundancy, fault tolerance, and load balancing. This process, often central to the architecture of databases and application systems, must be both efficient and reliable. We will delve into the methods and technologies commonly used for data synchronization, along with providing a technical explanation and examples.

Understanding Master-Slave Architecture

In a master-slave architecture, the master node holds the primary copy of data, handling write operations and updates. The slave nodes usually support read operations and backup, ensuring data redundancy and availability. They periodically sync data from the master to keep up-to-date.

Methods of Data Synchronization

There are several methods to sync data from the master node to one or more slave nodes. Each method has its use cases, advantages, and disadvantages.

  1. Snapshot Replication:
    • A full copy of data from the master node is taken at a specific point in time and copied to the slave nodes.
    • Useful for initializing the slave nodes or restoring data.
  2. Transaction Log Replication:
    • Also known as log shipping, this technique involves copying the transaction log from the master node to the slave nodes.
    • Slaves replay these transactions to maintain a state consistent with the master.
  3. Trigger-Based Replication:
    • Triggers on the master database detect changes (inserts, updates, deletes) and log these changes, which are then synchronized to the slave.
    • Provides a near real-time data synchronization but can add overhead to the master.

Data Synchronization Technologies

Several technologies facilitate the synchronization between master and slave nodes:

  • MySQL Replication: Using binary logging and replication threads.
  • PostgreSQL Replication: Offers several modes such as streaming replication or logical replication.
  • MongoDB's Replication: Utilizes oplog (operations log) for syncing across nodes in a replica set.
  • Redis Replication: Redis uses asynchronous replication where slaves periodically sync with the master.

Example of Setting Up Replication

Here’s a brief example of setting up replication in MySQL:

On the Master:

  1. Configure the MySQL master to log changes:
sql
   [mysqld]
   log-bin=mysql-bin
   server-id=1
  1. Restart MySQL service and create a user for replication:
sql
   CREATE USER 'replicator'@'%' IDENTIFIED BY 'password';
   GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';

On the Slave:

  1. Configure the MySQL slave with the master information:
sql
   [mysqld]
   server-id=2
   relay-log=mysql-relay-bin
  1. Point the slave to the master and start the replication:
sql
   CHANGE MASTER TO MASTER_HOST='master_ip', MASTER_USER='replicator', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 107;
   START SLAVE;

Now, the slave will sync data from the master based on the binary logs.

Best Practices and Considerations

Implementing a robust data sync strategy requires consideration of several aspects:

  • Data Consistency: Confirm that data integrity is maintained after replication.
  • Performance Impact: Recognize that sync mechanisms might affect the performance of the master.
  • Failover Procedures: Ensure there are clear procedures in place for failover to a slave if the master fails.

Summary Table

Here is a summary of key synchronization methods and their characteristics:

MethodDescriptionUse Case
Snapshot ReplicationFull copy from master to slave at set intervalsInitial sync, Data restore
Transaction Log ReplicationReplicate changes via transaction logsContinuous sync
Trigger-Based ReplicationTriggers detect and log changesNear real-time sync

Conclusion

Synchronizing data between master and slave nodes is fundamental in distributed systems for data reliability and accessibility. The correct choice of synchronization method depends on specific requirements for latency, data volume, and system resources. With proper setup and maintenance, data replication can significantly enhance the resilience and efficiency of distributed architectures.


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.