Prometheus WAL Corruption After Crash Fix
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-compressionfor smaller, more resilient WAL files - Configure remote write for off-server data redundancy
- Use graceful shutdown (
SIGTERMnotSIGKILL) - Monitor Prometheus disk space — full disk causes WAL corruption
- Run
promtool tsdb analyzeperiodically for health checks
Common Mistakes with wal replay
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro