Solomon Eseme
Posted on June 6, 2024
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.
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.
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:
- 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 distributes the traffic to the masters.
- 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.
- 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
- High Availability — Your system will remain available if your primary database fails.
- Improved Scalability- The traffic is distributed to the replicas, so the system can effectively handle a surge in traffic.
- 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.
- 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
- 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.
- 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.
- 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.
- Cost: Replication can require additional hardware and software licenses, increasing costs. Maintaining multiple copies of the data can also require additional storage and bandwidth.
- 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 → 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: 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.
Originally published at https://newsletter.masteringbackend.com.
Posted on June 6, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.