Smart home: a million devices
A million thermostats and plugs report in every few minutes: 4,000 req/s at the evening peak, 80% of them readings. Every reading lands in the main database, and the nightly energy report runs at 20:00, on top of the peak.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Device readings are nearly all new writes, and old values never change. A time-series database is built for exactly that, and keeps them out of the database the app needs.
Put the readings on an event stream first. Devices come and go in bursts, and the stream gives each burst a place to wait while workers save it.
The nightly report is the other half. A scheduler starts it at a set hour, and someone set it to the busiest hour of the day. At 03:00 traffic is a small fraction of the peak.
Check every hour of the day, not just the peak. A design that holds at 20:00 can still fall over at 21:00 if something else starts then.
What you have to achieve
4,000 requests a second at the busiest hour, 80% of them writes.
- No broken or illegal connections
- Use a Time-Series DB
- Use an Event Stream
- No component is overloaded
- Workers clear the queue within the day
- Under 0.1% of requests fail in every hour of the day
- p99 response time under 250 ms
What you learn
The database went from drowning to calm, and the report costs nothing more: it just runs at night. Most "we need bigger machines" moments start with work in the wrong place or at the wrong time.
Check yourself
Why do device readings belong in a Time-Series DB?
- They're nearly all new writes that never change
- They need joins
- They're searched by text
Show the answer
They're nearly all new writes that never change. A time-series store is built for that pattern, and keeps it away from the database the app needs.
The nightly report broke the evening. What fixed it for free?
- A bigger database
- More workers
- Running it at 03:00
Show the answer
Running it at 03:00. Same job, same machines. At 03:00 traffic is a small fraction of the peak.