First, some background: both persistent and transient messages can be written to disk. Persistent messages will be written to disk as soon as they reach the queue, while transient messages will be written to disk only so that they can be evicted from memory while under memory pressure.
持久化消息和临时(transient)消息都可以写到磁盘中。持久化消息只要到了队列,就会被写入磁盘;而临时消息只有在内存压力(memory pressure)时被换出到磁盘中。
The persistence layer has two components: the queue index and the message store. The queue index is responsible for maintaining knowledge about where a given message is in a queue, along with whether it has been delivered and acknowledged. There is therefore one queue index per queue.
The message store is a key-value store for messages, shared among all queues in the server. Messages (the body, and any properties and / or headers) can either be stored directly in the queue index, or written to the message store. There are technically two message stores (one for transient and one for persistent messages) but they are usually considered together as "the message store".
Messages in the queue index
There are advantages and disadvantages to writing messages to the queue index.
Advantages:
- Messages can be written to disk in one operation rather than two; for tiny messages this can be a substantial gain.
- Messages that are written to the queue index do not require an entry in the message store index and thus do not have a memory cost when paged out.
Disadvantages:
- The queue index keeps blocks of a fixed number of records in memory; if non-tiny messages are written to the queue index then memory use can be substantial.
- If a message is routed to multiple queues by an exchange, the message will need to be written to multiple queue indices. If such a message is written to the message store, only one copy needs to be written.
- Unacknowledged messages whose destination is the queue index are always kept in memory.