AlgoViz

Trade-offs in databases

medium 30 min

Every database trades freshness, speed, safety and uptime against each other.

See it run

Turn R and W on five copies, take a copy away, and see what each setting costs.

Operations

Balanced: 3 and 3

Majorities for both. The sets always meet.

Step 1 of 4

Five copies

copy 1copy 2copy 3copy 4copy 5
valuev1v1v1v1v1
write set
read set

All hold v1.

Step 1: Five copies

def write(value):    acks = send_to_all(value)    wait(acks >= W)            # W = 3def read():    got = ask(R)               # R = 3    return newest(got)
Reads
Medium
Writes
Medium
Survives
2 of 5 failures

Look at the worst case: the sets as far apart as possible.

In the real world

The same trade-offs in databases doing real jobs. Pick one and step through it.

Quorums overlap, and an update is lost all the same.

Step 1 of 4

Stock is 1

Our systemClientCoordinatorCopy 1stock 1Copy 2stock 1Copy 3stock 1

All three copies say so.

Step 1: Stock is 1

Common mistakes

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

  • Waiting for all copies

    major

    One copy in maintenance stops every write.

    Wrong
    W = N
    Right
    W = N // 2 + 1
  • Trusting overlap with concurrent writers

    critical

    Two writers may both meet the quorum and one change vanishes.

    Wrong
    write("stock", read("stock") - 1)
    Right
    update_if("stock", expected=1, new=0)
  • Confirming from one disk

    critical

    The disk dies and a paid order is gone.

    Wrong
    place_order(w=1)
    Right
    place_order(w="majority")
  • Writing the level into the query text

    minor

    In Cassandra the level is sent by the driver, not written in the SELECT.

    Wrong
    SELECT * FROM usersUSING CONSISTENCY QUORUM;
    Right
    CONSISTENCY QUORUM;SELECT * FROM users WHERE id = 7;

Interview tips

  • How to explain it

    • N copies, W confirm, R answer.
    • R + W > N makes the sets meet.
    • Choose for each operation.
  • Memory trick: RWN

    • Readers and Writers
    • must meet in the Network.
    • Two circles that overlap.
  • Go safer for

    • Orders and payments.
    • Stock at checkout.
    • Anything confirmed to a customer.
  • Go faster for

    • Views and likes.
    • Reviews.
    • Metrics.