A dealer portal's database runs on a single server, and at 02:00 the disk fails. There is a backup, but restoring it takes two hours, and during that time dealers cannot place orders. Database replication means keeping one or more copies of a live database continuously up to date, and switching to one of them when the primary node fails (failover). Done well, a two-hour outage shrinks to minutes; done badly, it brings data loss, stale reads and even two "primaries".
This guide covers how replication differs from backup, synchronous and asynchronous replication, replication lag and the "can't read your own write" problem, read replicas, automatic failover and the split-brain risk, the link between RPO and RTO, failover drills and cost. All figures are representative, not a product's performance or an industry average.
What Is Database Replication?
Database replication is the continuous transfer of every change made on the primary node, as a change log, to one or more replica nodes, where the log is applied in the same order. Writes go only to the primary; replicas serve reads and act as standbys. If the primary fails, one replica is promoted to become the new primary; this switch is called failover.
A replica applies the log slightly behind the primary, so there is always some lag. We covered choosing the database type in our guide to choosing a database; here we focus on making a chosen database resilient.
We recommend reading: Our custom software service
Does Replication Replace Backup?
No. A backup lets you go back in time, while replication keeps a current second copy. A table dropped by mistake is dropped on the replica too, so the two complement each other.
| Topic | Backup | Replication (replica) | Failover |
|---|---|---|---|
| Purpose | Return to an earlier moment | Keep a current second copy | Make a replica the new primary |
| Accidental delete, corrupted data | Recovers (to a restore point) | Does not; the error spreads | Does not |
| Hardware failure | Restore takes hours | Replica waits ready | Minutes, seconds with automation |
| Data loss window (RPO) | The backup interval | The lag, or ≈ 0 | Depends on replication mode |
How RPO and RTO are calculated for a single system is in our data backup guide; layering many systems into a recovery strategy is in our disaster recovery plan article.
How Do Primary–Replica Setups and Synchronous vs Asynchronous Replication Work?
The difference is whether the primary waits for the replica's acknowledgement before reporting a transaction as complete. Asynchronous replication does not wait: it is fast, but if the primary fails, transactions not yet shipped are lost. Synchronous replication waits: an acknowledged transaction is never lost, but every transaction gets slower.
Representative assumptions: local disk acknowledgement 4 ms, round trip to a replica in the same region 2 ms, to a replica in a distant region 30 ms. Synchronous time is the round trip plus the replica's disk acknowledgement (parallel to the local write).
| Mode | Acknowledgement time | Sequential commit rate | Data loss (RPO) | Write downtime (month, replica included) |
|---|---|---|---|---|
| Asynchronous | 4 ms | 250/s | The lag | 216 min |
| Synchronous, same region, one replica | 2 + 4 = 6 ms | 166.7/s | ≈ 0 | 431 min |
| Synchronous, either of two replicas acknowledges | 6 ms | 166.7/s | ≈ 0 | 217 min |
| Synchronous, distant region, one replica | 30 + 4 = 34 ms | 29.4/s | ≈ 0 | 431 min |
The sequential commit rate is 1,000 ÷ acknowledgement time: 1,000 ÷ 4 = 250, 1,000 ÷ 6 ≈ 166.7, 1,000 ÷ 34 ≈ 29.4. If an order save consists of five sequential commits, same-region synchronous acknowledgement adds (6 − 4) × 5 = 10 ms, and a distant region adds (34 − 4) × 5 = 150 ms.
In the last column, let each node be 99.5% available per month (30 days = 43,200 min) with independent failures. In asynchronous mode, writes depend only on the primary: 43,200 × 0.005 = 216 min. With one strictly synchronous replica, writes need both nodes up: 0.995 × 0.995 = 0.990025, so downtime is 43,200 × 0.009975 ≈ 431 min. If the acknowledgement of either of two replicas is enough: 0.995 × (1 − 0.005²) = 0.995 × 0.999975 = 0.994975, so downtime is 43,200 × 0.005025 ≈ 217 min. Strict synchrony with a single replica doubles write downtime; several candidate replicas avoid that. The independence assumption is optimistic; shared network or management faults make reality worse.
What Is Replication Lag and Why Can't Users Read Their Own Writes?
Replication lag is the time it takes for a change on the primary to become visible on the replica. When lag is high, a user reading from a replica sees stale data; the most annoying form is a user being unable to read a record they have just written.
Lag builds up as a queue. At 02:00 a bulk price update produces 3,000 row changes per second for 20 s (60,000 in total). The replica applies 1,200 changes per second; in 20 s it applies 24,000 and 36,000 remain behind. Peak lag is 36,000 ÷ 1,200 = 30 s. When the job ends, normal traffic is 150 per second, so the replica drains a net 1,050/s: 36,000 ÷ 1,050 ≈ 34 s later the lag disappears. If you throttle the bulk job to 1,000 changes per second (1,000 + 150 = 1,150 < 1,200), no backlog forms at all; the job takes 60,000 ÷ 1,000 = 60 s.
Not reading your own write looks like this: a dealer saves an order, the system redirects to "my orders", the page reads from a replica, and the order is missing. Example measurement: in 1,000 redirects the order was missing 70 times, that is 7.0%. The fixes are simple:
- Read from the primary briefly: for 10 s after a write, that session's reads go to the primary.
- Position token: store the log position after the write; until the replica reaches it, route the read to the primary.
- Session pinning: a session always reads from the same replica, so data never goes backwards.
The cost: with 9,000 orders a day and 3 reads within 10 s after each, 27,000 reads go to the primary. Against 1,800,000 reads a day that is 1.5%: negligible for load, decisive for user experience.
Track lag as p50, p95 and maximum, and set the alert threshold from a business rule (for example, when p95 exceeds 5 s). For monitoring design see our software monitoring guide.
How Do Read Replicas Spread the Load?
You send most reads to the replicas; the primary keeps only writes and lag-sensitive reads. Replicas scale reads, not writes.
Representative assumptions: 600 queries/s at peak, 90% reads (540) and 10% writes (60). Load unit: read 1, write 3; each node has capacity of 800 units/s. A replica applying a write costs 2 units.
| Situation | Primary load | Load on each replica |
|---|---|---|
| Single server | 540 + 60 × 3 = 720 → 90.0% | — |
| Two replicas, 85% of reads on replicas | 81 + 180 = 261 → 32.6% | 229.5 + 120 = 349.5 → 43.7% |
| One replica down | 261 → 32.6% | 459 + 120 = 579 → 72.4% |
15% of reads stay on the primary (540 × 0.15 = 81); the other 85%, 459 reads, split across two replicas (229.5 each). The 15% that stays covers reads after a write (1.5%) and queries that cannot accept stale data (credit limit, stock reservation). Headroom: on a single server, 800 ÷ 720 ≈ 1.11×; with two replicas the replica is the bottleneck, 800 ÷ 349.5 ≈ 2.29×. Replicas do not reduce write load. For every dimension of scaling see our scalability guide, and before adding replicas consider queries and caching in our caching article.
How Does Failover Work and When Is Automatic Failover Risky?
Failover means recognising the primary has failed, silencing the old primary, promoting the most current replica and redirecting applications to the new primary. Automatic failover does this without a human; it is fast, but if it decides wrongly it makes things worse.
Time budget: detection 6 missed heartbeats × 5 s = 30 s; fencing 5 s; promotion 15 s; applications switching connections to the new primary 40 s. Total 30 + 5 + 15 + 40 = 90 s. The detection threshold is a trade-off: a short threshold is fast on a real failure but produces false failovers on brief network drops. A representative year: 12 short network interruptions (8 lasting 8 s, 3 lasting 20 s, 1 lasting 40 s) and 3 real failures. Outage in a real failure = threshold + 60 s (fencing, promotion, redirect); a false failover causes a 60 s outage.
| Detection threshold | False failovers (year) | Outage in a real failure | Total annual outage |
|---|---|---|---|
| 10 s | 4 | 70 s | 3 × 70 + 4 × 60 = 450 s = 7.5 min |
| 30 s | 1 | 90 s | 3 × 90 + 1 × 60 = 330 s = 5.5 min |
| 60 s | 0 | 120 s | 3 × 120 = 360 s = 6.0 min |
Here 30 s is best; choose yours from your own network's measurements.
Split-brain is when, after a network break, both the old primary and the promoted replica accept writes. Example: for 4 min, 40% of clients still reach the old primary and total writes are 60/s. The old node receives 0.4 × 60 = 24/s × 240 s = 5,760 writes, the new node 36/s × 240 s = 8,640 writes. If both continue from the same number sequence, at least 5,760 record numbers are used twice and the two sides cannot be merged automatically; one side's writes are discarded or reconciled by hand. Protections:
- Fencing: before promotion, the old primary's ability to write is really shut off.
- Majority vote: three votes (primary, replica, a small witness node) and 2 are needed to decide; a primary cut off alone has one vote and stops writing.
- No automatic return of the old primary: when it comes back it resyncs as a replica; the skipped writes are reviewed.
Avoid two-node automatic failover without fencing and a witness. We covered simultaneous writes to the same record in our concurrency and locking article.
How Are RPO and RTO Linked to Replication?
Replication determines RTO, and its mode determines RPO. Assume an outage costs ₺1,000 per minute (representative).
| Strategy | RPO | RTO | Outage cost per event |
|---|---|---|---|
| Backup only (hourly) | 60 min | 120 min | ₺120,000 |
| Asynchronous + manual failover | The lag (p95 2 s; peak 30 s) | 25 min | ₺25,000 |
| Asynchronous + automatic failover | The lag (p95 2 s; peak 30 s) | 1.5 min | ₺1,500 |
| Synchronous replica + automatic failover | ≈ 0 (acknowledged transactions) | 1.5 min | ₺1,500 |
The backup RTO comes from a 200 GB restore taking 2 hours; the 25 min of manual failover is reaching the on-call person (10), deciding (5) and acting (10). With asynchronous mode, data loss is the lag at the moment of failure: measured p95 lag of 2 s (representative) × 60 transactions/s = 120 transactions; during a bulk job, in the worst case, 36,000 row changes. With synchronous acknowledgement no transaction is lost, but you pay the acknowledgement delay. Replication does not undo an accidental delete; you still need a backup.
How Do You Run a Failover Drill?
Try a planned switchover first (a controlled role swap), then an unplanned failure by stopping the primary; the first in a live maintenance window, the second first in a test environment with masked data.
- Planned switchover: stop writes, wait for the replica to catch up, swap roles; expected outage ≈ 1 min.
- Unplanned failure: stop the primary abruptly; measure detection, promotion and redirect times separately.
- Network partition: cut the link between primary and replica; verify the old primary stops writing.
- Rejoin: try bringing the old primary back as a replica.
Example: the first drill took 130 s because the connection pool stayed attached to the old address for 80 s (redirect 80 s instead of 40). After the pool refresh setting was fixed, the time fell to 90 s: (130 − 90) ÷ 130 ≈ 30.8% shorter. Close the findings with a blameless review, described in our incident management and post-mortem guide.
How Do You Calculate the Cost of Replication and Failover?
You compare the reduction in annual outage cost with the yearly cost of the replica, the witness and the drills. Representative assumptions: 3 unplanned failures and 4 planned maintenance windows a year; unplanned outage minute ₺1,000, night maintenance minute ₺250; restoring from backup 120 min, planned maintenance on a single node 20 min, switchover with a replica 1 min; replica server ₺12,000 a month, witness ₺1,500 a month; setup 18 person-days × ₺9,000 = ₺162,000.
| Item (annual) | Backup only | Replica + automatic failover |
|---|---|---|
| Unplanned outage | 3 × 120 = 360 min → ₺360,000 | 3 × 1.5 + 1 false × 1 = 5.5 min → ₺5,500 |
| Planned maintenance (night) | 4 × 20 = 80 min → ₺20,000 | 4 × 1 = 4 min → ₺1,000 |
| Replica server (₺12,000 × 12) | — | ₺144,000 |
| Witness (₺1,500 × 12) | — | ₺18,000 |
| Drills (6 person-days × ₺9,000) | — | ₺54,000 |
| Annual total | ₺380,000 | ₺222,500 |
Annual net gain is ₺380,000 − ₺222,500 = ₺157,500. Payback: ₺162,000 ÷ ₺157,500 ≈ 1.03 years, about 12.3 months. The result is sensitive to unplanned failures: the gain per failure is ₺120,000 − 1.5 × ₺1,000 = ₺118,500; the fixed items (planned maintenance gain ₺19,000, false failover −₺1,000) add +₺18,000. Net = 118,500 × n + 18,000 − 216,000. Break-even n = 198,000 ÷ 118,500 ≈ 1.67 failures a year. With 1 failure a year, net is −₺79,500 and replication does not pay for itself. For internal tools where a minute is cheap, backup and a fast restore may be enough. For the cost items see our software project cost guide.
What Are Common Mistakes in Replication?
- Treating a replica as a backup: an accidental delete spreads to it.
- Not monitoring lag: stale reads and data loss come as surprises.
- Sending every read to replicas: credit limit and stock reservation must stay on the primary.
- Automatic failover without a witness: a network break can create two primaries.
- Trusting an untested failover: it usually gets stuck in the connection pool or in permissions.
- Running bulk jobs at unlimited speed: backlog drives lag up.
Leave the legal side of KVKK (Turkey's personal data protection law) and of keeping personal data in a second region to your legal advisor.
We recommend reading: How to back up business data: RPO, RTO, the 3-2-1 rule and restore testing
We recommend reading: Our integration service
Resilient Custom Software Infrastructure with İnovabil
At İnovabil our focus is building custom software from scratch for your company: custom software, ERP, CRM, e-commerce, web, mobile and integrations. As recorded on our process page, secure hosting, role-based authorization, audit logs and maintenance and support are covered in our projects. We do not claim to offer database replication or failover as a service; however, we suggest writing down your RPO and RTO expectations in the requirements phase and comparing them with your outage cost.
We built CRM, purchasing and mobile for Avcılar İnşaat; a purchasing system for İZBAN; logistics and shipment planning with wholesale order management for Karyak; and warehouse management plus a complaint and support management system for Sleepy. Send us your outage cost and we will write the requirement together.
We recommend reading: Our software development process: from analysis to release, step by step
Frequently Asked Questions
What is database replication, and what is it for in short?
It is the continuous transfer of changes on the primary database to one or more replicas. It lets a replica take over when the primary fails (failover) and spreads read load across replicas.
If I have replication, do I still need backups?
Yes. An accidental delete or corrupted data spreads to the replica too. A backup lets you go back in time, while replication shortens downtime; they cover different risks.
Should I choose synchronous or asynchronous?
Synchronous if an acknowledged transaction must never be lost, asynchronous if speed and simplicity matter more. In the example, same-region synchronous acknowledgement takes 6 ms instead of 4 ms; accepting either of two replicas barely raises write downtime (217 min versus 216 min for asynchronous).
Why is replication lag a problem and how can it be reduced?
A user reading from a replica sees stale data, and in asynchronous mode data is lost at failure. Split bulk jobs, keep replica capacity above the write rate, and let the writing session read from the primary briefly.
What is split-brain and how is it prevented?
It is when, after a network break, two nodes both act as primary and accept writes, so records collide. Fencing, a majority vote with a witness, and not automatically bringing the old primary back prevent it.