Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does RabbitMQ actually store the message physically?

Tags:

rabbitmq

I want to know how does RabbitMQ store the messages physically in its RAM and Disk?

I know that RabbitMQ tries to keep the messages in memory (But I don't know how the messages are put in the Ram). But the messages can be spilled into disk when the messages are with persistent mode or when the broker has the memory pressure. (But I don't know how the messages are stored in Disk.)

I'd like to know the internals about these. Unfortunately, the official documentation in its homepage do not expose the internal details.

Which document should I read for this?

like image 309
syko Avatar asked Jul 18 '16 19:07

syko


People also ask

How are RabbitMQ messages stored?

By default, messages with a serialised size of less than 4096 bytes (including properties and headers) are stored in the queue index. Each queue index needs to keep at least one segment file in memory when reading messages from disk. The segment file contains records for 16,384 messages.

Where does RabbitMQ store its data?

In RabbitMQ versions starting with 3.7. 0 all messages data is combined in the msg_stores/vhosts directory and stored in a subdirectory per vhost. Each vhost directory is named with a hash and contains a . vhost file with the vhost name, so a specific vhost's message set can be backed up separately.

How does RabbitMQ deliver messages?

Applications can subscribe to have RabbitMQ push enqueued messages (deliveries) to them. This is done by registering a consumer (subscription) on a queue. After a subscription is in place, RabbitMQ will begin delivering messages. For each delivery a user-provided handler will be invoked.

Does RabbitMQ use memory?

By default, queues keep an in-memory cache of messages that's filled up as messages are published into RabbitMQ. The idea of this cache is to be able to deliver messages to consumers as fast as possible As the queue grows, it will require more memory.


1 Answers

RabbitMQ uses a custom DB to store the messages, the db is usually located here:

/var/lib/rabbitmq/mnesia/rabbit@hostname/queues 

Starting form the version 3.5.5 RabbitMQ introduced the new New Credit Flow https://www.rabbitmq.com/blog/2015/10/06/new-credit-flow-settings-on-rabbitmq-3-5-5/

Let’s take a look at how RabbitMQ queues store messages. When a message enters the queue, the queue needs to determine if the message should be persisted or not. If the message has to be persisted, then RabbitMQ will do so right away[3]. Now even if a message was persisted to disk, this doesn’t mean the message got removed from RAM, since RabbitMQ keeps a cache of messages in RAM for fast access when delivering messages to consumers. Whenever we are talking about paging messages out to disk, we are talking about what RabbitMQ does when it has to send messages from this cache to the file system.

This post blog is enough detailed.

I also suggest to read about lazy queue: https://www.rabbitmq.com/lazy-queues.html and https://www.rabbitmq.com/blog/2015/12/28/whats-new-in-rabbitmq-3-6-0/

Lazy Queues This new type of queues work by sending every message that is delivered to them straight to the file system, and only loading messages in RAM when consumers arrive to the queues. To optimize disk reads messages are loaded in batches.

like image 151
Gabriele Santomaggio Avatar answered Sep 26 '22 02:09

Gabriele Santomaggio