Member-only story
How Database Replication Works
And why it is crucial for your data

Databases are an essential piece of almost every software product. A database is an organized place to store data. At some point, every company realizes that the amount of data they have increases over time. They cannot afford to lose all the accumulated information.
Making a regular backup is a good practice (and not only with databases). Generating a backup is a long process and it is not performed every minute. That being said, losing the user’s data gathered since the most recent backup is still a possibility.
This is where database replication comes into play.
The Idea
The general approach is similar to with a backup — to have copies of the data. The difference is in the how. Unlike with a backup, replication is performed on each data modification. This is a permanent process. It is not limited only by that purpose.
In fact, it is a full and working copy of the database. Therefore, it fits well into distributed systems where the components are placed on different computers. Each component can contain a database replica. That brings multiple solid advantages:
- The replicas can be distributed across the globe and be closer to the end-users. This leads to lower latency and happier customers.
- The read queries can be served faster. On average, the number of read-only queries is way higher than the number of write queries. This unloads the system and allocates the queries proportionally.
- Distributed systems are resilient and able to handle failures. If one of the parts of the system doesn’t respond, the request goes to another machine. If we try to fetch information from the database and it fails because of an internal error, the request will be redirected to another machine with a working database and continue.
The Implementation
While the idea is clear, the implementation brings some challenges. The database roles on each computer in a distributed system cannot be equal. Some databases will accept only write requests and others only read requests. The database performing a write operation is a Leader and the database performing a read-only…