Bugpit

Lessons › Core concepts

Pick the right key

Orders are stored in a NoSQL DB split by country, and 30% of orders come from one country. 12,000 req/s arrive, and the partition holding that country can't keep up.

Play this level

Free with an account. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

A partitioned database splits data by a key you choose. Each partition takes about 1,000 writes a second, so a key that sends a big share of traffic to one value makes one partition hot while the rest sit idle.

A good key has many values that are used about equally, such as a user ID or an order ID. Country, date or store are poor keys, because a few values get most of the traffic.

One database counter for every new ID is a bottleneck and a single point of failure. Snowflake style IDs combine the time, a machine number and a counter, so every machine makes unique IDs alone, and they still sort by time.

What you have to achieve

12,000 requests a second at the busiest hour, 30% of them writes.

What you learn

The database didn't need to be bigger. It needed a key that spreads the work, so every partition does a fair share. The key you choose often decides how far a design can scale.

Check yourself

Which partition key spreads orders most evenly?

Show the answer

Order ID. There are millions of order IDs and each is used about equally. Countries and dates bunch traffic onto a few values.

Why not use one database counter for every new ID?

Show the answer

Every machine has to wait for that one database. One counter means one place every write must visit. It limits how fast you can write, and when it's down nothing new can be made.

A Snowflake style ID is built from the time, a machine number and a counter. What does that give you?

Show the answer

IDs every machine makes alone, which still sort by time. The machine number keeps IDs from clashing without any coordination, and putting the time first keeps them in order, which suits indexes.

Try it yourself

More in Core concepts

← Forget the user between requests Built for writes →