Java Performance Workshop With Peter Lawrey
Join the DZone community and get the full member experience.Join For Free
peter lawrey at it days
of java and low latency
little’s law defines concurrency as:
to increase throughput we can either:
- increase server resources (scaling vertically or horizontally)
- decrease latency (improve performance)
while enterprise applications are usually designed for scaling, trading systems focus on lowering latencies.
as surprising as it may sound, most trading systems, i’ve heard of, are actually written in java. java automatic memory management is a double-edged sword, since it trades simplicity for flexibility.
a low latency system worse nightmare is a stop-of-the-world process, like a garbage collector major collection. so in order to avoid such situations, you must go off-heap.
java doesn’t directly offer off-heap memory management so you have to resort to unorthodox methods, like interacting with the sun.misc.unsafe class.
as it’s name clearly suggests, you shouldn’t be using this class in the very first place. this class allows you to bypass many jvm safety mechanisms, so you can:
- allocate off-heap memory
- define classes without an actual classloader
- reassign memory data, even constants
this class is not meant to be used by most java professionals, being mostly useful for spoon bending libraries such as:
for low latency trading systems, you cannot use any off-the-shelf messaging system. to reach 20 millions per seconds you need a high performance queuing solution.
java chronicle is a much better alternative to using memory-mapped files with the risky sun.misc.unsafe api. in short, java chronicle allows a producer process to write data to a shared memory location, only to be consumed by some other process.
so, java chronicle is more like a memory-mapped file utility than an actual messaging system. so if you plan on connecting two separate applications running in the same box, then you should definitely give a try to this low latency library.
if you enjoyed this article, i bet you are going to love my book as well.
while you should always strive for the least possible latency, an enterprise application requires a totally different queuing solution. an enterprise system is built of several applications that need to be inter-connected and these applications might reside on different nodes.
so an enterprise messaging system must be designed for networking in the very first place. compared to trading systems latencies (microseconds), network latencies are much higher (milliseconds).
an enterprise queue must deal with both multiple producers as well as multiple consumers. some systems use brokers, to acknowledge consumed messages (e.g. jms), while other systems delegate message reading offsets to the consumer-side (apache kafka).
apache kafka is a publish-subscribe messaging solution based on a distributed commit log. so while java chronicle increases throughput by reducing latency, kafka uses distributed scaling instead.
Published at DZone with permission of Vlad Mihalcea. See the original article here.
Opinions expressed by DZone contributors are their own.