1---
2name: influxdb
3description: Store and query time-series data with proper schema design and retention.
4metadata:
5 source: https://skillmd.com/skills/modbender/influxdb
6 clawdbot:
7 emoji: 📈
8 requires:
9 anyBins:
10 - influx
11 - curl
12 os:
13 - linux
14 - darwin
15 - win32
16---
17
18## Version Differences
19
20- InfluxDB 2.x uses Flux query language, 1.x uses InfluxQL—syntax completely different
21- 2.x: buckets, organizations, tokens; 1.x: databases, retention policies, users
22- Don't mix documentation—check version before copying queries
23
24## Tags vs Fields (Critical)
25
26- Tags are indexed, fields are not—filter on tags, aggregate on fields
27- Tag values must be strings—numbers as tags work but waste index space
28- Fields support numbers, strings, booleans—store metrics as fields
29- Wrong choice kills query performance—can't change after data written
30
31## Cardinality Trap
32
33- High-cardinality tags destroy performance—unique user IDs as tags = disaster
34- Cardinality = unique combinations of tag values—grows multiplicatively
35- Check with `SHOW CARDINALITY` (1.x) or `influx bucket inspect` (2.x)
36- Rule of thumb: <100K series per measurement; millions = problems
37
38## Line Protocol
39
40- Format: `measurement,tag1=v1,tag2=v2 field1=1,field2="str" timestamp`
41- No spaces around `=` in tags—space separates tags from fields
42- String fields need quotes, tag values don't—`field="text"` vs `tag=text`
43- Timestamps in nanoseconds by default—specify precision to avoid mistakes
44
45## Timestamps
46
47- Default precision is nanoseconds—sending seconds without precision flag = year 2000 data
48- Specify on write: `precision=s` for seconds, `precision=ms` for milliseconds
49- Missing timestamp uses server time—usually fine for real-time ingestion
50- Timestamps are UTC—client timezone doesn't matter
51
52## Retention and Downsampling
53
54- Set retention policy/bucket duration—data older than retention auto-deleted
55- Raw data at 10s intervals for 7 days, downsample to 1min for 30 days, 1h for 1 year
56- 2.x: Tasks for downsampling; 1.x: Continuous Queries
57- Without downsampling, storage grows forever and queries slow down
58
59## Flux Query Patterns (2.x)
60
61- Always start with `from(bucket:)` then `|> range(start:)`—range is required
62- `|> filter(fn: (r) => r._measurement == "cpu")` for filtering
63- `|> aggregateWindow(every: 1h, fn: mean)` for time-based aggregation
64- Chain transforms with `|>` pipe operator—order matters for performance
65
66## InfluxQL Patterns (1.x)
67
68- `SELECT mean("value") FROM "measurement" WHERE time > now() - 1h GROUP BY time(5m)`
69- Double quotes for identifiers, single quotes for string literals
70- `GROUP BY time()` for time-based aggregation—required for most dashboards
71- `FILL(none)` to skip empty intervals, `FILL(previous)` to carry forward
72
73## Schema Design
74
75- Measurement name = table name—one per metric type (cpu, memory, requests)
76- Tag for dimensions you filter/group by—host, region, service
77- Field for values you aggregate—usage_percent, count, latency_ms
78- Avoid encoding data in measurement names—`cpu.host1` wrong, `cpu` + `host=host1` right
79
80## Write Performance
81
82- Batch writes—individual points have HTTP overhead
83- Telegraf for production ingestion—handles batching, buffering, retry
84- Write to localhost if possible—network latency adds up at high throughput
85- `async` writes in client libraries—don't block on each write
86
87## Query Performance
88
89- Always include time range—unbounded queries scan everything
90- Filter on tags before fields—tags use index, fields scan data
91- Limit results with `LIMIT` or `|> limit()`—dashboard doesn't need 1M points
92- Use `GROUP BY` / `aggregateWindow` to reduce data before returning
93
94## Common Errors
95
96- "partial write: field type conflict"—same field with different types; fix at source
97- "max-values-per-tag limit exceeded"—cardinality too high; redesign schema
98- "database not found"—2.x uses buckets, not databases; check API version
99- Query timeout—add narrower time range or aggregate more aggressively