Bugpit

Lessons › Design skills

Fresh or fast

People change their shipping address, reload the page and see the old one. Your replicas are a moment behind the primary, so reads can return old data. For this shop, that isn't acceptable.

Play this level

Part of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

Eventual consistency means copies catch up soon, but a read right now may be old. Strong consistency means every read sees the latest write. You can't have it for free.

With asynchronous replication, the primary says "done" and copies the change afterwards. That's fast, but copies lag. With synchronous replication, the primary waits for the copy first. Reads are fresh, and every write takes longer.

The CAP theorem is about what happens when machines can't reach each other. You then have to choose: refuse the request and stay correct, or answer with data that may be old.

Pick per feature. A like count can be old for a second. A bank balance can't. Another fix is read your writes: send a person's reads to the primary just after they change something.

This simulator only shows the slower writes. In real systems a synchronous copy has one more cost: if it dies, writes can stop until it's replaced.

What you have to achieve

9,000 requests a second at the busiest hour, 5% of them writes.

What you learn

Follow a write now: it waits for the copy. You paid in speed to get correct reads. Almost every data decision in system design is this same trade.

Check yourself

A like count is one second out of date. What should you do?

Show the answer

Nothing: eventual consistency is fine here. Nobody is harmed by an old like count, so don't pay for strong consistency you don't need.

The network splits two database machines apart. What does the CAP theorem say?

Show the answer

Choose between correct data and answering every request. During a split, a machine either refuses to answer or risks returning old data. It can't promise both.

What does synchronous replication cost you?

Show the answer

Writes get slower. The primary waits for the copy before it says "done", so each write takes longer. In return, the copy is never behind.

Try it yourself

More in Design skills

← Shape the data and the API Add a machine, keep your data →