Configure Connection Timeout in RabbitMQ: Simple Guide
To configure connection timeout in RabbitMQ, set the
heartbeat interval in the RabbitMQ server configuration or client connection settings. The heartbeat controls how often the server and client check the connection health, effectively managing connection timeouts.Syntax
The connection timeout in RabbitMQ is controlled mainly by the heartbeat setting. It is specified in seconds and can be set in the server configuration file or the client connection parameters.
In the server config, it looks like this:
heartbeat = <seconds>- sets the heartbeat interval.
In client libraries, it is usually passed as a parameter when creating the connection, for example:
heartbeat=seconds
conf
# Server configuration example (rabbitmq.conf) heartbeat = 60 # Client connection example (Python pika) connection = pika.BlockingConnection(pika.ConnectionParameters(heartbeat=60))
Example
This example shows how to set a 30-second heartbeat timeout in the RabbitMQ server configuration and a Python client using pika library.
python
# rabbitmq.conf heartbeat = 30 # Python client example import pika params = pika.ConnectionParameters('localhost', heartbeat=30) connection = pika.BlockingConnection(params) channel = connection.channel() print('Connected with 30s heartbeat timeout') connection.close()
Output
Connected with 30s heartbeat timeout
Common Pitfalls
Common mistakes when configuring connection timeout in RabbitMQ include:
- Setting heartbeat too low, causing frequent unnecessary disconnects.
- Not matching heartbeat values between client and server, which can cause connection drops.
- Confusing
heartbeatwith TCP socket timeout settings, which are different.
Always ensure both client and server heartbeat settings align for stable connections.
conf
# Wrong: Client heartbeat 10s, server heartbeat 60s # This mismatch can cause unexpected disconnects # Right: Both client and server heartbeat set to 60s heartbeat = 60 connection = pika.BlockingConnection(pika.ConnectionParameters(heartbeat=60))
Quick Reference
| Setting | Description | Example Value |
|---|---|---|
| heartbeat | Interval in seconds for connection health checks | 30 |
| tcp_listen_options.backlog | TCP backlog queue size (not timeout) | 128 |
| client heartbeat param | Heartbeat value passed to client connection | 30 |
Key Takeaways
Set the heartbeat interval in both RabbitMQ server and client to control connection timeout.
Heartbeat values must match on client and server to avoid unexpected disconnects.
Heartbeat controls connection health checks, not TCP socket timeout.
Configure heartbeat in rabbitmq.conf for server and connection parameters for clients.
Avoid setting heartbeat too low to prevent frequent connection drops.