Bugpit

Lessons › From one user to millions

Find anything, fast

A shop with 10,000 req/s adds a search box. 4% of requests are searches, 400 a second, and each one makes the database read through the whole product table.

Play this level

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

The lesson

A normal database index finds an exact value, or a range of values, fast. Finding a word inside text is different: the database reads row after row. It gets slower as the table grows, and bigger machines barely help.

A search index such as Elasticsearch or OpenSearch works like the index at the back of a book. It lists every word and where it appears, so a search takes milliseconds instead of a second.

It's a second copy of your data, and copies drift. The usual way to keep it fresh: send each write to a stream, and let one Worker save it to the database while another adds it to the index.

So new products show up in search a moment late. That's fine for a shop, and worth checking for anything where it isn't.

How much a search costs a database depends on your table. This level assumes about 100 reads each.

What you have to achieve

10,000 requests a second at the busiest hour, 4% of them writes.

What you learn

Searches now take milliseconds and the database is calm again. Count what it took though: an index, a stream and two groups of workers, all to answer 4% of your traffic. Search is worth it, but it's a whole new system to run.

Check yourself

Why are text searches so hard on a normal database?

Show the answer

It reads row after row to find a word. A normal index finds exact values. Finding a word inside text means scanning, and it gets slower as the table grows.

How did you keep the search index up to date?

Show the answer

Writes go to a stream, and a worker adds them to the index. One worker saves to the database, another updates the index. New products show up in search a moment late.

Try it yourself

More in From one user to millions

← One event, many readers Take the money →