DEV Community

Varun Sagar
Varun Sagar

Posted on • Updated on

Database Replications

Why we need replication?

Earlier days, we used a simple machine having a web server, database, cache. But problem arises when this machine goes down, this is where we need a backup of this machine. So, In replication when we write to a machine we also ensure to write to other replicas (and the most common model we use is
a master-slave).

Types of replications

  • Synchronous replication : In simple master-slave model, we this sequence, when a write comes in

1.Master writes to its own database
2.Master sends it to replica to write
3.Replica write in its own db
4.Replica sends an ack to master
5.Master sends ack to client

And the master waits until all the replicas are write the change. There can be retry policy/roll back mechanisms too on master when the acks from replicas fails.
use case: banking and financial services
pros: no replication lag
con: takes longer - latency is more
with many replicas, it takes more time
this needs high speed network -typically lan

  • Asynchronous replication : In this case, when a write comes in

1.Master writes to its own database
2.Master returns the ack to client
3.replication to replica will start
4.eventually, the Replica will have the write.

In this case, replica can have a delay - replica might not have the latest data at a point
use case: to update likes/tweet/views
pros:

  • No waiting for replications
  • with many replicas performance remains
  • replication can be performed over internet too.

cons: replication lag - replica might be behind

This maintains eventual consistency. We can maintain a log when a replica goes down and perform the all the missed operations in order.

What happens when master fails?

  • sync replication of master : We can have synchronous HA master. (semi synchronous setup i.e sync with back up master and async with all other replicas). the benefit here is there won't be no down time.
  • sync replication of logs : Other way here could be to have a master backup which is checkpointing the db for every x minutes, but the master1 is writing the logs to backup synchronously.so backup can replay all the operations in the log if master goes down

Top comments (0)