Understanding Database Replication

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

Hello “👋”

Welcome to another week, another opportunity to become a Great Backend Engineer.

Today’s issue is brought to you by Masteringbackend → A great resource for backend engineers. We offer next-level backend engineering training and exclusive resources.

Before we get down to the business of today. Part 10 of Understanding System Design.

I have a special request from you: You will love this one.

I have consistently written the newsletter weekly for the past 5 months, and I hope you enjoy it.

I aimed to grow this newsletter to 20k backend engineers this year, and I can’t achieve this without you.

Here’s how you can help and the reward for you.

I created a referral program that rewards you for the subscribers you refer to learn from this newsletter.

You will be rewarded with any Premium book from my store. To get your referral link, go to the homepage and use your email. Then, copy and share your referral link across your social media.

Here’s a short video demo:

Now, back to the business of today.

In the previous edition, 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.

A screenshot of Database Replication [Image Source]

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, and then the changes are synced to the other databases. A load balancer 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 in the case where 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, therefore, 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.

Next week, I will start exploring Database Sharding.

Don’t miss it. Share with a friend

Did you learn any new things from this newsletter this week? Please reply to this email and let me know. Feedback like this encourages me to keep going.

See you on Next Week.

Remember to get the  Masteringbackend → A great resource for backend engineers. We offer next-level backend engineering training and exclusive resources.

Top 5 Remote Backend Jobs this week

Here are the top 5 Backend Jobs you can apply to now.

👨‍💻 Profiq
✍️ Medior/Senior BE Developer - Global CDN platform
📍Remote
💰 Click on Apply for salary details
Click here to Apply for this role.

👨‍💻 Woltair
✍️ Software Engineer (Senior)
📍Remote, Prague
💰 Click on Apply for salary details
Click here to Apply for this role.

👨‍💻 Uncapped
✍️ Backend Engineer
📍Remote, Poland
💰 Click on Apply for salary details
Click here to Apply for this role.

👨‍💻 Stripe
✍️ Backend Engineer, Developer SDKs
📍Remote
💰 Click on Apply for salary details
Click here to Apply for this role.

Want more Remote Backend Jobs? Visit GetBackendJobs.com

Backend Engineering Resources

Whenever you're ready

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

1. The MB Platform: 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. The MB Academy:​ The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

3. MB Video-Based Courses: 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: 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.

LAST WORD 👋 

How am I doing?

I love hearing from readers, and I'm always looking for feedback. How am I doing with The Backend Weekly? Is there anything you'd like to see more or less of? Which aspects of the newsletter do you enjoy the most?

Hit reply and say hello - I'd love to hear from you!

Stay awesome,
Solomon

I moved my newsletter from Substack to Beehiiv, and it's been an amazing journey. Start yours here.

Join the conversation

or to participate.