Java Low Latency Tutorial
Filed in: Java
There are no specific rules that determine if your code is low latency but rather it is a mindset to create code that is performant so that you can squeeze every last millisecond of performance from the JVM. In order to be this performant it is important to understand what happens under the hood in Java so here we may dive a lot deeper into Java than you would normally need for daily programming tasks. However it is important to understand the internals so that you can control how your code is executed and the speed at which it is executed which is really at the heart of Low Latency programming.
There is no real exhaustive list of things you need to know but here I will work through some of the items you must be aware of to get the code to run faster, this list is not exhaustive and if you find a new trick feel free to share it in the contact us link.
Measuring Low Latency
Firstly we need to understand what we mean by performant and how a piece of code can be shown to be performant. Let us take a simple method that does a piece of distinct functionality. This method can be measured and you will see how long it takes to perform its work. The measurement sample here is in Milliseconds but you will see that sometimes the work is quick and sometimes the work takes a bit longer the larger spikes in the output. If I took an average it would not really represent the time taken.

As you can see the Time taken varies but the Average does not in any way represent the latency of the method. The average does imply a behaved Gaussian distribution. This means that a straight average will not work. Instead you need to look at the Percentiles which I have given as a table and here you can see 90% of the time we are under 57 Ms but 10% of the time we are not. This represents the true latency of the method.
This can be combined with measurements of other sensitive parts of the code to see how fast the code is and then you can look at how you can improve upon the performance.
Warming up ByteCode and Pre Load Classes.
It is important to recall that Java programs are interpreted and while we may say “javac” compiles code what it doss in reality is pre-compile to byte code. Follow this link to Learn more about the flow of compilation and how we go from code to cpu.
Compare and Swap and LongAdder optimizations for Low Latency
Single Writer Principle - Mechanical Sympathy
Use O(1) or worst case O(log n)
Write Zero GC code Algorithms
Create lock free Algorithms using Optimistic Locking
Use tail Recursion patterns as they can be optimised by the compiler.
As time permits I will write Articles on these subjects and link them.
Thread Safe Java Singleton
Create a Statistics Distribution
Naive Bayes classification AI algorithm
K-Means Clustering AI algorithm
Equity Derivatives tutorial
Fixed Income tutorial
Java
python
Scala
Investment Banking tutorials
HOME

There is no real exhaustive list of things you need to know but here I will work through some of the items you must be aware of to get the code to run faster, this list is not exhaustive and if you find a new trick feel free to share it in the contact us link.
Measuring Low Latency
Firstly we need to understand what we mean by performant and how a piece of code can be shown to be performant. Let us take a simple method that does a piece of distinct functionality. This method can be measured and you will see how long it takes to perform its work. The measurement sample here is in Milliseconds but you will see that sometimes the work is quick and sometimes the work takes a bit longer the larger spikes in the output. If I took an average it would not really represent the time taken.

As you can see the Time taken varies but the Average does not in any way represent the latency of the method. The average does imply a behaved Gaussian distribution. This means that a straight average will not work. Instead you need to look at the Percentiles which I have given as a table and here you can see 90% of the time we are under 57 Ms but 10% of the time we are not. This represents the true latency of the method.
This can be combined with measurements of other sensitive parts of the code to see how fast the code is and then you can look at how you can improve upon the performance.
Warming up ByteCode and Pre Load Classes.
It is important to recall that Java programs are interpreted and while we may say “javac” compiles code what it doss in reality is pre-compile to byte code. Follow this link to Learn more about the flow of compilation and how we go from code to cpu.
Compare and Swap and LongAdder optimizations for Low Latency
Single Writer Principle - Mechanical Sympathy
Use O(1) or worst case O(log n)
Write Zero GC code Algorithms
Create lock free Algorithms using Optimistic Locking
Use tail Recursion patterns as they can be optimised by the compiler.
As time permits I will write Articles on these subjects and link them.
People who enjoyed this article also enjoyed the following:
Thread Safe Java Singleton
Create a Statistics Distribution
Naive Bayes classification AI algorithm
K-Means Clustering AI algorithm
Equity Derivatives tutorial
Fixed Income tutorial
And the following Trails:
C++Java
python
Scala
Investment Banking tutorials
HOME
