Tuning InfluxDB Shard Group Durations to Prevent Query Degradation
Avoid 'too many open files' errors and query lag in InfluxDB by correctly aligning your shard durations with your retention policy.
03 May 2026, 04:12 UTC

The Shard Fragmentation Problem
When managing time-series data in InfluxDB, it is easy to focus on the retention policy (RP)—the rule that tells the database when to delete old data. However, the underlying engineering decision that actually dictates performance is the Shard Group Duration. If this duration is misaligned with your retention period, you likely encounter "too many open files" errors or a steady crawl in query latency as your dataset grows.
The core issue is how the Time-Structured Merge Tree (TSM) engine handles data on disk. InfluxDB partitions data into shards based on time. If you have a one-year retention policy but a shard group duration of one hour, the engine must manage 8,760 separate shard directories. Every query spanning a month would then need to open and scan hundreds of files, creating massive I/O overhead.
How TSM Sharding Works
InfluxDB uses a log-structured approach to handle high write throughput. Data first hits the Write-Ahead Log (WAL) for durability and is then compacted into immutable TSM files. These files are grouped into shards.
A shard is the smallest unit of data expiration. When a shard's time range falls entirely outside the retention policy, InfluxDB deletes the entire shard directory from the disk. This is significantly more efficient than deleting individual rows, but it means your shard duration must be a logical divisor of your total retention time.
Calculating the Right Shard Duration
The goal is to balance the number of open file handles against the efficiency of data deletion. As a general rule, you want to keep the number of active shards for a given retention policy manageable—typically between 5 and 50 shards for the total duration.
Comparison: Misaligned vs. Aligned Sharding
| Scenario | Retention Policy | Shard Duration | Total Shards | Impact |
|---|---|---|---|---|
| Under-sharded | 30 Days | 1 Hour | ~2160 | High I/O, file limits |
| Over-sharded | 30 Days | 30 Days | 1 | Inefficient deletion (latency) |
| Balanced | 30 Days | 1 Day | 30 | Optimal query/cleanup |
Practical Implementation
Since you cannot change the shard duration of an existing retention policy, you must create a new policy with the correct settings and migrate your data. Here is how to configure a 90-day retention policy with 7-day shard groups:
# Run in Influx CLI with administrative privileges CREATE RETENTION POLICY "long_term" ON "telemetry" DURATION 90d REPLICATION 1 SHARD GROUP 7d
To verify the configuration, check the number of shards created in the data directory (usually /var/lib/influxdb/data). You should see a folder structure that reflects the 7-day windowing logic.
Trade-offs and Limitations
There is a critical trade-off between query granularity and disk cleanup. While larger shards reduce the number of files the engine must scan, they make the deletion process "chunkier." If a shard is too large, data might stay on disk longer than the retention policy specifies until the shard expires. Additionally, TSM compression is most effective when time intervals are regular; irregular bursts can increase the storage footprint within a shard.
Because InfluxDB versions differ in storage engine details, confirm the exact retention policy syntax and shard behavior against your deployed version before applying changes in production.
Actionable Summary
Before deploying, calculate your total shard count (Retention Duration / Shard Duration). Aim for a number that keeps your active shards manageable to avoid OS-level file handle exhaustion. If you are already seeing performance degradation, consider a new policy with a larger duration and using a Continuous Query (CQ) to downsample and migrate your data.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.