Prompt
How do I shard a time-series database for many devices?
Latest observation
Sharding a time-series database for many devices usually comes down to optimizing for two things:
- write throughput — lots of incoming device data
- query locality — most queries ask for data from a device or group of devices over a time range
A good sharding strategy for this kind of workload is usually device-based partitioning with time inside each shard.
Common sharding patterns
1. Shard by device ID
Use a hash of device_id to assign each device to a shard.
Example:
shard = hash(device_id) % N
Pros
- Easy to distribute writes evenly
- Queries for a single device hit only one shard
- Simple to implement
Cons
- If some devices are much “hotter” than others, one shard can still become overloaded
- Time-range queries across many devices need fan-out to multiple shards
This is often the default starting point.
2. Shard by time
Partition by time windows like day, week, or month.
Pros
- Efficient retention and archival
- Easy to delete old data
- Great for queries over recent data only
Cons
- Very bad for write balancing if all new data goes into the latest partition
- Not ideal if many devices write simultaneously
- Can create a hot shard/partition problem
Usually time partitioning alone is not enough for a large multi-device system.
3. Composite sharding: device + time
This is often the best approach.
Typical design
- First shard by
device_idortenant_id + device_id - Then within each shard, partition by time buckets
Example key
shard_key = hash(device_id) % N- storage layout inside shard:
device_id / yyyy-mm-dd
Pros
- Balances write load across shards
- Keeps per-device queries local
- Makes retention easier via time partitions
- Avoids huge single partitions
Cons
- More operational complexity
- Cross-device queries may still fan out
Practical recommendations
If most queries are “get one device’s recent history”
Use:
hash(device_id)for shard assignment- time-based partitions inside each shard
This is usually the best fit.
If you have tenants/customers and many devices per tenant
Use:
hash(tenant_id + device_id)if you need balance across all devices- or
hash(tenant_id)if you want tenant data isolated per shard
If large tenants exist, sharding only by tenant can create hotspots.
A safer pattern is often:
- shard by
tenant_id, then sub-shard bydevice_idfor very large tenants
If some devices are extremely hot
Add a second level of sharding:
hash(device_id + salt)for writes- or split hot devices across multiple partitions
But only do this if needed, because it makes reads more complex.
Indexing and query design
Sharding alone is not enough. Make sure each shard can efficiently answer queries.
Inside each shard:
- index by
(device_id, timestamp) - store data clustered by time
- use compression suitable for time-series data
This helps queries like:
SELECT ... WHERE device_id = ? AND timestamp BETWEEN ...
Retention and downsampling
For time-series data, plan for:
- hot data: recent, frequently queried
- warm data: older, less frequently queried
- cold/archive data: long-term storage
A common setup:
- raw data kept for 7–30 days
- aggregated/downsampled data kept longer
Partitioning by time makes this easier.
Avoid these mistakes
- Sharding only by time for high-write systems
- Using too few shards early on
- Using a shard count that is hard to change
- Ignoring query patterns
- Letting one shard own all recent writes
- Not planning for rebalancing
Rebalancing strategy
Use consistent hashing or a virtual-node approach so you can add shards without moving everything.
Options:
- consistent hashing
- hash ring with virtual shards
- range partitioning with split/merge
For many devices, consistent hashing is often the simplest operationally.
Simple rule of thumb
If you need one practical default:
- Shard by
hash(device_id) - Partition inside each shard by time
- Index by
(device_id, timestamp) - Use retention policies per time partition
- Rebalance with consistent hashing or virtual shards
Example architecture
- 64 logical shards
- devices mapped by consistent hash
- each shard stores monthly partitions
- each partition indexed by device + timestamp
- recent partitions on SSD, old partitions compressed or moved to object storage
If you want, I can also help with:
- a schema design for this,
- a sharding algorithm with examples,
- or advice for a specific database like Postgres, Cassandra, TimescaleDB, InfluxDB, or ClickHouse.