Low Latency Java
Low Latency Java
To achieve low latency in java you have to take control of GC in java, there are
many ways to do that for eg pre-allocate objects(i.e use flyweight design pattern),
use primitive objects - trove is very good for that, all data structure are based on
primitive, Reuse object instance for eg create system wide dictionary to reduce
creating new objects, very good option when reading data from stream/socket/db
Try to use wait-free algo( which is bit difficult), lock free algo. You can find
tons of example for that
Use in-memory computing. Memory is cheap, you can have tera byte of
data in memory.
If you can master bit-wise algo then it gives very good performance.
Low latency is a function of many things, the two most important ones being:
network latency - i.e. the time taken on the network to transmit/receive messages.
processing latency - i.e. the time taken by your application to act on a
message/event.
So, if you are say writing an Order Matching system, the network latency would
represent how soon within your network were you able to receive the order matching
request. And processing latency would represent the time taken by your application to
match the Order against existing, open orders.
Multicast, UDP, reliable multicast, Kernel bypass (supported by Java 7, Informatica Ultra
Messaging, and many others) on Infiniband networks are some common technologies
used by all companies in this field.
Additionally, there are low latency programming frameworks like disruptor
(http://code.google.com/p/disruptor/) which implement design patterns for dealing with
low latency applications. What could kill you is having to write to a DB or log files as part
of your main workflow. You will have to come up with unique solutions that fulfill the
requirements of the problem you are trying to solve.
In languages like Java, implementing your app such that it creates (almost) zero
garbage becomes extremely important to latency. As Adamski says, having a knowledge
of Java memory model is extremely important. Understand different JVM
implementations, and their limitations. Typical Java design patterns around small object
creation are the first things that you will throw out of the window - one can never fix the
Java Garbage Collector enough to achieve low latency - the only thing that can be fixed
is the garbage.
Good luck!
I work for a financial company that produces low latency software for communication
directly with exchanges (for submitting trades and streaming prices). We currently
develop primarily in Java. Whilst the low latency side isn't an area I work in directly I
have a fair idea of the skillset required, which would include the following in my opinion:
Detailed knowledge of TCP/IP and UDP multicast including utilities for debugging
and measuring latency (e.g. DTrace on Solaris).
Warm up your JVM. Bytecode starts starts off being interpreted for
Hotspot and then gets compiled on the server after 10K observations.
Tiered Compilation can be a good stop gap.
2.
3.
4.
Model you business domain and ensure all your algorithms are O(1) or
at least O(log n). This is probably the biggest cause of performance
issues in my experience. Make sure you have performance tests to cover
the main cases.
5.
6.
7.
8.
9.
Most importantly get someone involved with experience. This will save
you so much time in the long run. Shameless plug :-)
2.
Generally you want lock free algorithms and I/O. Even the most well
designed concurrent application (that uses locks) is at risk of blocking,
blocking in low latency is generally bad :-).
3.
4.
The Hotspot JIT is freaking amazing. Learn about its optimizations, but
generally speaking all of the good OO techniques (encapsulation, small
methods, as much immutable data as possible) will allow JIT to optimize,
giving you the sorts of performance levels that well crafted C/C++ code
gives you.
5.
6.
every micro-second counts. You will have an idea of most much each microsecond costs your business per year and how much time it is worth reducing each
micro-second.
you want to measure the highest 99% or even 99.99% latencies. (worst 1% or
0.01% respectly)
you want a fast clock which is often limited to one host, or even one socket. (You
can measure low latency between hosts with specialist hardware) For multimillisecond timings you can relatively easily measure between hosts (with just NTP
configured)
you want to minimise garbage, esp in your measurements.
it is quite likely you will need to develop application specific tools which are
embedded into the application and run in production. You can use profilers as a
start but most ultra low latency applications don't show anything useful in
commercial profilers (nor do they GC much, if at all when running)
You can have a read of my blog for general low latency, high performance testing
practices (some of these are nano-second based). Vanilla Java