Database Replication 101: A Beginner's Guide to Building High-Availability Systems
Ria Bhatia31:542023
Your upload was confirmed, yet a replica says your video does not exist
A user uploads a video, gets a confirmation, refreshes seconds later, and is told the video is not there, because the read landed on a follower database that has not caught up. Software engineer Ria Bhatia, who has evaluated database systems for performance, durability and scalability in fintech, shows why replication is far more than a backup. Which replication design, and which consistency trade-off, actually fits your system?
Learn how leader, multi-leader and leaderless replication handle lag, conflicts and failover
Software engineers Ria Bhatia and Tanya Sneh explain why database replication is more than a backup: it cuts latency, keeps systems running through failures and scales reads. They compare single-leader, multi-leader and leaderless replication, demonstrate asynchronous MySQL replication, and show how to handle replication lag and write conflicts before choosing an approach.
“So no, data replication is not just a backup. It is important to keep data geographically closer to your users.”
“So in reality, there's no one-size-fits-all approach to choosing a synchronization approach for single master database.”
What you will learn
Explain the three main benefits of replication: lower latency, fault tolerance and read scaling.
Compare asynchronous and synchronous single-leader replication and the trade-off between write speed, stale reads and availability.
Distinguish single-leader, multi-leader and leaderless replication and when each suits a workload.
Apply read-your-writes and monotonic read consistency to reduce the user impact of replication lag.
Choose a conflict strategy such as last write wins or manual resolution, and plan monitoring, backups and testing.
Key takeaways from the speaker
Understanding the purpose of database replication and its benefits, including improved performance and reduced downtime
Will serve as a stepping stone in making better design decisions and effectively conveying them, even if you lack prior experience in the problem domain
Details
Level
Beginner
Format
Tech talk
Technical depth
Practitioner
For
Student, Early career, Mid-career
Length
31:54
Published
June 2023
Questions this talk answers
Is database replication just a backup?
No. Replicas keep data close to users to cut latency, let the system keep working when a server fails, and spread read traffic across machines.
What is the difference between asynchronous and synchronous replication?
Asynchronous replication acknowledges writes after the leader updates, so writes are fast but followers can serve stale data and unreplicated writes can be lost. Synchronous replication waits for followers, so writes are slow and a down follower can stall the system; many systems combine both.
When should you use multi-leader replication?
When a system is write-heavy or geographically distributed, so clients can write to a nearby leader instead of overloading one distant leader. The cost is potential write conflicts between leaders.
How can you limit the effects of replication lag?
Use read-your-writes consistency, sending a user's reads to the leader for a short window after they write, and monotonic read consistency, pinning a user's reads to one replica so values do not appear to go back in time.
What should you consider before choosing a replication approach?
Consider workload volume and change frequency, consistency requirements and system architecture, then implement with monitoring and alerting, backup and recovery, and thorough testing.
About this talk
In today's fast-paced business environment, ensuring the availability and consistency of data is crucial for organizations of all sizes. One of the most effective ways to achieve this is through the use of database replication. This talk will provide an overview of the different types of replication available, including master-slave, master-master, and multi-master replication. It will also discuss the benefits and trade-offs of each approach, and elaborate strategies for dealing with issues such as replication lag and conflict resolution. Attendees will leave with a good understanding of how database replication can be used to protect their systems and ensure availability.
Hello, hi everyone. Thank you for coming and attending this talk. Firstly, we would like to thank the Women Tech Network team for giving us this wonderful opportunity. We are very excited to talk about data replication, share our learnings with you all, and hope this session is useful. Before we get to the main presentation, let us quickly introduce ourselves. Starting with me. Hi, everybody. I'm Rhea Bhatia, and I'm currently working as a software engineer in the fintech industry. Over the course of my career, I've worked with multiple database systems, databases on cloud, on-prem, MySQL, NoSQL, and have had a wonderful time learning new technologies and implementing and trying that with different database systems one thing which i really like about learning and which makes learning even more exciting for me is understanding
the whys and hows behind all these things apart from this i'm also very passionate about organizing mentorship and outreach initiatives for women in stem and giving back to the community in whatever way possible when i'm not coding or working i love to play musical instruments like the ukulele and harmonica and i also like reading books over to tanya hi everyone i'm tanya i also work as a software engineer in a financial services firm and i've worked on increasing the performance of database systems both on cloud and on prem apart from this i keep on learning about diverse fields in technology and have previously dabbled in the fields of robotics and game theory.
I am passionate about increasing diversity in tech and when I'm not working, you can find me either fainting, reading or planning my next trip. Over to you, Rhea. Okay. While we thought that out, I will quickly go over what we're going to cover in this session. We will dive deep in today's replication techniques and also witness best practices to keep in mind when doing so.
I hope by the end of this presentation, so without further without further delay let's get started first things first what is database replication as i mentioned in my introduction the why and what and that makes learning even more fun so let's start with the what Firstly, we need to understand why you are scrolling through all the time.
The problem with this however is that this data is constantly changing. Firstly, let's talk about why this data matters. This data matters because this is crucial to unlock the insights into customer behavior. Dislikes, your purchasing habits, what top you like on your pizza, all this earlier is that this data is constantly changing and hard to what really happens is that so no matter the full overlooked is the ability and consistency which you focus on and you are constantly generating data in the form of custom coming in place and your team is sitting and making decisions based on that data there is a hyper data from different resources and you're already left behind most of your competitors.
So to avoid this data data replication me copy of data on it's like having a backup dancer in case your main dancer fails to perform. So it's like having somebody who's always there, that data thing goes wrong. And it's all sync with the original database. That is the primary database. And this is called the replica. Thank you.
Okay, while Rhea joins back, I'll share this video. Yes, I apologize for the network issues continuing if data replication is just a backup. So no, data replication is not just a backup. It is important to keep data geographically closer to your users. Now, what that means is, suppose you are trying to connect, taking Netflix example, you're trying to connect to a server from the Americas to UK, you would obviously experience some buffering issues because that request is taking that much time to travel.
no matter how efficient your query or your database is, that request, the time it'll take to leave your system, go fetch the data and come back, you can't reduce that. You can only reduce that if that server is located closer to your location. And that is how we reduce the latency. Having different replicas in multiple regions, one can connect to the closest one and can easily have a low buffering experience and a better experience with respect to video streaming this is this was just netflix example and many such businesses use that keeping data geographically closer to your users next is it allows your system to continue working even if some parts have failed now you have an application you're connected to a server and suppose something bad happens
to that server that server catches fire what happens in this case of course you might have a backup for that but even that backup would take some time to get restored and it won't have the latest data why because like you you are keeping that data separately and taking that backup from time to time but the latest changes are lost because they were just written to that server having a replica instead solves the issue if you have a replica with the same set of changes which gets replicated as and when the leader is updated or when the primary database is updated the request can directly be handled by that particular server while you solve the fire situation so your system continues to work even if some parts have failed thirdly it allows us to scale the number of machines
for especially for read intensive applications now right now you have one node and certain set of people are connected to it as the number of people grow as the number of users on your application grow you would need multiple nodes to serve those read requests and putting that pressure on just one database could be disastrous for your system so having multiple queries allows you read replicas allows you to send those requests and handle multiple requests without putting pressure or load on just one because all of them have the same data and are already in sync with this we talked about the why and what of data replication talking about the techniques over to tanya yeah so having understood how replication can help us let us see what are the types and
techniques of replication that are available to choose from the first technique is called the single leader replication this is also called the master slave replication here the leader can be thought of as the team who is delegating instructions to all of the followers in this architecture while all databases can cater to leads from the clients only the leader can serve a right request when it receives an instruction from the application it makes changes to its own copy of the database and then it forwards the same instruction to its followers who can apply these these changes to their own respective copies this raises an important question when does the application actually happens. So the leader can go about replicating in two ways. First, let us look at asynchronous replication. We can understand this by looking at an example of what happens when
we receive a transaction. So in this example, the leader received a transaction to write x is equal to 5. It will respond back to the client by making changes to itself and then at a later point in time send the instruction to all of its followers this makes the system eventually consistent what this means is that eventually all of the databases will have the same copy of the data but the client cannot say so with priority immediately after receiving back an acknowledgement so what does this architecture mean for the client well next time well one can expect the right to be very fast because we just wait for it to happen on the leaders.
But because there could be a lag in the data reflecting correctly in the followers, the client can end up making stale reads. Also, if the leader goes down before having replicated the transactions, it can lead to a potential data loss situation. So proper failover mechanisms need to be in place to handle such situations. In order to avoid some of the pitfalls of asynchronous replication, the leader can also choose to replicate synchronously.
In this case, when the leader receives the transaction, it will first make sure that all of the followers have performed the same before sending back an acknowledgement to the client. Now, right off the bat, we can see that this will make the writes very slow because now all of the databases need to perform the operation. Also, if the leader keeps on waiting for a follower database that has gone down to actually write the value, we can potentially end up stalling the system altogether and the system becomes not available. Again, this can be handled by proper care. So in reality, there's no one-size-fits-all approach to choosing a synchronization approach for single master database. So what happens is that we usually end up using a combination of these two approaches. Some databases are kept
in sync with the leaders, whereas the other databases are still replicating asynchronously. This will help us get the best of both the worlds. But does that solve all of our problems? Well, not really. If your system is write-heavy, then you are sending all of the writes to one particular leader and increasing the workload tremendously on the same. Also, if your system is geographically distributed and you just have one leader then clients all over the world will be trying to contact this leader in order to perform a ride and for a client that is located geographically far away from that leader the network induced latency will be huge and the ride becomes slow the multi-leader approach can solve these issues so in this architecture there can be multiple
leaders that are potentially located geographically farther apart then the client can connect to a leader that is closest to it in order to perform the right operation this right will then be replicated to all of the followers of this leader and it will also be replicated to other leaders across the world this helps us maintain a consistent state of the data across the globe so with multi-leader replication we distributed the right workload amongst various databases but the situation is not all roses with multiple databases undergoing right there could be a situation of conflict generation we will look at it later and decide and figure out what it means and how it can be handled okay yet another approach of replication is the leaderless replication in the leaderless replication there is no follower or leader all of the databases are
capable of performing both read and write operations so what happens is that when a client has to perform a write it sends the instruction to all of the databases and waits for acknowledgement from some of them for instance in this example the client sends the right operation to all the five nodes and is waiting for an acknowledgement back from three of the databases a similar thing needs to be done when we are to perform read operation as well this is also called forum establishment and it is an advanced concept that is out the scope of this presentation.
So for people who are interested in DLH applications, they should definitely check out forums. With this, I hand over to Rhea to demonstrate database replication. Hi, thank you so much, Tanya. So now let's get to the fun part and see database replication live in action. We will see this using a demo. So what we have done over here is that we have started two MySQL servers on different ports.
So you see this one is the master one and this one is the slave. If you see that we have mentioned that- - Rhea, sorry to break you. You've only shared the Chrome tab. Can we share that actually? - Yes, sure. Okay, is it visible now? Yeah, thank you, Tanya. Okay, so this is our leader or the master, and this is the slave or the follower. So, what we have done is that we have started two MySQL servers on two different ports. MySQL does its replication in asynchronous way, leader to slave. So, that is what we will be demonstrating today. Now, let me show you that we have started. Is the next screen visible?
rate so you can see that i've started the master on this port that is 3307 and the server the slave on 3308 let's see the cons the current state of our databases so if i do show me select this this is a table which i've already created in my uh particular database and if i want to see what is in this table let me fire a select statement and i see okay it has id1 and first name as tanya similarly let's check this for our slave okay right now everything is in sync now let me create another record uh for my table and let's add it and let's see if things get replicated what i'm basically doing is that i'm writing to the leader and i will be reading from the follower so let's
insert into test replication that's the name of my table values and this time i would be sending it my name okay so let's check the state of our leader once if everything got inserted great now let's check the state of our replicator model see we have our record over here also So if we make some changes to the leader, those are getting replicated to our slave, that is our follower in this case.
So this is how replication works and you can take the advantage of setting backups or keeping your system geographically closed, all the benefits which we have discussed till now. One thing to note here is that till now we have discussed about all the wonderful things of data replication. We have seen how that in action as well, how database replication is happening for MySQL.
As I mentioned, it was asynchronous. But is everything perfect? Of course not. When you're designing large scale applications, a lot can go wrong. There are so many issues when dealing with database replication, which need to be addressed. rest and at the same time there are some trade-offs which you need to choose that what fits for your system in that place and at that time. So with this let's talk about the two most common issues which one can face with database replication. One is replication lag. So till now we have seen that there's this leader and there's the follower. We do our rights to the leader and we are reading from the follower.
That is a leader follower based or master slave based replication model. Now this helps us to keep many number of leaders and scale the number of readers so that in case your application is read heavy, the readers can handle all those massive read requests for you. Having a synchronous architecture here won't suit us because that kills the sense of replication.
We want that we are able to read from the followers without being dependent on the leader for first replicating the data and then sending it back to the client. Asynchronous makes sense here that we accept the rights, we replicate it to the followers and they are in sync as in when that happens. The problem with this is that there could be a lag between the leader and the follower. By lag and mean there could be a delay by the leader side to replicate the followers and putting all the data in the followers so let's understand this with a quick example suppose you're all set and ready to upload your new video to your favorite social media app and you send your video to the leader the leader says okay i have accepted your
video and your video is live now in the background async replication is happening and it's updating its followers but within a couple of seconds you check again you check your video again and this time your read request gets connected to the follower till now the follower hasn't really caught up with the leader and it's still running behind it still doesn't have the latest changes so the follower tells you that no your video is not there this is a potential reflection replication lag scenario where despite receiving the confirmation from the client you don't see things which you have committed already because you are connected to a different instance which is not really in sync in theory replication lag can take from a few seconds in theory actually
there's no time limit to how much this delay would be because it could be because of any issue because of network issues in between or one node is working on full capacity and it's taking some time to replicate the changes so from a few seconds to many minutes there's no limit to how much replication lag you could experience another thing which you can do here is what you are ready to trade off maybe as a client i want to see the information of my latest updates but your friend could see that same information 30 seconds later it won't matter right i mean again depending upon your business model there is no right or wrong here we are discussing a social media scenario over here so for that one solution could be read your right consistency what's happening over here
is that if a user creates a post and then it tries to read that post after in a short period of time then all the reads would be directed to the leader only now again this kills the sense of replication if always all the reads are being handled by the leader if you're making multiple changes and then all the reads are being handled by the leader then what is the point of having read replicas what we can do instead that for like a short time span like 10 seconds maybe all the readers all the reads would be directed to the leader post you create a post you upload something and other reads could be taken care by the follower because they are not linked to this
post until the time async replication catches up even and that time has passed this follower would already be up to date with the leader for other users again the right of course would be handled by the leader because that's our architecture over here and they would also continue to read from the follower in case they haven't created any post but your post they might see it after some delay if that suits your architecture well and good but there is no right or wrong over here another problem with replication lag over here is that suppose your leader the current state of a value x is current is changing continuously from x is equal to 1 x is equal to 2 x is equal to 3 that is that data is in your leader and in the background it is getting replicated to your
followers now a user tries to read the latest value of x right now for him the value of x is 2 but like in a couple of seconds he sends another read request and this time value of x is 1 imagine that you're reading your blog and comments appear then disappear then appear then disappear something is happening something weird is happening with every refresh so this is not a very good experience for any user and you are reading data from the past the solution for this is is to make the reads stick to just one replica like for again for a certain period of time till the replication catches up the reads would always be directed to that particular follower so that no uh they do not
experience this traveling back in time where they are reading faulty or stale values or they are not sure what the actual value is. Again, for user two, if they have read this, it will be sticky to this follower only and they would, the read request would again be directed to this particular follower only despite refreshing. So this is called monotonic read consistency. Let's also discuss another issue, another common issue with database replication that is dealing with conflicts now as Tanya mentioned in while discussing multi-leader architecture that this is a common issue which one can face let's understand what that exactly is so you have two leaders in different geographical locations and both these leaders are capable of taking in the rights at one location your colleague changes something in the database value for customer a
suppose uh that customer is address is updated by this person and in region one and similarly your colleague in region two updates that record for the same customer but this time they might change the phone number now they have done that they have committed the leader has said okay well and good when things get replicated across leaders this is a potential conflict scenario because we don't know which is the correct value the the there's possibility that your application is not like up to your application is not in a state to handle the merge conflicts or it could be anything like how exactly in that scenario which is the correct record that is difficult to figure out and this is a conflict scenario so one possible solution for this is
last right wins what that means is that one of the colleagues they both have have a certain time difference in between their commits to the database and the last right the one which did the latest one wins the problem with this however is for example this person did that at one second and this did that at one point a few milliseconds the last right one would be this one and this person would feel that their rights despite being committed despite getting the acknowledgement from the client have gotten lost another solution for this is manual conflict resolution what that means is that we leave it up to our application code to decide which one is correct what what our application could do is since it was performed on two different values of the same record it could
merge it maybe or it could show both the values to the client and the client can decide which one the correct one or another scenario would be that it creates a marker object in our database and later on somebody from the team could decide which one is the correct one this way we prevent data loss and this requires manual intervention there are some of the practices which one needs to follow uh when discussing when implementing a successful replication over to tanya for that yeah so with all of the replication techniques that are in our arsenal it is very important that you carefully choose what needs to be done so there are some factors that you need to consider first and foremost comes the workload how much data are you generating and how frequently is
it changing this will determine the replication frequency of your system and the method that you choose you also need to consider consistency requirements does your system need to be strongly consistent across replicas at all times. If that is the case, then you need to choose something that guarantees strong consistency. Also, system architecture becomes... Is your system distributed? Are you using a cloud-based or an on-premise system? All these factors will impact your replication approach as these will determine any sort of network latency that you might face.
now once you have selected a replication method it is time to implement it properly in the best way possible there are some best practices that you need to keep in mind the first one is monitoring and alerting monitoring the replication process is critical to detecting any fault in your replicas as soon as possible setting up alerts will notify you when a replica is down or when there are any replication errors or discrepancies and it will allow you to take action before something bigger happens right next comes backup and recovery this should be top of your mind your application process is only as good as your ability to recover from a failure so you should have a robust backup and recovery plan to ensure that your data is protected and recoverable in case of a failure
Then comes testing and validation. Before you actually go ahead and roll out your replication strategy, you should make sure to test and validate it thoroughly. This will identify any potential issues beforehand and ensure that your replication process is running smoothly in production. With that, we come to an end of our presentation and thank all of you for attending the same. We hope that you gained some insights on data replication and are excited about using this in your own architecture. If there are any questions, we're happy to answer them.
Or we can also connect offline at the given Twitter and LinkedIn. Thank you everyone for attending. Please free feel to ask any questions if you have. Again, thanks Women Tech Network for giving us this wonderful opportunity. It was great to talk about one of the topics which we are really passionate about. Okay, in that case, should we end this question?
yes thank you so much everyone do connect if you have any questions thank you bye
Transcribed automatically; names checked against the speaker record. Select a time to jump there.
Article based on this talk
Why database replication is about far more than backups
Ria Bhatia and Tanya Sneh explain the main replication models, their trade-offs, and how to handle lag and conflicts in real systems.
In short
Database replication keeps synced copies of data on several servers, and according to software engineers Ria Bhatia and Tanya Sneh it does much more than protect against loss: it lowers latency, keeps systems running through failures and scales read traffic. Their beginner's guide compares single-leader, multi-leader and leaderless replication, shows asynchronous MySQL replication live, and explains how to deal with replication lag and write conflicts before picking an approach.
Key takeaways
Understand the purpose of database replication and its benefits, including improved performance and reduced downtime.
Use it as a stepping stone to make better design decisions and convey them effectively, even without prior experience in the problem domain.
Replication is not just a backup
Bhatia describes a replica as a copy of the primary database that stays in sync with it, like a backup dancer ready to step in. Tanya Sneh then makes the case that the backup comparison undersells it. "So no, data replication is not just a backup. It is important to keep data geographically closer to your users." (6:58)
Using a streaming example, she explains that a request crossing from the Americas to the UK takes time no query optimisation can remove. "You can only reduce that if that server is located closer to your location." (8:21) Replicas in several regions let each user connect to the nearest one.
The second benefit is resilience. If a server catches fire, a traditional backup takes time to restore and misses the latest writes, while a replica that receives changes as the primary is updated can take over requests immediately. The third is scale: read-intensive applications can spread requests across read replicas instead of loading a single database.
Single-leader replication and its two modes
In single-leader (master-slave) replication, every database can serve reads but only the leader accepts writes, forwarding each change to its followers. Sneh walks through the two ways it can replicate. With asynchronous replication, the leader acknowledges a write such as x equals 5 after updating itself and passes it on later, making the system eventually consistent. Writes are fast, but clients can read stale data from followers. "Also, if the leader goes down before having replicated the transactions, it can lead to a potential data loss situation." (12:23)
Synchronous replication waits for every follower before acknowledging, which slows writes and can stall the whole system if a follower goes down. "So in reality, there's no one-size-fits-all approach to choosing a synchronization approach for single master database." (12:52) In practice, teams often keep some followers synchronous and others asynchronous to get the best of both.
Multi-leader, leaderless and a live MySQL demo
A single leader becomes a bottleneck for write-heavy or globally distributed systems, since distant clients face large network latency on every write. Multi-leader replication places leaders in different regions, lets clients write to the closest one, and replicates changes to followers and other leaders. The trade-off is that concurrent writes on different leaders can conflict.
Leaderless replication drops the distinction entirely: a client sends a write to all nodes and waits for acknowledgement from some of them, for example three out of five, with reads handled the same way. Sneh points interested viewers to quorums as the advanced concept behind it.
Bhatia then runs two MySQL servers on ports 3307 and 3308, one as leader and one as follower. She inserts a record into a test table on the leader and reads it back from the follower, showing MySQL's asynchronous leader-to-follower replication in action.
Living with replication lag
After the demo, Bhatia turns to what goes wrong. "When you're designing large scale applications, a lot can go wrong." (19:18) With asynchronous replication, a follower can trail the leader. In her example, a user uploads a video, is told it is live, refreshes within seconds, hits a follower that has not caught up, and is told the video is not there. In theory, she notes, there is no limit to how long the lag can last, from seconds to many minutes.
Her first remedy is read-your-writes consistency: for a short window after a user writes, perhaps 10 seconds, that user's reads go to the leader, while other users keep reading from followers. Her second addresses values that seem to jump back in time, such as comments appearing and disappearing on refresh. Monotonic read consistency pins a user's reads to one replica until replication catches up. Throughout, she stresses that the right choice depends on the business model, and that there is no right or wrong answer.
Resolving conflicts and choosing well
In multi-leader setups, two colleagues in different regions might update the same customer record, one changing the address and the other the phone number. Last write wins keeps the latest commit, but the earlier writer's change is silently lost despite an acknowledgement. Manual conflict resolution leaves the decision to application code, which might merge the values, show both to the user, or create a marker object for someone on the team to resolve later, preventing data loss at the cost of manual effort.
Sneh closes with selection factors: workload volume and frequency of change, consistency requirements, and system architecture, including whether it is distributed, cloud-based or on-premise. Once a method is chosen, she recommends monitoring and alerting, a robust backup and recovery plan, and validation before rollout. "Before you actually go ahead and roll out your replication strategy, you should make sure to test and validate it thoroughly." (30:14)
Watch the talk
Database Replication 101: A Beginner's Guide to Building High-Availability Systems, Ria Bhatia with Tanya Sneh, 32 minutes: https://www.womentech.net/video/database-replication-101-beginners-guide-building-high-availability-systems
About the speaker
Ria Bhatia is a software engineer in the fintech industry who has evaluated multiple database systems with a focus on performance, durability and scalability, and now works on embedded persistent key-value stores. She co-presented with Tanya Sneh, a software engineer and IIT Kharagpur alumna who builds scalable applications with multiple database technologies in fintech.
Questions this talk answers
Is database replication just a backup?
What is the difference between asynchronous and synchronous replication?
When should you use multi-leader replication?
How can you limit the effects of replication lag?
What should you consider before choosing a replication approach?
Drafted with AI from the talk transcript. Editor review pending.
Frameworks and ideas mentioned
Single-leader (master-slave) replication
Multi-leader replication
Leaderless replication
Asynchronous replication
Synchronous replication
Eventual consistency
Quorums
Read-your-writes consistency
Monotonic read consistency
Last write wins
Cite this talk
Ria Bhatia. "Database Replication 101: A Beginner's Guide to Building High-Availability Systems." WomenTech Network, 2023. https://www.womentech.net/video/database-replication-101-beginners-guide-building-high-availability-systems