Skip to content

Prometheus WAL Corruption After Crash Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Prometheus WAL Corruption After Crash Fix. We cover key concepts, practical examples, and best practices.

Your Prometheus fails to start after a crash — logs show TSDB: corrupted WAL or unexpected error in WAL replay. The write-ahead log was damaged by an unexpected shutdown, disk error, or out-of-memory kill.

The Problem

level=error msg="TSDB: corrupted WAL" component=tsdb
level=error msg="Failed to open TSDB" err="error replaying WAL: unexpected EOF"

Prometheus stores recent data in the WAL before compacting into blocks. A crash during a write can corrupt the WAL, preventing Prometheus from starting.

Step-by-Step Fix

1. Remove the corrupted WAL

# Stop Prometheus
systemctl stop prometheus

# Find the TSDB path (default: /var/lib/prometheus/data)
cd /var/lib/prometheus/data

# Backup the WAL
cp -r wal wal-corrupted

# Remove the corrupted WAL
rm -rf wal

# Restart Prometheus
systemctl start prometheus

Warning: This loses any data that was in the WAL but not yet compacted (typically the last ~2 hours of data).

2. Use the TSDB repair tool

# Check TSDB health
promtool tsdb analyze /var/lib/prometheus/data

# Attempt repair
promtool tsdb repair /var/lib/prometheus/data

# If repair fails, check which blocks are corrupted
promtool tsdb list /var/lib/prometheus/data

3. Recover with WAL replay mode

# Start Prometheus with strict WAL replay
prometheus --storage.tsdb.retention.time=15d \
  --storage.tsdb.path=/var/lib/prometheus/data \
  --storage.tsdb.wal-replay-mode=strict

# If strict fails, use memory mode (loses unreplayed data)
prometheus --storage.tsdb.path=/var/lib/prometheus/data \
  --storage.tsdb.wal-replay-mode=memory

4. Prevent future corruption

# Prometheus startup flags for resilience
--storage.tsdb.wal-compression  # Enable WAL compression (reduces size)
--storage.tsdb.wal-segment-size=128MB  # Smaller segments = less data loss
--storage.tsdb.no-lockfile  # Avoid stale lockfile issues

5. Set up remote write for redundancy

# prometheus.yml
remote_write:
  - url: http://mimir:9009/api/v1/push
    queue_config:
      capacity: 10000
      max_shards: 10

# With remote write, data is safely stored off-server
# WAL corruption only affects the local Prometheus instance

Expected output after recovery:

TSDB started successfully
WAL: 0 segments found (clean start)
Blocks: 420 blocks loaded
Data loss: ~2 hours (WAL-to-block gap)

Prevention Tips

  • Enable --storage.tsdb.wal-compression for smaller, more resilient WAL files
  • Configure remote write for off-server data redundancy
  • Use graceful shutdown (SIGTERM not SIGKILL)
  • Monitor Prometheus disk space — full disk causes WAL corruption
  • Run promtool tsdb analyze periodically for health checks

Common Mistakes with wal replay

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How much data is lost when the WAL is corrupted?

The WAL holds data that has not yet been compacted into TSDB blocks — typically the last 2 hours. Data in completed blocks (older than ~2 hours) is safe on disk. The WAL is replayed on startup to restore recent data; a corrupted WAL loses only the unrecoverable portion.

Can I prevent WAL corruption entirely?

WAL corruption is a disk-level issue. Use reliable storage, monitor disk health with SMART metrics, and ensure clean shutdown. Enable wal-compression which reduces I/O. Use remote write to Mimir or Thanos for critical data that cannot tolerate any loss.

What's the difference between WAL replay modes?

strict (default) — fails on any corruption, requiring manual repair. memory — loads as much WAL data as possible, skipping corrupted segments. Use memory for automatic recovery, strict when data integrity is critical and you want to investigate corruption.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro