RocketMQ Secrets: Sequential Writes, Page Cache, Async Flush, Zero‑Copy
This article explains how RocketMQ achieves high throughput by using sequential disk writes, OS page caching, asynchronous flushing, and zero‑copy techniques, detailing each mechanism and providing code examples. It also discusses the impact on disk seek time and data durability.
Disk Sequential Write
RocketMQ writes messages sequentially to disk, which greatly reduces disk seek time and improves write efficiency. Messages are stored in a CommitLog, a sequential log file; new messages are appended to the end, ensuring ordered writes and high performance.
Page Cache Technology
RocketMQ leverages the operating system's page cache to accelerate writes. Data is first written to a MappedByteBuffer, which maps to a memory region backed by the file, causing the data to reside in the OS page cache before being flushed to disk.
byte[] messageData = "example message".getBytes();<br/>mappedByteBuffer.put(messageData);The system periodically flushes the page cache to disk, using an asynchronous flush strategy that writes data to the cache first and lets the OS persist it at an appropriate time.
Asynchronous Flushing
Asynchronous flushing (ASYNC_FLUSH) returns immediately after a message is written, boosting throughput by avoiding the latency of synchronous disk writes. While this improves performance, it introduces a risk of data loss if the system crashes before the cache is persisted.
Zero‑Copy
Zero‑copy reduces the number of memory copies during data transmission and storage, enhancing speed. Traditional data transfer involves copying from user space to kernel space, then to disk or network buffers, and back again. Zero‑copy eliminates these extra copies.
On Linux, the sendfile system call can transfer file data directly from a file descriptor to a network socket, bypassing user‑space buffers.
ssize_t sent_bytes = sendfile(socket_fd, file_fd, NULL, file_size);These techniques collectively enable RocketMQ to achieve high performance and low latency in large‑scale messaging systems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
