Skip to main content

RabbitMQ best practices

I have worked with RabbitMQ a long time. Its easy to make various mistakes which prevent you from getting the best out of it. Each application has its own performance requirements, like some of them require really high throughput while others process batch jobs where its fine if there are some delays. 

  • RabbitMQ queues are single threaded: Since RabbitMQ queues are single threaded, one queue can handle max 50k messages/sec (also depends on available RAM and swap size).
  • Don’t let your queues grow too long: Since RabbitMQ stores all unconsumed messages in RAM, if your queues start to grow too much it will lead to memory and performance issues on broker. RabbitMQ will start throttling your publishers using Flow Control. If your memory consumption breaches a preset threshold, broker will stop accepting any new messages altogether.
  • Use lazy queues for batch publishing jobs: Lazy queues automatically store all the messages to the disk. They are loaded into the memory only when requested by the consumer. This helps with the memory consumption issues in broker. You can use this option, if your consumers are fine with somewhat higher latencies.
  • Set TTL/max-length for all the queues to limit size: This will help you ensure that your queues don’t grow beyond a certain size and cause memory issues on the broker.
  • Limit Number of queues: Since RabbitMQ management interface tracks and calculates several metrics for all the queues on the cluster, having too many queues might cause performance issues.
  • Always have separate connections for Publishers and Consumers: RabbitMQ applies flow control on publishers if consumers can’t keep up with them, which limits the bandwidth of connection. If your consumers and publishers share the same connection, then your consumers will also be throttled by RabbitMQ, which might lead to a cascading effect bringing down the broker.
  • Persistent messages and Durable queues: If you want your queues and messages to survive broker restarts and crashes, then declare your queues as durable and set delivery mode to persistent for messages. However, performance would be impacted by persistent messages as they have to be always stored in the disk.
  • Prefetch count: Prefetch count determines how many messages will be delivered to the consumer at any point of time before receiving acknowledgement. Having too small of a prefetch count would hurt performance, as broker will be waiting for the acknowledgement most of the time. If you have a very high prefetch count, it might lead to all messages being delivered to a single consumer, resulting in other consumers sitting idle. You need to figure out the correct prefetch value for your use case based on number of consumers, network latency, consumer latency.

Comments

Popular posts from this blog

Monitoring Spring Boot API response times with Prometheus

Prometheus is a very useful tool for monitoring web applications. In this blog post, we will see how to use it to monitor Spring Boot API response times. You have to include following dependencies in your build.gradle file: compile group: 'io.prometheus', name: 'simpleclient_hotspot', version: '0.0.26' compile group: 'io.prometheus', name: 'simpleclient_servlet', version: '0.0.26' compile group: 'io.prometheus', name: 'simpleclient', version: '0.0.26' Now you will have to expose a Rest Endpoint, so that Prometheus can collect the metrics by scraping it at regular intervals. To do that, you would have to include these Java configuration classes: @Configuration @ConditionalOnClass(CollectorRegistry.class) public class Config { private static final CollectorRegistry metricRegistry = CollectorRegistry. defaultRegistry ; @Bean ServletRegistrationBean registerPrometheusExporterServlet() { retu...

Redis Pipelines and Transactions with Golang

Redis is an in memory datastore mostly used as a cache. Clients can send commands to server using TCP protocol and get the response back. So, usually a request works like this. Client sends a request to server and waits for the response to be sent back. Server processes the command and writes the response to the socket for client to read. Sometimes, in application flow we have to process multiple keys at once. In this case, for each request there will be a network overhead for the round trip between server and client. We can reduce this network overhead by sending commands to the Redis server in a batched manner and then process all the responses at once. This can be achieved using pipelines as well as transactions. Pipelining in Redis is when you send all the commands to the server at once and receive all the replies in one go. It does not provide any guarantees like no other commands will be processed between your pipelined commands. Transactions in Redis are meant to be ...

Redis Pipelines with Python

Redis is an in memory datastore mostly used as a cache. Clients can send commands to server using TCP protocol and get the response back. So, usually a request works like this. Client sends a request to server and waits for the response to be sent back. Server processes the command and writes the response to the socket for client to read. Sometimes, in application flow we have to retrieve multiple keys at once. In this case, for each request there will be a network overhead for the round trip between server and client. We can reduce this network overhead by sending commands to the Redis server in a batched manner and then process all the responses at once. For eg., using Python we can use pipeline like this: Connect to Redis server first. You can change the host parameter to the IP address/DNS of your Redis server. import redis r = redis.Redis(host='localhost', port=6379, db=0) Create a pipeline instance using the pipeline method pipe = r.pipeline() Send the c...