Peter Lawrey
CEO of Higher Frequency Trading
JAX Finance 2015
Determinism in Finance
Peter Lawrey
Java Developer/Consultant for hedge fund and trading firms for 6
years.
Most answers for Java and JVM on stackoverflow.com
Founder of the Performance Java User’s Group.
Architect of Chronicle Software
Agenda
• Lambda functions and state machines
• Record every input
• Determinism by Design
• Record every output
• Java 8 and Garbage
• Chronicle Queue demo
Lambda Functions
• No mutable state
• Easy to reason about
• Easy to componentize
• But … no mutable state.
State Machine
• Local mutable state
• Easier to reason about,
than shared state
• Easier to componentize
• Not as simple as Lambda
Functions
Lambda functions and a state machine
Record every input
• By recording every input you can recreate the state of the
system at any point and recreate bugs, test rare conditions, and
test the latency distribution of your system.
But
• This approach doesn’t support software upgrades.
• A replay facility which is implemented after the fact might not
recreate your system completely.
Determinism by design
• You want a system where producers write every event, and
consumers and continuously in replay. This way you can be
sure that you have this facility early in the development cycle
and you know that you have recorded every event/input.
• This facility can help you in the testing of your system by
allowing to you build small simple tests to huge complex data
driven tests.
Record every output
• Supports live software upgrades. By recording and replaying
outcome you can have a system which commits to any decision
the previous one made. Ie you can change the software to
make different decisions.
• This can be tested at the API level by having two state
machines, where the input of one is the output of the other.
Isn’t writing to disk slow?
• Uncommitted synchronous writes can be extremely fast.
Typically around a micro-second. The writes are synchronous
to the application so data is not lost if the application dies, but
not actually committed to disk.
• To prevent loss of data on power failure, you can use
replication.
Doesn’t the GC stop the world?
• The GC only pauses the JVM when it has some work to do.
Produce less garbage and it will pause less often
• Produce less than 1 GB/hour of garbage and you can get less
than one pause per day. (With a 24 GB Eden)
Do I need to avoid all objects?
• In Java 8 you can have very short lived objects placed on the
stack. This requires your code to be inlined and escape analysis
to kick in. When this happens, no garbage is created and the
code is faster.
• You can have very long lived objects, provided you don’t have
too much.
• The rest of your data you can place in native memory (off
heap)
• You can create 1 GB/hour of garbage and still not GC
Do I need to avoid all objects?
• In Java 8 you can have very short lived objects placed on the
stack. This requires your code to be inlined and escape analysis
to kick in. When this happens, no garbage is created and the
code is faster.
• You can have very long lived objects, provided you don’t have
too much.
• The rest of your data you can place in native memory (off
heap)
• You can create 1 GB/hour of garbage and still not GC
How does Java 8 avoid creating objects?
One way to think of Java 8 lambdas is the ability to pass behaviour
to a library. With inlining, an alternative view is the ability to
template your code. Consider this locking example
lock.lock();
try {
doSomething();
} finally {
lock.unlock();
}
How does Java 8 avoid creating objects?
This boiler place can be templated
public static void withLock(Lock lock,
Runnable runnable) {
lock.lock();
try {
runnable.run();
} finally {
lock.unlock();
}
}
How does Java 8 avoid creating objects?
This simplifies the code to be
withLock(lock, () -> doSometing());
Doesn’t using a Runnable create an object?
With inlining and escape analysis the Runnable can be placed on
the stack and eliminated (as it has no fields)
A low latency with fail over
• Data sent between
servers is half round
trip.
• Inputs are written on
both servers.
• Outputs are written on
both servers.
• The end to end latency
can be 25 µs, 99% of
the time.
Demo from http://chronicle.software/products/chronicle-queue/
Next Steps
• Chronicle is open source so you can start right away!
• Working with clients to produce Chronicle Enterprise
• Support contract for Chronicle and consultancy
Q & A
Peter Lawrey
@PeterLawrey
http://chronicle.software
http://vanillajava.blogspot.com

Determinism in finance

  • 1.
    Peter Lawrey CEO ofHigher Frequency Trading JAX Finance 2015 Determinism in Finance
  • 2.
    Peter Lawrey Java Developer/Consultantfor hedge fund and trading firms for 6 years. Most answers for Java and JVM on stackoverflow.com Founder of the Performance Java User’s Group. Architect of Chronicle Software
  • 3.
    Agenda • Lambda functionsand state machines • Record every input • Determinism by Design • Record every output • Java 8 and Garbage • Chronicle Queue demo
  • 4.
    Lambda Functions • Nomutable state • Easy to reason about • Easy to componentize • But … no mutable state.
  • 5.
    State Machine • Localmutable state • Easier to reason about, than shared state • Easier to componentize • Not as simple as Lambda Functions
  • 6.
    Lambda functions anda state machine
  • 7.
    Record every input •By recording every input you can recreate the state of the system at any point and recreate bugs, test rare conditions, and test the latency distribution of your system. But • This approach doesn’t support software upgrades. • A replay facility which is implemented after the fact might not recreate your system completely.
  • 8.
    Determinism by design •You want a system where producers write every event, and consumers and continuously in replay. This way you can be sure that you have this facility early in the development cycle and you know that you have recorded every event/input. • This facility can help you in the testing of your system by allowing to you build small simple tests to huge complex data driven tests.
  • 9.
    Record every output •Supports live software upgrades. By recording and replaying outcome you can have a system which commits to any decision the previous one made. Ie you can change the software to make different decisions. • This can be tested at the API level by having two state machines, where the input of one is the output of the other.
  • 10.
    Isn’t writing todisk slow? • Uncommitted synchronous writes can be extremely fast. Typically around a micro-second. The writes are synchronous to the application so data is not lost if the application dies, but not actually committed to disk. • To prevent loss of data on power failure, you can use replication.
  • 11.
    Doesn’t the GCstop the world? • The GC only pauses the JVM when it has some work to do. Produce less garbage and it will pause less often • Produce less than 1 GB/hour of garbage and you can get less than one pause per day. (With a 24 GB Eden)
  • 12.
    Do I needto avoid all objects? • In Java 8 you can have very short lived objects placed on the stack. This requires your code to be inlined and escape analysis to kick in. When this happens, no garbage is created and the code is faster. • You can have very long lived objects, provided you don’t have too much. • The rest of your data you can place in native memory (off heap) • You can create 1 GB/hour of garbage and still not GC
  • 13.
    Do I needto avoid all objects? • In Java 8 you can have very short lived objects placed on the stack. This requires your code to be inlined and escape analysis to kick in. When this happens, no garbage is created and the code is faster. • You can have very long lived objects, provided you don’t have too much. • The rest of your data you can place in native memory (off heap) • You can create 1 GB/hour of garbage and still not GC
  • 14.
    How does Java8 avoid creating objects? One way to think of Java 8 lambdas is the ability to pass behaviour to a library. With inlining, an alternative view is the ability to template your code. Consider this locking example lock.lock(); try { doSomething(); } finally { lock.unlock(); }
  • 15.
    How does Java8 avoid creating objects? This boiler place can be templated public static void withLock(Lock lock, Runnable runnable) { lock.lock(); try { runnable.run(); } finally { lock.unlock(); } }
  • 16.
    How does Java8 avoid creating objects? This simplifies the code to be withLock(lock, () -> doSometing()); Doesn’t using a Runnable create an object? With inlining and escape analysis the Runnable can be placed on the stack and eliminated (as it has no fields)
  • 17.
    A low latencywith fail over • Data sent between servers is half round trip. • Inputs are written on both servers. • Outputs are written on both servers. • The end to end latency can be 25 µs, 99% of the time.
  • 18.
  • 19.
    Next Steps • Chronicleis open source so you can start right away! • Working with clients to produce Chronicle Enterprise • Support contract for Chronicle and consultancy
  • 20.
    Q & A PeterLawrey @PeterLawrey http://chronicle.software http://vanillajava.blogspot.com