Skip to content

Crack SDE

Most of the content are generated by AI, with human being reviewed, edited, and revised

Menu
  • Home
  • Daily English Story
  • Tech Interviews
  • Cloud Native
  • DevOps
  • Artificial Intelligence
Menu

Database Master -Slave Replication

Posted on 11/17/202311/19/2023 by user

Database master-slave replication is a common method for ensuring data redundancy, load balancing, and high availability in database management systems. Here’s a simplified overview of how it works:

  1. Configuration of Master and Slave Servers: Initially, the database servers are configured in a master-slave relationship. The master server is the primary database where all data changes (inserts, updates, deletes) occur. The slave servers are replicas that receive updates from the master.
  2. Data Changes on the Master: When changes are made to the data on the master server (e.g., through SQL statements), these changes are recorded in the master’s binary log. This log is a record of all transactions that have modified the data.
  3. Binary Log Transfer: The changes recorded in the master’s binary log are then sent to the slave servers. This can be done synchronously or asynchronously, depending on the configuration and requirements for data consistency and performance.
  4. Relay Log on Slave Servers: The slave servers receive the changes and store them in a relay log. The relay log on the slave is similar to the binary log on the master. It records the changes that are to be applied to the slave database.
  5. Applying Changes to Slaves: The slave server then reads the relay log and applies the changes to its database. This process is typically handled by a background thread known as the SQL thread.
  6. Maintaining Data Consistency: Throughout this process, mechanisms are in place to ensure data consistency between the master and slave databases. This includes maintaining the order of transactions and handling any conflicts or errors that may arise.
  7. Handling Failover and Recovery: In some setups, if the master server fails, one of the slave servers can be promoted to become the new master, ensuring continuity of operations. This process is known as failover.
  8. Read Load Balancing: Slave servers are often used to handle read queries to distribute the load and reduce the performance impact on the master server. This is especially beneficial in read-heavy environments.

The exact implementation details can vary based on the specific database system (like MySQL, PostgreSQL, etc.), but the overall process follows this pattern. It’s important to note that master-slave replication is primarily for data replication and not for scaling write operations, as all write operations still go through the master server.

MySQL implements master-slave replication using the following steps:

  1. Binary Log Configuration on the Master: In MySQL, the master server must have binary logging enabled. This is done by setting the log_bin configuration directive in the my.cnf or my.ini file. The binary log records all changes to the database (such as table creation operations or changes to data) in a form that can be read back and applied to another MySQL server.
  2. Position Tracking: Each event in the binary log is given a unique identifier known as the log position. This position is used by the slave to track which changes it has already applied.
  3. Creating a Data Snapshot: To initialize replication, a consistent snapshot of the master database is usually created and copied to the slave. This can be done using tools like mysqldump.
  4. Configuring the Slave: On the slave server, you need to configure the connection information for the master server (including hostname, log file name, and log position). This is done using the CHANGE MASTER TO statement in MySQL. The slave also needs to have a unique server ID.
  5. Starting Replication: After the slave has been configured and has the snapshot of the master’s data, replication can be started using the START SLAVE statement. The slave will connect to the master and request binary log contents starting at the specified log position.
  6. Applying Changes on the Slave: The slave server copies events from the master’s binary log and stores them in its own relay log. Then, a separate thread on the slave (the SQL thread) reads the relay log and applies the changes just as they were applied on the master.
  7. Error Handling and Skip Functionality: MySQL provides ways to handle errors during replication, such as skipping a problematic event that cannot be applied on the slave.
  8. Monitoring and Maintenance: The status of replication can be monitored using commands like SHOW SLAVE STATUS. This is important for ensuring that replication is working correctly and for troubleshooting any issues.
  9. Failover and Recovery: In case the master server fails, one of the slaves can be promoted to be the new master. This requires reconfiguring the other slaves to replicate from the new master.
  10. Read-Only Slaves: It’s a common practice to set slave servers to be read-only to prevent accidental data modifications on them. This is not mandatory but helps maintain the integrity of the replication setup.

MySQL’s replication is asynchronous by default, meaning the master does not wait for the slave to confirm that it has received and processed an event before moving on to the next one. This can lead to a small lag between the data on the master and the data on the slave. There are options for semi-synchronous replication, which can provide stronger data consistency guarantees at the cost of some performance overhead.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related

Recent Posts

  • LC#622 Design Circular Queue
  • Started with OpenTelemetry in Go
  • How Prometheus scrap works, and how to find the target node and get the metrics files
  • How to collect metrics of container, pods, node and cluster in k8s?
  • LC#200 island problem

Recent Comments

  1. another user on A Journey of Resilience

Archives

  • May 2025
  • April 2025
  • February 2025
  • July 2024
  • April 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Cloud Native
  • Daily English Story
  • Database
  • DevOps
  • Golang
  • Java
  • Leetcode
  • Startups
  • Tech Interviews
©2025 Crack SDE | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}