Configuration Section
Persistence is configured within the<persistence> element:
Persistence Modes
HiveMQ Community Edition supports two persistence modes:File-Based Persistence (Default)
- Data is persisted to disk in the
datadirectory - Survives HiveMQ restarts
- Durable and reliable
- Suitable for production environments
- Slightly slower than in-memory mode
file
In-Memory Persistence
- All data is stored in RAM
- Faster performance
- Data is lost on HiveMQ restart
- Suitable for testing and development
- Not recommended for production
What Gets Persisted
The following data is affected by the persistence mode:- Client Sessions - Session state for connected and disconnected clients
- Queued Messages - QoS 1 and QoS 2 messages queued for delivery
- Retained Messages - Messages published with the retain flag
- Subscriptions - Client subscriptions
- Message Metadata - Message expiry, publish timestamps, etc.
Data Directory
When using file-based persistence, HiveMQ stores data in:persistence/- Retained messages and client session dataqueued_messages/- Messages queued for delivery
Performance Considerations
File-Based Persistence
Advantages:- Data durability and reliability
- Survives crashes and restarts
- Suitable for production workloads
- Disk I/O can be a bottleneck
- Requires sufficient disk space
- SSD storage recommended for better performance
In-Memory Persistence
Advantages:- Fastest performance
- No disk I/O overhead
- Ideal for testing and ephemeral workloads
- All data lost on restart
- Limited by available RAM
- Not suitable for production
Use Cases
File-Based Persistence
Use file-based persistence for:- Production deployments
- When data durability is required
- Long-lived client sessions
- Retained messages that must survive restarts
In-Memory Persistence
Use in-memory persistence for:- Development and testing
- Temporary or disposable data
- High-performance scenarios where data loss is acceptable
- Docker containers that don’t need state
Configuration Examples
Production Configuration
Development/Testing Configuration
Switching Persistence Modes
When switching from file-based to in-memory persistence:- Stop HiveMQ
- Update the configuration
- Optionally remove the
datadirectory - Start HiveMQ
- Stop HiveMQ
- Update the configuration
- Start HiveMQ (a new
datadirectory will be created)
Monitoring Persistence
Monitor the following to ensure healthy persistence:File-Based
- Disk space usage in the
datadirectory - Disk I/O metrics
- Write latency
In-Memory
- JVM heap usage
- Garbage collection metrics
- Available RAM
Best Practices
- Use file-based persistence in production for data durability
- Use SSD storage for better file-based persistence performance
- Monitor disk space when using file-based persistence
- Allocate sufficient heap when using in-memory persistence
- Backup the data directory regularly (file-based mode)
- Test failover scenarios to ensure your persistence configuration meets requirements
Related Configuration
- MQTT Settings - Configure message and session expiry
- Restrictions - Limit the number of connections and data
- Listeners - Configure client connections