Module 13 / Replication

Module 13 · lesson 3 of 9 · Read 9 min

Synchronous or asynchronous?

Durability against latency, and semi-sync in between.

A customer places an order. The leader writes it. At some point the leader replies "order placed", and the customer's screen turns green. The question this lesson answers is simple to ask: at that moment, how many machines have the order?

If the answer is "only the leader", a single crash can lose an order the customer was told about. If the answer is "the leader and every follower", one slow follower can make every order slow. Most real systems choose something in between, and knowing exactly where they sit is the difference between "we never lose data" and "we usually don't".

The moment that matters

Here's a write, step by step, with one follower:

%% caption: The leader can reply to the order service before or after the follower has the change. That choice is the whole lesson.
sequenceDiagram
  autonumber
  participant O as Order service
  participant L as Leader
  participant F as Follower
  O->>L: insert order
  Note over L: writes to its own log
  alt async
    L-->>O: order placed
    L->>F: change, in the background
  else sync
    L->>F: change
    F-->>L: got it
    L-->>O: order placed
  end
The leader can reply to the order service before or after the follower has the change. That choice is the whole lesson.
  • Asynchronous: the leader commits locally, replies to the client straight away, and sends the change to followers in the background.
  • Synchronous: the leader commits locally, sends the change, waits for the follower to confirm it has it, and only then replies.

In synchronous replication, the leader waits for a follower to confirm a write before reporting success. In asynchronous replication, it reports success first and sends the write to followers afterwards.