Skip to main content
โšก Calmops

InfluxDB Internals: Understanding the Time-Series Engine

Introduction

Understanding InfluxDB’s internal architecture helps you design better schemas, optimize queries, and troubleshoot performance issues. InfluxDB uses specialized data structures optimized for time-series data: the TSM (Time-Structured Merge) storage engine. This article explores the key components that make InfluxDB efficient at handling high-volume time-series data.

Storage Architecture

TSM Storage Engine

InfluxDB uses TSM (Time-Structured Merge) - a storage engine optimized for time-series data:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        InfluxDB                             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Query Engine    โ”‚   Write Path   โ”‚   Storage Engine       โ”‚
โ”‚                  โ”‚                โ”‚                        โ”‚
โ”‚  - InfluxQL     โ”‚   - Line Proto โ”‚   - TSM                โ”‚
โ”‚  - Functions    โ”‚   - Parser     โ”‚   - WAL                โ”‚
โ”‚  - Aggregations โ”‚   - Write API  โ”‚   - Shards             โ”‚
โ”‚                  โ”‚                โ”‚   - Compression        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Key Components

  1. WAL (Write-Ahead Log): First stop for incoming data
  2. Cache: In-memory buffer for recent writes
  3. TSM Files: Columnar storage on disk
  4. Shard: Organized by retention policy and time range

Write Path

When data is written to InfluxDB:

-- Write operation
INSERT cpu,host=server01 value=0.5

The write path:

  1. WAL Write: Data written to WAL immediately
  2. Cache Update: Data added to in-memory cache
  3. Response: Client receives confirmation
// Simplified write flow
func (w *Writer) WritePoint(p *Point) error {
    // 1. Write to WAL
    w.wal.Write(p)
    
    // 2. Update cache
    w.cache.Add(p)
    
    // 3. Return success
    return nil
}

Shard Management

Shards organize data by time range and retention policy:

-- View shards
SHOW SHARDS

-- Create retention policy with shard duration
CREATE RETENTION POLICY "one_week" ON "mydb" 
  DURATION 1w 
  SHARD DURATION 1d 
  REPLICATION 1

Shard Structure

/var/lib/influxdb/data/mydb/one_week/
โ”œโ”€โ”€ 000000001-000000001.tsm      # TSM file
โ”œโ”€โ”€ 000000002-000000002.tsm
โ”œโ”€โ”€ 000000003-000000003.tsm
โ””โ”€โ”€ .index                        # Index file

Each shard contains:

  • Multiple TSM files
  • Index for fast lookups
  • Min/Max time indices

TSM File Format

TSM (Time-Structured Merge) files store data efficiently:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           TSM File                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Header (Magic, Version)           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Index Block                       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚Col 1 โ”‚Col 2 โ”‚Col 3 โ”‚ ...  โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Data Block 1 (columnar)           โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ Timestamps                   โ”‚ โ”‚
โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚
โ”‚  โ”‚ Values                       โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Data Block 2                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Compression

TSM uses multiple compression algorithms:

Data Type Compression
Timestamps Delta-of-delta encoding
Float values Gorilla compression
Integers RLE or snappy
Strings Snappy compression

Example compression effectiveness:

-- Query shows compression ratio
SHOW SERIES CARDINALITY ON mydb

-- InfluxDB internally compresses:
-- Raw: 1,000,000 points ร— 8 bytes = 8 MB
-- Compressed: ~0.5 MB (16:1 ratio typical)

WAL (Write-Ahead Log)

The WAL ensures durability:

// WAL structure
type WAL struct {
    // Write-ahead log files
    // Each entry is encoded point data
    // fsynced before acknowledging write
}

Properties:

  • Append-only
  • Memory-mapped files
  • Crash-safe
  • Size: ~10MB per file

Query Execution

Query Flow

SELECT mean(value) FROM cpu WHERE time > now() - 1h GROUP BY time(5m)

Steps:

  1. Parse: Parse InfluxQL to AST
  2. Plan: Create execution plan
  3. Read: Read from TSM files
  4. Aggregate: Apply aggregation
  5. Return: Return results

Query Planning

-- EXPLAIN shows query plan
EXPLAIN SELECT mean(value) FROM cpu

-- Result:
-- Plan: MapReadGroup -> MeanReducer -> HTTPResponse

Time-Based Pruning

InfluxDB efficiently skips irrelevant data:

-- Query with time filter
SELECT * FROM cpu WHERE time > now() - 1h

-- InfluxDB:
-- 1. Check shard time ranges
-- 2. Skip shards outside time range
-- 3. Use index to find relevant blocks

Caching

In-memory cache for recent reads:

# Cache configuration
[storage]
  cache-max-memory-size = "8g"
  cache-snapshot-memory-size = "1g"
  cache-snapshot-write-cold-duration = "10m"

Cache Structure

// Cache entry
type CacheEntry struct {
    SeriesID uint64
    Values   []Value  // sorted by timestamp
}

Cache behavior:

  • Sorted by series key + timestamp
  • Flushed to TSM when full or after duration
  • Compacts during flush

Compaction

Background process to optimize storage:

// Compaction levels
const (
    Level0Compaction = iota  // Minor, within shard
    Level1Compaction        // Merge TSM files
    Level2Compaction        // Major merge
    FullCompaction          // Full optimization
)

Compaction Triggers

[compaction]
  max-concurrent-compactions = 4
  compact-throughput = "50m"
  compact-throughput-burst = "100m"

Index Architecture

Series Index

InfluxDB maintains a series index:

-- Show all series
SHOW SERIES ON mydb

-- Series cardinality
SHOW SERIES CARDINALITY ON mydb

-- Index on tags creates series
CREATE INDEX ON cpu (host)   -- Creates series: cpu,host=server01
CREATE INDEX ON cpu (region) -- Creates series: cpu,region=us-west

Tag Index

Tag lookups are optimized:

// Tag index structure
type TagIndex struct {
    // Maps tag values to series IDs
    // Fast O(1) lookups
}

Memory Management

Understanding memory usage:

# View memory usage
curl http://localhost:8086/debug/vars | jq '.memstats'

# Key metrics
# - Alloc: Current memory allocated
# - Sys: Total memory from OS
# - NumGC: Number of garbage collections

Memory pools:

  • Series index: Maps series keys to IDs
  • Field index: Maps fields to types
  • Cache: Recent time-series data
  • WAL buffer: Write-ahead log

Query Performance Characteristics

Operation Complexity
Point lookup by timestamp O(log n)
Range query O(k + n) where k = blocks scanned
Aggregation O(n)
GROUP BY time O(n)
JOIN O(n ร— m)

Optimizing Queries

-- Good: Use time filter
SELECT * FROM cpu WHERE time > now() - 1h

-- Bad: No time filter (full scan)
SELECT * FROM cpu

-- Good: Limit fields
SELECT host, value FROM cpu

-- Bad: Select all fields
SELECT * FROM cpu

Data Retention

Automatic data lifecycle:

-- Create retention policy
CREATE RETENTION POLICY "one_day" ON "mydb"
  DURATION 1d
  SHARD DURATION 1h
  REPLICATION 1

-- View retention policies
SHOW RETENTION POLICIES ON mydb

Shard duration determines data granularity:

  • Short duration: More shards, faster deletes
  • Long duration: Fewer shards, better compression

Conclusion

InfluxDB’s architecture is optimized for time-series workloads. The TSM storage engine provides efficient compression, the WAL ensures durability, and intelligent query planning minimizes IO. Understanding these internals helps you design schemas that leverage these optimizations: use appropriate time ranges, minimize series cardinality, and filter by time in queries.

In the next article, we’ll explore recent InfluxDB developments and trends for 2025-2026.

Resources

Comments