Chronicle Queue is a broker-less, off-heap Java library for ultra-low-latency, persisted messaging at millions of events/sec.
Chronicle Queue is a persisted low-latency messaging framework for high performance applications. It supports multiple writers to a queue via locking, and multiple lock-less concurrent readers of the queue.
This project covers the Java version of Chronicle Queue. A C++ version of this project is also available and supports Java/C++ interoperability plus additional language bindings e.g. Python. If you are interested in evaluating the C++ version please contact [email protected].
At first glance Chronicle Queue can be seen as simply another queue implementation. However, it has major design choices that should be emphasised. Using off-heap storage, Chronicle Queue provides an environment where applications do not suffer from Garbage Collection (GC). When implementing high-performance and memory-intensive applications (you heard the fancy term "bigdata"?) in Java, one of the biggest problems is garbage collection.
Chronicle Queue allows messages to be added to the end of a queue ("appended"), read from the queue ("tailed"), and also supports random-access seek.
A number of relevant system properties are listed in systemProperties.adoc.
You could consider a Chronicle Queue to be similar to a low latency broker-less durable/persisted topic that can contain messages of different types and sizes. Chronicle Queue is a distributed unbounded persisted queue that:
-
supports asynchronous RMI and Publish/Subscribe interfaces with microsecond latencies.
-
passes messages between JVMs in under a microsecond
-
passes messages between JVMs on different machines via replication in under 10 microseconds (Enterprise feature)
-
provides stable, soft real-time latencies into the millions of messages per second for a single thread to one queue; with total ordering of every event.
When publishing 40-byte messages, a high percentage of the time we achieve latencies under 1 microsecond. The 99th percentile latency is the worst 1 in 100, and the 99.9th percentile is the worst 1 in 1000 latency.
| Batch Size | 10 million events per minute | 60 million events per minute | 100 million events per minute |
|---|---|---|---|
99%ile |
0.78 µs |
0.78 µs |
1.2 µs |
99.9%ile |
1.2 µs |
1.3 µs |
1.5 µs |
| Batch Size | 10 million events per minute | 60 million events per minute | 100 million events per minute |
|---|---|---|---|
99%ile |
20 µs |
28 µs |
176 µs |
99.9%ile |
901 µs |
705 µs |
5,370 µs |
|
Note
|
100 million events per minute is sending an event every 660 nanoseconds; replicated and persisted. |
|
Important
|
This performance is not achieved using a large cluster of machines. This is using one thread to publish, and one thread to consume. |
Chronicle Queue is designed to:
-
be a "record everything store" which can read with microsecond real-time latency. This supports even the most demanding High Frequency Trading systems. However, it can be used in any application where the recording of information is a concern.
-
support reliable replication with notification to either the appender (writer of message) or a tailer (reader of message), when a message has been successfully replicated.
Chronicle Queue assumes disk space is cheap compared with memory. Chronicle Queue makes full use of the disk space you have, and so you are not limited by the main memory of your machine. If you use spinning HDD, you can store many TBs of disk space for little cost.
The only extra software that Chronicle Queue needs to run is the operating system. It doesn’t have a broker; instead it uses your operating system to do all the work. If your application dies, the operating system keeps running for seconds longer, so no data is lost; even without replication.
As Chronicle Queue stores all saved data in memory-mapped files, this has a trivial on-heap overhead, even if you have over 100 TB of data.
Chronicle put significant effort into achieving very low latency. In other products which focus on support of web applications, latencies of less than 40 milliseconds are fine as they are faster than you can see; for example, the frame rate of cinema is 24 Hz, or about 40 ms.
Chronicle Queue aims to achieve latencies of under 40 microseconds for 99% to 99.99% of the time. Using Chronicle Queue without replication, we support applications with latencies below 40 microseconds end-to-end across multiple services. Often the 99% latency of Chronicle Queue is entirely dependent on the choice of operating system and hard disk sub-system.
Replication for Chronicle Queue supports Chronicle Wire Enterprise. This supports a real-time compression which calculates the deltas for individual objects, as they are written. This can reduce the size of messages by a factor of 10, or better, without the need for batching; that is, without introducing significant latency.
Chronicle Queue also supports LZW, Snappy, and GZIP compression. These formats however add significant latency. These are only useful if you have strict limitations on network bandwidth.
Chronicle Queue supports a number of semantics:
-
Every message is replayed on restart.
-
Only new messages are played on restart.
-
Restart from any known point using the index of the entry.
-
Replay only the messages you have missed. This is supported directly using the methodReader/methodWriter builders.
On most systems System.nanoTime() is roughly the number of nanoseconds since the system last rebooted (although different JVMs may behave differently).
This is the same across JVMs on the same machine, but wildly different between machines.
The absolute difference when it comes to machines is meaningless.
However, the information can be used to detect outliers; you can’t determine what the best latency is, but you can determine how far off the best latencies you are.
This is useful if you are focusing on the 99th percentile latencies.
We have a class called RunningMinimum to obtain timings from different machines, while compensating for a drift in the nanoTime between machines.
The more often you take measurements, the more accurate this running minimum is.
Chronicle Queue manages storage by cycle.
You can add a StoreFileListener which will notify you when a file is added, and when it is no longer retained.
You can move, compress, or delete all the messages for a day, at once.
NOTE : Unfortunately on Windows, if an IO operation is interrupted, it can close the underlying FileChannel.
Due to performance reasons, we have removed checking for interrupts in the chronicle queue code. Because of this, we recommend that you avoid using chronicle queue with code that generates interrupts. If you can not avoid generating interrupts then we suggest that you create a separate instance of Chronicle Queue per thread.
Chronicle Queue is most often used for producer-centric systems where you need to retain a lot of data for days or years. For statistics see Usage of Chronicle-Queue
|
Important
|
Chronicle Queue does not support operating off any network file system, be it NFS, AFS, SAN-based storage or anything else. The reason for this is those file systems do not provide all the required primitives for memory-mapped files Chronicle Queue uses. If any networking is needed (e.g. to make the data accessible to multiple hosts), the only supported way is Chronicle Queue Replication (Enterprise feature). |
Most messaging systems are consumer-centric. Flow control is implemented to avoid the consumer ever getting overloaded; even momentarily. A common example is a server supporting multiple GUI users. Those users might be on different machines (OS and hardware), different qualities of network (latency and bandwidth), doing a variety of other things at different times. For this reason it makes sense for the client consumer to tell the producer when to back off, delaying any data until the consumer is ready to take more data.
Chronicle Queue is a producer-centric solution and does everything possible to never push back on the producer, or tell it to slow down. This makes it a powerful tool, providing a big buffer between your system, and an upstream producer over which you have little, or no, control.
Market data publishers don’t give you the option to push back on the producer for long; if at all. A few of our users consume data from CME OPRA. This produces peaks of 10 million events per minute, sent as UDP packets without any retry. If you miss, or drop a packet, then it is lost. You have to consume and record those packets as fast as they come to you, with very little buffering in the network adapter. For market data in particular, real time means in a few microseconds; it doesn’t mean intra-day (during the day).
Chronicle Queue is fast and efficient, and has been used to increase the speed that data is passed between threads. In addition, it also keeps a record of every message passed allowing you to significantly reduce the amount of logging that you need to do.
Compliance systems are required by more and more systems these days. Everyone has to have them, but no one wants to be slowed down by them. By using Chronicle Queue to buffer data between monitored systems and the compliance system, you don’t need to worry about the impact of compliance recording for your monitored systems. Again, Chronicle Queue can support millions of events per-second, per-server, and access data which has been retained for years.
Chronicle Queue supports low latency IPC (Inter Process Communication) between JVMs on the same machine in the order of magnitude of 1 microsecond; as well as between machines with a typical latency of 10 microseconds for modest throughputs of a few hundred thousands. Chronicle Queue supports throughputs of millions of events per second, with stable microsecond latencies.
A Chronicle Queue can be used to build state machines.
All the information about the state of those components can be reproduced externally, without direct access to the components, or to their state.
This significantly reduces the need for additional logging.
However, any logging you do need can be recorded in great detail.
This makes enabling DEBUG logging in production practical.
This is because the cost of logging is very low; less than 10 microseconds.
Logs can be replicated centrally for log consolidation.
Chronicle Queue is being used to store 100+ TB of data, which can be replayed from any point in time.
Non-batching streaming components are highly performant, deterministic, and reproducible. You can reproduce bugs which only show up after a million events played in a particular order, with accelerated realistic timings. This makes using Stream processing attractive for systems which need a high degree of quality outcomes.
You can have different instances of SingleChronicleQueue set with different roll cycles.
However, if you have instances of SingleChronicleQueue with the same path but different roll cycles, first queue to be created determines the roll cycle, even if the first queue only have a tailer.
If the first queue is read-only, it will fail to read the queue if the roll cycle is later different.
Releases are available on Maven Central as:
<dependency>
<groupId>net.openhft</groupId>
<artifactId>chronicle-queue</artifactId>
<version><!--replace with the latest version, see below--></version>
</dependency>See Chronicle Queue Release Notes and get the Latest Version Number. Snapshots are available on https://oss.sonatype.org
|
Note
|
Classes that reside in either of the packages 'internal', 'impl', and 'main' (the latter containing various runnable main methods) and
any sub-packages are not a part of the public API and may become subject to change at any time for any reason.
See the respective package-info.java files for details.
|
In Chronicle Queue v5 tailers are now read-only, in Chronicle Queue v4 we had the concept of lazy indexing, where appenders would not write indexes but instead the indexing could be done by the tailer. We decided to drop lazy indexing in v5; making tailers read-only not only simplifies Chronicle Queue but also allows us to add optimisations elsewhere in the code.
The locking model of Chronicle Queue was changed in v5, in Chronicle Queue v4 the write lock (to prevent concurrent writes to the queue) exists in the .cq4 file. In v5 this was moved to a single file called a table store (metadata.cq4t). This simplifies the locking code internally as only the table store file has to be inspected.
You can use Chronicle Queue v5 to read messages written with Chronicle Queue v4, but this is not guaranteed to always work - if, for example,
you created your v4 queue with wireType(WireType.FIELDLESS_BINARY) then Chronicle Queue v5 will not be able to read the queue’s header.
We have some tests for v5 reading v4 queues but these are limited and all scenarios may not be supported.
You cannot use Chronicle Queue v5 to write to Chronicle Queue v4 queues.
Chronicle Queue v4 is a complete re-write of Chronicle Queue that solves the following issues that existed in v3.
-
Without self-describing messages, users had to create their own functionality for dumping messages and long term storage of data. With v4 you don’t have to do this, but you can if you wish to.
-
Vanilla Chronicle Queue would create a file per thread. This is fine if the number of threads is controlled, however, many applications have little or no control over how many threads are used and this caused usability problems.
-
The configuration for Indexed and Vanilla Chronicle was entirely in code so the reader had to have the same configuration as the writers and it wasn’t always clear what that was.
-
There was no way for the producer to know how much data had been replicated to the a second machine. The only workaround was to replicate data back to the producers.
-
You needed to specify the size of data to reserve before you started to write your message.
-
You needed to do your own locking for the appender when using Indexed Chronicle.
In Chronicle Queue v3, everything was in terms of bytes, not wire.
There are two ways to use byte in Chronicle Queue v4. You can use the writeBytes and readBytes methods, or you can get the bytes() from the wire.
For example:
appender.writeBytes(b -> b.writeInt(1234).writeDouble(1.111));
boolean present = tailer.readBytes(b -> process(b.readInt(), b.readDouble()));try (DocumentContext dc = appender.writingDocument()) {
Bytes<?> bytes = dc.wire().bytes();
// write to bytes
}
try (DocumentContext dc = tailer.readingDocument()) {
if (dc.isPresent()) {
Bytes<?> bytes = dc.wire().bytes();
// read from bytes
}
}Chronicle Queue Enterprise Edition is a commercially supported version of our successful open source Chronicle Queue. The open source documentation is extended by the following documents to describe the additional features that are available when you are licenced for Enterprise Edition. These are:
-
Encryption of message queues and messages. For more information see Encryption documentation.
-
TCP/IP (and optionally UDP) Replication between hosts to ensure real-time backup of all your queue data. For more information see the replication documentation.
-
Timezone support for daily queue rollover scheduling. For more information see Timezone support.
-
Async mode support to give improved performance at high throughput on slower filesystems. For more information see async mode and also performance.
-
Pre-toucher For improved outliers, see Pre-toucher and its configuration
In addition, you will be fully supported by our technical experts.
For more information on Chronicle Queue Enterprise Edition, please contact [email protected].
A Chronicle Queue is defined by SingleChronicleQueue.class that is designed to support:
-
rolling files on a daily, weekly or hourly basis,
-
concurrent writers on the same machine,
-
concurrent readers on the same machine or across multiple machines via TCP replication (With Chronicle Queue Enterprise),
-
concurrent readers and writers between Docker or other containerised workloads
-
zero copy serialization and deserialization,
-
millions of writes/reads per second on commodity hardware.
Approximately 5 million messages/second for 96-byte messages on a i7-4790 processor. A queue directory structure is as follows:
base-directory /
{cycle-name}.cq4 - The default format is yyyyMMdd for daily rolling.The format consists of size-prefixed bytes which are formatted using BinaryWire or TextWire.
Chronicle Queue is designed to be driven from code.
You can easily add an interface which suits your needs.
|
Note
|
Due to fairly low-level operation, Chronicle Queue read/write operations can throw unchecked exceptions.
In order to prevent thread death, it might be practical to catch RuntimeExceptions and log/analyze them as appropriate.
|
|
Note
|
For demonstrations of how Chronicle Queue can be used see Chronicle Queue Demo and for Java documentation see Chronicle Queue JavaDocs |
In the following sections, first we introduce some terminology and a quick reference to use Chronicle Queue. Then, we provide a more detailed guide.
Chronicle Queue is a persisted journal of messages which supports concurrent writers and readers even across multiple JVMs on the same machine. Every reader sees every message, and a reader can join at any time and still see every message.
|
Note
|
We deliberately avoid the term consumer and instead use reader as messages are not consumed/destroyed by reading. |
Chronicle queue has the following main concepts:
-
Excerpt
Excerpt is the main data container in a Chronicle Queue. In other words, each Chronicle Queue is composed of excerpts. Writing message to a Chronicle Queue means starting a new excerpt, writing message into it, and finishing the excerpt at the end.
-
Appender
An appender is the source of messages; something like an iterator in Chronicle environment. You add data appending the current Chronicle Queue. It can perform sequential writes by appending to the end of queue only. There is no way to insert, or delete excerpts.
-
Tailer
A tailer is an excerpt reader optimized for sequential reads. It can perform sequential and random reads, both forwards and backwards. Tailers read the next available message each time they are called. The followings are guaranteed in Chronicle Queue:
-
for each appender, messages are written in the order the appender wrote them. Messages by different appenders are interleaved,
-
for each tailer, it will see every message for a topic in the same order as every other tailer,
-
when replicated, every replica has a copy of every message.
Chronicle Queue is broker-less. If you need an architecture with a broker, please contact [email protected].
-
File rolling and queue files
Chronicle Queue is designed to roll its files depending on the roll cycle chosen when queue is created (see RollCycles).
In other words, a queue file is created for each roll cycle which has extension cq4.
When the roll cycle reaches the point it should roll, appender will atomically write EOF mark at the end of current file to indicate that no other appender should write to this file and no tailer should read further, and instead everyone should use new file.
If the process was shut down, and restarted later when the roll cycle should be using a new file, an appender will try to locate old files and write an EOF mark in them to help tailers reading them.
-
Topics
Each topic is a directory of queue files.
If you have a topic called mytopic, the layout could look like this:
mytopic/
20160710.cq4
20160711.cq4
20160712.cq4
20160713.cq4To copy all the data for a single day (or cycle), you can copy the file for that day on to your development machine for replay testing.
-
Restrictions on topics and messages
Topics are limited to being strings which can be used as directory names. Within a topic, you can have sub-topics which can be any data type that can be serialized. Messages can be any serializable data.
Chronicle Queue supports:
-
Serializableobjects, though this is to be avoided as it is not efficient -
Externalizableobjects is preferred if you wish to use standard Java APIs. -
byte[]andString

评论