What is master slave replication in database replication?

Mondo Technology Updated on 2024-01-28

In database replication, we create a copy (replica) of the same database across multiple nodes. However, with multiple replicas, a question arises: how do you ensure that all data ends up on all replicas after each write? The most common solution is master-slave replication, also known as:Active and passiveorSingle-leader replication.

There are two types of nodes for master-slave replication: master and sl**e. SingleLord (leader).The node acts as the primary database, while one or moreFrom (followerThe node maintains a copy of the master data.

The master node is responsible for handling write queries, while the slave node is responsible for handling read queries. Note: The master node can also perform read queries if required.

Whenever a write operation is performed on the master node, it is replicated to all slave nodes to keep the data consistent throughout the system.

When a client wants to execute a write query, they send the request to the leader, who first writes the new data to their local storage.

Whenever a leader writes new data to their local storage, it also sends the data changes to all of its followers as part of the replication log. Each follower takes the log from the leader and updates its local copy by applying all the writes in the same order that they are processed on the leader.

If only one slave is available, and that slave is offline, the read operation is temporarily directed to the master and the new slave replaces the old one.

If more than one slave is available, reads are redirected to other healthy slaves, and the new database server replaces the old one.

If the primary database goes offline, the slave database is promoted to the new primary database. All writes will now be performed on the new primary, and the new slave will immediately replace the old database for data replication. Note:Upgrading a new primary database is more complex because the data from the database may not be up to date. How do I update missing data? We'll discuss this idea later in this blog.

Master-slave replication is used when read workloads are large and read requests need to be distributed across multiple nodes to improve system performance. Because slave nodes can handle read requests, they can offload read traffic from the master node and allow it to focus on processing write requests.

This architecture provides fault tolerance so that the system can continue to operate even if the primary node fails.

Master-slave replication is used for relational databases such as MySQL, PostgreSQL, and Oracle, as well as NoSQL databases such as MongoDB, Cassandra, RethinkDB, and Espresso.

Leader-based replication isn't limited to databases. It is also used by distributed messaging such as Kafka and RabbitMQ to provide highly available queues.

List of high-quality authors

Related Pages