Understanding Database Replication
Tags: backend, webdev, beginners, tutorials
Published: 2024-06-06 09:34:52

---
title: Understanding Database Replication
published: true
date: 2024-06-05 11:49:48 UTC
tags: backend,webdev,beginners,tutorials
canonical_url: https://newsletter.masteringbackend.com/p/understanding-database-replication
---

_Today’s issue is brought to you by_ [Masteringbackend](https://masteringbackend.com?ref=backend-weekly&utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication) **_→ A great resource for backend engineers. We offer next-level backend engineering training and exclusive resources._**

[In the previous edition](https://newsletter.masteringbackend.com/p/acid-compliance-in-relational-databases), I discussed ACID Compliance in Databases and explored why it’s important, especially in relational databases.

In this episode, I will explain Database Replication, how it works, and the types, advantages, and disadvantages of database replication.

#### What is Database Replication?

A database is one of the most important components of your System Design, and it’s a data store for all your business records.

Imagine if you have only one database server, and it goes down.

Let’s imagine you are building an E-commerce store like Amazon. The store initially has a single database to create, read, and modify data. Everything is perfect. The business grows after a couple of years, and you have millions of customers.

If that one database was to be deleted by mistake or if the server goes down at some point. Your entire business is crumbled.

This can easily be mitigated by having secondary databases that take over whenever the primary database is down. Additionally, all the databases can work together to serve requests.

That’s why Database Replica is a crucial strategy for your database management.

**Database replication** is copying data from one database on a server to one or more replica databases on other servers. The databases then become synced.

![](https://cdn-images-1.medium.com/max/1024/0*nJpEZzyscLEztRuX)

#### **How does database replication work?**

Database replication is supported in many database systems, usually with the master/slave relationship. The master or primary database syncs data to the slave/secondary databases. A popular configuration is the master processing data-modifying operations like insert, update, and deletes.

The master then syncs the data to the slaves, which supports data read operations since applications require a much higher ratio of reads to writes; thus, the number of slaves should always be higher to scale and process most of the requests.

In this setup, if the master database goes offline, a slave database will be promoted as the new master to process data modifying requests and sync data to the slaves. However, in production, promoting a new master can be complicated as the slave data might not be up to date.

The missing data needs to be updated by running recovery scripts. If only a single slave database is available and it goes down, the read operations will be directed to the master database. Immediately after the issue is found, a new slave database will replace the old one, and data will be synced.

If our architecture has multiple slave databases, read operations will be directed to the healthy ones.

#### **Types of database replication**

We just described a master-slave replication in which one database is designated as the master and others as slaves.

The master receives all the write operations, whereas the slaves handle the read operations. Other types of database replication are:

1. Multi-Master replication: In this setup, we have more than one master database and one or more slaves. The masters receive write operations, then the changes are synced to the other databases. A [load balancer](https://masteringbackend.com/hubs/system-design/load-balancers?utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication) distributes the traffic to the masters.
2. Master-Master(Peer-to-peer) replication: All the databases acts as both a master and a slave, the data is replicated across all nodes in a peer-to-peer fashion.
3. One-Way Replication: In this type of replication, data is replicated from a master database to one or more slave databases in one direction only. This is useful for backup and reporting purposes. It is also known as data mirroring, where complete backups are maintained when the primary database fails. Mirrors act as hot standby databases.

#### **Advantages of database replication**

1. **High Availability — ** Your system will remain available if your primary database fails.
2. **Improved Scalability-** The traffic is distributed to the replicas, so the system can effectively handle a surge in traffic.
3. **Lower latency-** Database replication can improve latency by reducing the time it takes to access data. When data is replicated to multiple databases, it can be accessed from a database geographically closer to the user, reducing the time it takes to access the data.
4. **Disaster recovery:** Database replication can create a disaster recovery site in a separate location. In a disaster, the replicated data can restore operations quickly.

#### **Disadvantages of database replication**

1. **Complexity:** Database replication can be complex to set up and maintain, especially for multi-master replication scenarios. Configuration, management, and monitoring can require additional resources and expertise.
2. **Consistency:** With replication, there is a risk of inconsistent data if updates are made to different database copies simultaneously. This can lead to conflicts that need to be resolved.
3. **Performance impact:** Replication can have a performance impact on the database, especially during heavy write operations. This is because each update needs to be propagated to all the replicas, which can cause delays and increase system load.
4. **Cost:** Replication can require additional hardware and software licenses, increasing costs. Maintaining multiple copies of the data can also require additional storage and bandwidth.
5. **Security:** Replication can increase the risk of data breaches or other security issues. Each copy of the data is a potential target for attacks, so additional security measures may be required to protect the replicated databases.

Database replication aims to provide redundancy and fault tolerance in case of hardware or software failure.

It also improves application performance by reducing the load on the primary database server and distributing read requests across multiple replicas, improving your application performance, scalability, and availability.

Additionally, replication can be used for data mirroring, where complete backups are maintained when the primary database fails.

That will be all for this week. I like to keep this newsletter short.

Today, I discussed Database Replication, how it works, and the types, advantages, and disadvantages of database replication.

Remember to get the [Masteringbackend](https://masteringbackend.com?ref=backend-weekly&utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication) **_→ A great resource for backend engineers. We offer next-level backend engineering training and exclusive resources._**

**There are 4 ways I can help you become a great backend engineer:**

**1.** [**The MB Platform:**](https://app.masteringbackend.com?utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication) Join 1000+ backend engineers learning backend engineering on the MB platform. Build real-world backend projects, track your learnings and set schedules, learn from expert-vetted courses and roadmaps, and solve backend engineering tasks, exercises, and challenges.

**2.** [**​**](https://click.convertkit-mail4.com/4zuplwzlo3aeh5lkndnhxh3d3ml77/6qhehou7nrnkwlbo/aHR0cHM6Ly93d3cuanVzdGlud2Vsc2gubWUvdGhlLW9wZXJhdGluZy1zeXN0ZW0tZ3Jvdy1tb25ldGl6ZS15b3VyLWxpbmtlZGlu?utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication) [**The MB Academy:​**](https://masteringbackend.com/academy?utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication)The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

**3.** [**MB Video-Based Courses:**](https://app.masteringbackend.com?utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication) Join 1000+ backend engineers who learn from our meticulously crafted courses designed to empower you with the knowledge and skills you need to excel in backend development.

**4.** [**GetBackendJobs:**](https://getbackendjobs.com?ref=backend-weekly&utm_source=newsletter.masteringbackend.com&utm_medium=referral&utm_campaign=understanding-database-replication) Access 1000+ tailored backend engineering jobs, manage and track all your job applications, create a job streak, and never miss applying. Lastly, you can hire backend engineers anywhere in the world.

_Originally published at_ [_https://newsletter.masteringbackend.com_](https://newsletter.masteringbackend.com/p/understanding-database-replication)_._

* * *