AlgoViz

Data integrity and uptime

medium 30 min

When copies of your data cannot talk to each other you must choose: refuse some requests and stay correct, or answer and repair differences later.

See it run

Watch copies agree, fall behind, lose contact and come together again, and decide what each operation should do.

Operations

How many must confirm?

One write, three copies: wait for all, for a majority, or for one. Then one copy dies.

Step 1 of 6

Start

Three copiesClientCopy 17Copy 27Copy 37

In agreement

All three copies hold 7.

Step 1: Start

NEED = {"ONE": 1, "QUORUM": 2, "ALL": 3}def write(value, level, reachable):    if len(reachable) < NEED[level]:        raise Unavailable    store(value, reachable[:NEED[level]])
Write waits for
1, 2 or 3 copies
Risk
Refuse or stale
All, each 99%
97.03% uptime
Majority
99.97%
Any one
99.9999%

Strong does not mean all. A majority keeps agreement and still survives one loss.

In the real world

The same data integrity and uptime doing real jobs. Pick one and step through it.

Read your writes on top of lagging replicas.

Step 1 of 4

Edit

Our systemcopied laterUsersPrimaryAnaReplicaAnna

Saved

A user changes their name to Ana. The primary has it, the replica does not yet.

Step 1: Edit

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Waiting for every copy

    critical

    One dead copy stops all writes. With three copies of 99% that is 97.03%, worse than a single machine.

    Wrong
    need = len(copies)
    Right
    need = len(copies) // 2 + 1
  • One letter for the whole system

    major

    Payments and like counters have different needs. One setting makes one of them wrong.

    Wrong
    LEVEL = "ONE"          # everywhere
    Right
    level = {"pay": "QUORUM", "like": "ONE"}[operation]
  • Last write wins for a cart

    major

    The other side's items disappear, and clocks decide which side that is.

    Wrong
    cart = max(a, b, key=lambda c: c.time)
    Right
    cart = merge_items(a, b)
  • Replication as a backup

    critical

    A wrong delete reaches every copy within a second.

    Wrong
    # three replicas, so no backups
    Right
    backup_daily()keep_write_log()               # point-in-time recoveryrestore_test_every_month()

Interview tips

  • How to explain it

    • The wall is not your choice, what the lonely copy does is.
    • Wrong is expensive: refuse.
    • Silence is expensive: answer and merge.
  • Memory trick: CAP

    • Consistency: same data everywhere.
    • Availability: always responds.
    • Partition tolerance: works despite splits.
  • Good defaults

    • Transaction with a synchronous standby for money.
    • 2 and 2 of 3 for general data.
    • Read your writes for profiles.
  • Watch out for

    • W + R > N does not order concurrent writes.
    • Promotion without a look at the lag.
    • Copies instead of backups.