Introduction
KahaDB is ActiveMQ's default persistence store using a journal file and index. After unclean shutdowns (power loss, crash, kill -9), the index can become corrupted, preventing message recovery or causing startup failures.
Symptoms
Corrupted index:
```bash $ tail -f /var/log/activemq/activemq.log
[ERROR] KahaDB index corrupted: Cannot read index file [ERROR] java.io.IOException: Invalid index file header [WARN] KahaDB recovery failed, starting with empty store ```
Startup failure:
[ERROR] Failed to start KahaDB persistence adapter
[ERROR] Index file checksum mismatch: expected 12345, got 67890
java.io.IOException: Could not locate index fileMissing messages:
[WARN] Recovered 0 messages from KahaDB store
[INFO] Previous store had 5000 pending messages, now 0Common Causes
- 1.Unclean shutdown - Power loss, crash, or kill -9
- 2.Disk corruption - Filesystem or hardware issues
- 3.Journal file incomplete - Write interrupted mid-operation
- 4.Checkpoint failure - Index not properly synchronized
- 5.Disk full - Journal writes failed due to space
- 6.Concurrent access - Multiple brokers accessing same store
Step-by-Step Fix
Step 1: Check KahaDB Store Status
```bash # Check KahaDB directory ls -la /var/lib/activemq/kahadb/
# Files: # db-1.log - Journal file # db.data - Index file # db.redo - Redo log
# Check file sizes du -h /var/lib/activemq/kahadb/
# Check journal file count ls /var/lib/activemq/kahadb/db-*.log | wc -l
# Verify file integrity md5sum /var/lib/activemq/kahadb/db.data
# Check disk space df -h /var/lib/activemq/ ```
Step 2: Stop ActiveMQ and Backup
```bash # Stop ActiveMQ systemctl stop activemq
# Backup KahaDB store before recovery cp -r /var/lib/activemq/kahadb /var/lib/activemq/kahadb.backup
# Create timestamped backup tar -czf kahadb-backup-$(date +%Y%m%d-%H%M%S).tar.gz /var/lib/activemq/kahadb/
# Verify backup ls -la /var/lib/activemq/kahadb.backup/ ```
Step 3: Enable KahaDB Recovery Mode
```xml <!-- In activemq.xml, enable recovery --> <persistenceAdapter> <kahaDB directory="${activemq.data}/kahadb" ignoreCorruptedJournalFiles="true" checkForCorruptedJournalFiles="true" checksumJournalFiles="true" forceRecoverIndex="true"> </kahaDB> </persistenceAdapter>
<!-- Key recovery options --> <!-- ignoreCorruptedJournalFiles: Skip corrupted journal files --> <!-- checkForCorruptedJournalFiles: Check integrity on startup --> <!-- forceRecoverIndex: Rebuild index from journal --> ```
Step 4: Rebuild Index from Journal
```bash # Delete corrupted index file rm /var/lib/activemq/kahadb/db.data rm /var/lib/activemq/kahadb/db.redo
# ActiveMQ will rebuild index from journal files on startup # Start ActiveMQ systemctl start activemq
# Watch recovery process tail -f /var/log/activemq/activemq.log | grep -i "kahadb|recover|index"
# Expected output: # [INFO] KahaDB recovery: rebuilding index from journal # [INFO] Recovered 4500 messages from journal files ```
Step 5: Use External Recovery Tool
```bash # If automatic recovery fails, use KahaDB recovery tool # Download activemq-kahadb-recovery tool git clone https://github.com/apache/activemq-kahadb-recovery.git cd activemq-kahadb-recovery mvn package
# Run recovery java -jar target/activemq-kahadb-recovery.jar /var/lib/activemq/kahadb.backup/
# This reads journal files and outputs message data # Output can be used to restore messages manually
# For corrupt journal files, try partial recovery java -jar target/activemq-kahadb-recovery.jar /var/lib/activemq/kahadb.backup/ --partial ```
Step 6: Export and Re-import Messages
```bash # If index is corrupted but journal intact # Export messages from journal to file
# Using activemq-admin activemq-admin export -- kahadb.directory=/var/lib/activemq/kahadb.backup export.file=/tmp/messages.xml
# Clean store and re-import rm -rf /var/lib/activemq/kahadb/*
# Start ActiveMQ with clean store systemctl start activemq
# Import messages activemq-admin import -- import.file=/tmp/messages.xml
# Verify message count curl -u admin:admin http://localhost:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost/TotalMessageCount ```
Step 7: Configure Checkpoint Settings
```xml <!-- In activemq.xml, optimize checkpoint frequency --> <persistenceAdapter> <kahaDB directory="${activemq.data}/kahadb" checkpointInterval="5000" cleanupInterval="30000" journalMaxFileLength="32mb" indexCacheSize="10000" enableIndexDiskSyncs="true" enableJournalDiskSyncs="true"> </kahaDB> </persistenceAdapter>
<!-- Key checkpoint settings --> <!-- checkpointInterval: How often to sync index (ms) --> <!-- cleanupInterval: How often to cleanup old journal files --> <!-- journalMaxFileLength: Size of each journal file --> <!-- indexCacheSize: Number of cached index pages -->
<!-- More frequent checkpoints reduce corruption risk --> <!-- But increase disk I/O --> ```
Step 8: Enable Write Syncing
```xml <!-- Force disk syncs for durability --> <persistenceAdapter> <kahaDB directory="${activemq.data}/kahadb" enableIndexDiskSyncs="true" enableJournalDiskSyncs="true" syncOnWrite="true"> </kahaDB> </persistenceAdapter>
<!-- syncOnWrite ensures each write is synced to disk --> <!-- Prevents corruption from unclean shutdown --> <!-- May reduce performance -->
<!-- Alternative: Use fdatasync instead of fsync --> <kahaDB directory="${activemq.data}/kahadb" indexWriteBatchSize="1000" indexCacheSize="10000"/> ```
Step 9: Check Disk Health
```bash # Check disk for errors # Linux: dmesg | grep -i "I/O error|disk|sda"
# Check filesystem fsck -n /dev/sda1 # Read-only check
# Check disk SMART status smartctl -a /dev/sda
# Look for: # - Reallocated sectors # - Pending sectors # - Read error rates
# Check disk performance hdparm -tT /dev/sda
# If disk issues found, replace disk and restore from backup ```
Step 10: Configure Clean Shutdown
```bash # Ensure proper shutdown procedure # Use systemctl stop, not kill
# If manual shutdown needed: activemq stop
# This ensures: # - Pending writes flushed # - Index checkpointed # - Journal files closed
# Never use kill -9 on ActiveMQ # If stuck, try: kill -TERM <pid>
# Configure shutdown timeout in activemq.xml <broker shutdownOnSlaveStop="true" shutdownOnMasterFailure="true"> ```
KahaDB Recovery Settings
| Setting | Default | Recommended |
|---|---|---|
| checkpointInterval | 10000ms | 5000ms |
| journalMaxFileLength | 32mb | 32-128mb |
| enableJournalDiskSyncs | true | true |
| ignoreCorruptedJournalFiles | false | true (for recovery) |
| forceRecoverIndex | false | true (for recovery) |
Verification
```bash # After recovery # Check KahaDB status curl -u admin:admin http://localhost:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost/PersistenceAdapter
# Check message count curl -u admin:admin http://localhost:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost/TotalMessageCount
# Verify queue depths activemq-admin query --objname type=Broker,destinationType=Queue --attribute QueueSize
# Check KahaDB directory ls -la /var/lib/activemq/kahadb/
# Should have: # - db.data (new index) # - db.redo (redo log) # - db-*.log (journal files)
# Test message persistence activemq-admin send --queue test.queue --message "test" --persistent true activemq-admin browse --queue test.queue
# Restart and verify persistence systemctl restart activemq activemq-admin browse --queue test.queue ```
Prevention
To prevent ActiveMQ Kaha PIndex corrupted issues from recurring, implement these proactive measures:
1. Monitor KahaDB Health
groups:
- name: activemq-persistence
rules:
- alert: ActiveMQKahaDBCorruption
expr: |
activemq_kahadb_corruption_detected > 0
for: 1m
labels:
severity: critical
annotations:
summary: "ActiveMQ KahaDB corruption detected"2. Configure Journal Synchronization
```xml <!-- activemq.xml --> <persistenceAdapter> <kahaDB directory="${activemq.data}/kahadb" journalMaxFileLength="32mb" checkpointInterval="5000" cleanupInterval="30000" syncOnWrite="true" concurrentStoreAndDispatchQueues="true"/> </persistenceAdapter>
<!-- syncOnWrite ensures data integrity --> ```
3. Set Up Regular Backups
```bash # Backup KahaDB directory cat << 'EOF' > /usr/local/bin/backup_activemq.sh #!/bin/bash BACKUP_DIR=/backup/activemq DATA_DIR=/var/lib/activemq/kahadb DATE=$(date +%Y%m%d_%H%M%S)
# Stop ActiveMQ for consistent backup systemctl stop activemq
# Create backup tar -czf $BACKUP_DIR/kahadb_$DATE.tar.gz $DATA_DIR
# Restart ActiveMQ systemctl start activemq
# Keep last 7 backups find $BACKUP_DIR -name "kahadb_*.tar.gz" -mtime +7 -delete EOF
chmod +x /usr/local/bin/backup_activemq.sh ```
Best Practices Checklist
- [ ] Monitor KahaDB health
- [ ] Configure journal synchronization
- [ ] Set up regular backups
- [ ] Use UPS for power protection
- [ ] Monitor disk health
- [ ] Test recovery procedures
Related Issues
- [Fix ActiveMQ Destination Full](/articles/fix-activemq-destination-full)
- [Fix ActiveMQ JDBC Lock Expired](/articles/fix-activemq-jdbc-lock-expired)
- [Fix Disk I/O Error](/articles/fix-disk-io-error)
Related Articles
- [Fix Fix Activemq Broker Down Issue in Messaging](fix-activemq-broker-down)
- [Fix ActiveMQ Destination Full](fix-activemq-destination-full)
- [Fix ActiveMQ JDBC Lock Expired](fix-activemq-jdbc-lock-expired)
- [Fix ActiveMQ Master Slave Failover](fix-activemq-master-slave-failover)
- [Fix ActiveMQ Slow Consumer](fix-activemq-slow-consumer)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix ActiveMQ Kaha PIndex Corrupted", "description": "Troubleshoot ActiveMQ KahaDB index corruption. Rebuild index, recover messages, and configure checkpoint settings.", "url": "https://www.fixwikihub.com/fix-activemq-kaha-pindex-corrupted", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-04-04T01:30:54.880Z", "dateModified": "2026-04-04T01:30:54.880Z" } </script>