Quick revision
The short answer
Two million devices sending every 10 seconds is about 200,000 writes per second and roughly 17 billion readings a day, so we ingest through a durable queue and write in large batches into a time-partitioned store sharded by device group. Recent raw data lives in a hot tier with daily chunks that are compressed after a day, continuous aggregates per minute and per hour serve dashboards and multi-year reports, raw data is kept only as long as the business needs it, and a cold tier of columnar files in object storage holds the full two years for analytics. The design must also handle late and out-of-order readings, device clock errors and backfill.
What to remember
In this answer, we sized the problem at 200,000 writes per second and about 400 TB of raw data over two years, which forced a tiered design. Readings flow through a durable queue into batched writes on time-partitioned shards grouped by device. Recent raw data is hot and compressed after a day, per-minute and per-hour continuous aggregates serve dashboards and long reports, and the full history lives as columnar files in object storage. Late data, bad clocks and backfill are handled by refresh windows, validation at ingest and replay from the queue.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Two million devices sending every 10 seconds is about 200,000 writes per second and roughly 17 billion readings a day, so we ingest through a durable queue and write in large batches into a time-partitioned store sharded by device group. Recent raw data lives in a hot tier with daily chunks that are compressed after a day, continuous aggregates per minute and per hour serve dashboards and multi-year reports, raw data is kept only as long as the business needs it, and a cold tier of columnar files in object storage holds the full two years for analytics. The design must also handle late and out-of-order readings, device clock errors and backfill.
Why this question is asked
IoT storage questions are really arithmetic questions first. Many designs fail in an interview because the candidate picks a database before working out how much data there is. At this scale a single wrong choice, such as one row per insert statement or keeping raw data online for two years, is the difference between a system that costs a few thousand dollars a month and one that cannot be built at all. Interviewers ask this to see whether we size the problem, separate hot and cold data, and plan for the messy reality of devices. In this answer, we will do the numbers, design the ingest path, design the hot tier, add aggregates for dashboards and reports, add a cold tier for history, and then deal with late data and clock errors.
1Sizing the Problem First
Everything follows from a few multiplications, so we do them out loud.
So we need a steady 200,000 inserts per second with headroom for bursts, perhaps 2 to 3 times that when devices reconnect after an outage. And keeping two years of raw rows in an online row-store database would mean hundreds of terabytes before indexes. That single number tells us the design must have tiers: raw data cannot all be hot.
2The Ingest Path: Queue, Batch, Partition
Devices usually publish over MQTT to a broker, which forwards readings into a durable log such as Kafka. The log absorbs bursts and lets the database be slow or down for a while without losing data. We partition the Kafka topic by device id, so readings from one device stay in order within one partition.
Writer services consume the topic and write in batches. One insert per reading would mean 200,000 round trips and commits per second, which no single database handles well. Batching 5,000 to 10,000 rows per COPY or multi-row insert brings that down to 20 to 40 commits per second, each of which writes contiguous data. We also split the devices into groups, for example 8 device groups by hash of device id, and give each group its own database node. Each node then takes about 25,000 rows per second, which a well-tuned PostgreSQL with TimescaleDB can sustain on good hardware. Within each node, the data is partitioned by time into daily chunks.
- Devices -> MQTT broker2 million devices, one reading every 10 seconds
- Kafka topic by device idabsorbs bursts, keeps order per device, replay for backfill
- Batch writersCOPY 5,000 to 10,000 rows per batch, one writer group per shard
- Hot tier: 8 time-partitioned shardsdaily chunks, raw data, compressed after 1 day
- Continuous aggregatesper minute for 90 days, per hour for 2 years
- Cold tier: Parquet in object storageall raw data for 2 years, queried by a SQL engine
6 layers, from the top down. Step through them one at a time.
Each tier answers a different question at a different cost.
3The Hot Tier: Recent Raw Data
The hot tier answers the last-day dashboards and live views, so it holds raw readings for a short window, and it is designed so that the chunk being written fits in memory.
Each shard receives about 2.2 billion rows a day, which is roughly 70 GB raw. Compressing chunks older than a day, segmented by device, typically reduces that by ten times or more, because timestamps 10 seconds apart and slowly changing temperatures compress extremely well. With 30 days of raw data online, each shard holds a few hundred GB compressed instead of about 2 TB raw. Notice the received column: we keep both when the device measured and when we stored it, which we will need for late data.
4Aggregates for Dashboards and Reports
Dashboards over the last day can read raw data for a few devices, but a fleet-wide chart or a report over two years must not touch raw rows. So we maintain continuous aggregates: per-minute rows with min, max, average and count, and per-hour rows built on top of the per-minute ones. Each has its own retention.
The arithmetic again guides the choice. Per-minute rows for every device are 2 million times 1,440, about 2.9 billion rows a day, still large, so we keep them for 90 days. Per-hour rows are 48 million a day, about 35 billion over two years, which is manageable when compressed. A yearly report then reads hourly rows, around 8,760 per device, instead of more than 3 million raw readings per device.
How long raw data stays online is a business decision, not a technical one, so we ask. If raw readings are needed only for 30 days of troubleshooting, the hot tier stays small. If a regulator needs every raw reading for two years, that is what the cold tier is for.
| Tier | Resolution | Kept for | Serves |
|---|---|---|---|
| Hot raw chunks | 10 seconds | 30 days | live views, last-day dashboards, debugging |
| Per-minute aggregates | 1 minute | 90 days | fleet dashboards, weekly trends |
| Per-hour aggregates | 1 hour | 2 years | monthly and yearly reports |
| Cold Parquet files | 10 seconds | 2 years | ad hoc analytics, audits, model training |
5The Cold Tier: Two Years in Object Storage
For the full raw history, a database is the wrong place. We export each day's raw data, partitioned by date and device group, into columnar Parquet files in object storage, ideally managed as an Apache Iceberg or similar table so the schema and file list are tracked. A query engine such as Trino, Spark, Athena or DuckDB can scan these files for multi-year analytics.
Columnar files compress raw readings well, so the 400 TB raw becomes tens of terabytes, and object storage costs a small fraction of database storage per terabyte. The export can come from the hot tier before chunks are dropped, or directly from Kafka through a separate consumer, which has the advantage that the cold tier does not depend on the database at all.
6Late Data, Clock Errors and Backfill
Real devices do not behave. A device that loses connectivity for six hours will upload six hours of readings at once, so data arrives late and out of order. Device clocks drift or reset to 1970 after a battery change. And sometimes we must reload a week of data after fixing a bug.
We plan for each case. Late data is normal, so the continuous aggregate refresh window, its start_offset, covers a few hours back, and readings later than that trigger a targeted refresh of the affected buckets. Clock errors are checked at ingest: a timestamp more than, say, five minutes in the future or before the device was installed is either corrected using the received time or sent to a quarantine table for inspection, rather than silently creating a chunk in the year 2099. Backfill goes through the same Kafka path or a bulk COPY into the right chunks; inserting into chunks that are already compressed is supported in recent TimescaleDB versions but costs more, so check your version and prefer backfilling before compression where possible.
7Follow-up questions
Why not a dedicated time-series database instead of PostgreSQL?
That is a fair option. InfluxDB, ClickHouse or a managed time-series service can handle this load too, and ClickHouse in particular is very strong at large scans. The design stays the same: batched ingest, time partitioning, aggregates and a cold tier. I would choose based on the team's skills, the query patterns, and whether we need joins with relational device data.
Why shard by device group rather than by time?
Sharding by time would send all current writes to the one node that owns the current period, a hot spot. Sharding by device group spreads current writes evenly, and each shard then partitions by time internally, so we get both even writes and cheap retention.
What happens if a database shard is down for an hour?
Kafka keeps the readings, so writers for that shard simply fall behind and catch up when it returns. At 25,000 rows per second per shard we need enough spare write capacity to replay an hour of backlog in a reasonable time, which is another reason to size for 2 to 3 times the average rate.
8Conclusion
In this answer, we sized the problem at 200,000 writes per second and about 400 TB of raw data over two years, which forced a tiered design. Readings flow through a durable queue into batched writes on time-partitioned shards grouped by device. Recent raw data is hot and compressed after a day, per-minute and per-hour continuous aggregates serve dashboards and long reports, and the full history lives as columnar files in object storage. Late data, bad clocks and backfill are handled by refresh windows, validation at ingest and replay from the queue.
Open with
I would start with the numbers. Two million devices every ten seconds is two hundred thousand writes per second, about seventeen billion readings a day, and roughly four hundred terabytes of raw rows over two years, so raw data cannot all live in an online database.
Then, if they want more
Devices publish over MQTT into Kafka, partitioned by device id, which absorbs bursts and lets us replay. Writers batch five to ten thousand rows per COPY into a time-partitioned store, for example TimescaleDB, split into about eight shards by device group so each takes around twenty-five thousand rows a second.
Each shard uses daily chunks, compresses chunks older than a day segmented by device, and keeps raw data for maybe thirty days, if the business agrees. Continuous aggregates per minute, kept ninety days, and per hour, kept two years, serve the dashboards and yearly reports.
The full raw history goes to Parquet in object storage, managed as an Iceberg table and queried with Trino or similar. Finally I plan for messy devices: I store both device time and received time, refresh aggregates over a late-data window, reject or quarantine impossible timestamps, and backfill through the same pipeline.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation, MongoDB manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your company stores IoT sensor readings from 2 million devices, one reading per device...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/cbef13e174/