SlideShare una empresa de Scribd logo
1 de 28
Lynx Capital Partners
Use Java in HFT
Intro / Trading
•Brief history of financial markets
•Era of gesturing brokers/traders
•Electronic markets, machines replacing
humans
•Modern trading almost completely automated
(incl. crashes)
Information flows, briefly
Markets
Markets
Ticker plant
market data processing
marketdata, N x 10G
Historical data storage
Research clusterOrder stream
Execution management
Order/risk
management
Latencies matter
Nuances... (incl. the devil)
• There’s no such thing as current price
• Inter-market arbitrage, faster wins
• Good data stream is a thick data stream
• ... and it works being diligently processed (only)
• How thick? From GBs to TBs. Daily.
• ... ah, all that jazz is high-dimensional jazz
Complexity

chart
Development
costs
Alternatives (working ones)
• FPGA!
• Memory limitations...
• Algorithmic limitations...
• License and development costs...
• Scaling issues....
• Unmanaged native code (C++ doom)
• Long development cycle (debugging hell with imps)
• Error prone
• Complex business logic -> exponential costs growth
• May be Rust? But not now (yet)…
С++, eh?
Java! Familiar, isn’t it?
• Pros
• Scales better in regards to complexity
• Better performance at lower cost
• JIT produces the code of actual load
• ± the same result as native code if u know proper tricks
• Better interoperability with non-real time parts
• Easier deployment and configuration
• Scales well for bandwidth
• Rich tooling
• Cons
• Latency (damn GC, the sleeping dragon!)
• Lack of control of JIT’s optimiser
GC. Don’t wake the Dragon.
GC. Don’t wake the Dragon.
• #1 enemy
• Dying is forbidden
• At once we can call it to swipe garbage after
initialisation
• Preallocated and pooled, it’s how that all look alike
• Allocation trees can become vast and GC can climb
on them for ages, doing nothing
• Critical JNI can keep that bstrd behind the bars
• Frameworks? Didn’t hear that word
• Most of JDK utility classes are sinful
• Founding Fathers wrote their own, righteous utility classes
Direct Memory Access
Direct Memory Access
• Off-heap memory can be used
• Unsafe class to help allocate in OS heap
• Unsafe reads from and writes to direct addresses
• Manual memory management, finally! We kill them all
when we have ordered to do that.
• The framework to rule has been built
• No type safety and no range checks mean that strong
exorcism skills are required. Sometimes.
• Yes, it can blast the whole world off
• There’s a candy, though: better performance inside
NUMA group.
UnsafeUtil example
UnsafeUtil writing
UnsafeUtil reading
Unsafe: other pattern
Off-heap object allocation
Off-heap object allocation
• Everything fast is pricey
• Could we allocate Java objects off-heap?
• We developed the cure
• Pre-allocated mutable Java object which contents can be copied
off-heap creating new reference. Stairway to heaven.
• This “template” object never dies and saves all references to off-
heap copies
• There’s no direct access to heaven, and JVM is catcher in rye
• Setters have to be defined for all reference fields
• Dura lex, but there are tricks
• Transparent memory mapping w/o expl. serialization/parsing
• Pointer math (we missed it in Java!)
• Passing objects between threads
Off-heap object example
Off-heap object example
Off-heap object example
The art of context switching
• Random and significant delay
• from 500µs to 10s of ms - long as dark ages
• To prevent contextquakes threads are nailed
down to physical CPU cores
• Bless OS for system calls for thread affinity
• But OS still can suspend your thread
• So, OS is not your friend, no, bro
• Avoid any interaction with this troublemaker (OS)
• Including OS scheduler and shared IO
Thread blocking
Thread blocking
• LockSupport class allows allocation-free block/unblock
• Causes random and significant delays for both sleeper
and waker though
• There’s a cure: insomnia! And continuous activity.
• Uses a lot of power, but it’s a winter, after all. And
there’s no choice, threads are affine to CPU cores
already.
• System scheduler can not balance the load anymore,
we have to do that manually
• There are some patterns to help minimise the work
involved
Threads never sleep
Threads never sleep
IO
• Specialized NICs with userspace API
• Application has an exclusive ownership over
a NIC
• No interrupts>no kernel>no unpredictability
• Non-blocking async reads from network
• 10Gbit bypass NICs are cheap
• No local IO (bye-bye, filesystem)
• File access causes unpredictable jitter
• Move all IO to the network, incl logging
Summary
• There’s no ideal environment to develop latency-
restricted apps
• Java is far from this ideal too
• But the balance of features enables Java’s usage in
this complex uneven area
• ... and help to make some money too
• Unfortunately, there’s no room for Scala (yet) :(
• Thank you! (and we’re hiring)
viktor@algotradinghub.com @vsovietov

Más contenido relacionado

La actualidad más candente

Advanced off heap ipc
Advanced off heap ipcAdvanced off heap ipc
Advanced off heap ipc
Peter Lawrey
 
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
Jonas Bonér
 

La actualidad más candente (20)

GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @Geecon
 
Determinism in finance
Determinism in financeDeterminism in finance
Determinism in finance
 
Low latency for high throughput
Low latency for high throughputLow latency for high throughput
Low latency for high throughput
 
Low level java programming
Low level java programmingLow level java programming
Low level java programming
 
Advanced off heap ipc
Advanced off heap ipcAdvanced off heap ipc
Advanced off heap ipc
 
Introduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users GroupIntroduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users Group
 
Low latency microservices in java QCon New York 2016
Low latency microservices in java   QCon New York 2016Low latency microservices in java   QCon New York 2016
Low latency microservices in java QCon New York 2016
 
Chronicle accelerate building a digital currency
Chronicle accelerate   building a digital currencyChronicle accelerate   building a digital currency
Chronicle accelerate building a digital currency
 
QCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVM
QCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVMQCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVM
QCon London: Low latency Java in the real world - LMAX Exchange and the Zing JVM
 
Lessons learnt on a 2000-core cluster
Lessons learnt on a 2000-core clusterLessons learnt on a 2000-core cluster
Lessons learnt on a 2000-core cluster
 
Shared Memory Performance: Beyond TCP/IP with Ben Cotton, JPMorgan
Shared Memory Performance: Beyond TCP/IP with Ben Cotton, JPMorganShared Memory Performance: Beyond TCP/IP with Ben Cotton, JPMorgan
Shared Memory Performance: Beyond TCP/IP with Ben Cotton, JPMorgan
 
Introduction to Real Time Java
Introduction to Real Time JavaIntroduction to Real Time Java
Introduction to Real Time Java
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examples
 
Mantis qcon nyc_2015
Mantis qcon nyc_2015Mantis qcon nyc_2015
Mantis qcon nyc_2015
 
Java and Containers - Make it Awesome !
Java and Containers - Make it Awesome !Java and Containers - Make it Awesome !
Java and Containers - Make it Awesome !
 
Multi core programming 2
Multi core programming 2Multi core programming 2
Multi core programming 2
 
Cassandra compaction
Cassandra compactionCassandra compaction
Cassandra compaction
 
Compaction, Compaction Everywhere
Compaction, Compaction EverywhereCompaction, Compaction Everywhere
Compaction, Compaction Everywhere
 
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
 
A Python Petting Zoo
A Python Petting ZooA Python Petting Zoo
A Python Petting Zoo
 

Similar a Java in High Frequency Trading

The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
Abdelmonaim Remani
 
The economies of scaling software - Abdel Remani
The economies of scaling software - Abdel RemaniThe economies of scaling software - Abdel Remani
The economies of scaling software - Abdel Remani
jaxconf
 
BDM37 - Simon Grondin - Scaling an API proxy in OCaml
BDM37 - Simon Grondin - Scaling an API proxy in OCamlBDM37 - Simon Grondin - Scaling an API proxy in OCaml
BDM37 - Simon Grondin - Scaling an API proxy in OCaml
Big Data Montreal
 
Unbreaking Your Django Application
Unbreaking Your Django ApplicationUnbreaking Your Django Application
Unbreaking Your Django Application
OSCON Byrum
 

Similar a Java in High Frequency Trading (20)

Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
 
The Economies of Scaling Software
The Economies of Scaling SoftwareThe Economies of Scaling Software
The Economies of Scaling Software
 
The economies of scaling software - Abdel Remani
The economies of scaling software - Abdel RemaniThe economies of scaling software - Abdel Remani
The economies of scaling software - Abdel Remani
 
Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)Modern Java Concurrency (Devoxx Nov/2011)
Modern Java Concurrency (Devoxx Nov/2011)
 
Rust Programming Language
Rust Programming LanguageRust Programming Language
Rust Programming Language
 
Gpgpu intro
Gpgpu introGpgpu intro
Gpgpu intro
 
Scalable game-servers-tgc
Scalable game-servers-tgcScalable game-servers-tgc
Scalable game-servers-tgc
 
Building a Database for the End of the World
Building a Database for the End of the WorldBuilding a Database for the End of the World
Building a Database for the End of the World
 
Fi fo euc 2014
Fi fo euc 2014Fi fo euc 2014
Fi fo euc 2014
 
BDM37 - Simon Grondin - Scaling an API proxy in OCaml
BDM37 - Simon Grondin - Scaling an API proxy in OCamlBDM37 - Simon Grondin - Scaling an API proxy in OCaml
BDM37 - Simon Grondin - Scaling an API proxy in OCaml
 
Unbreaking Your Django Application
Unbreaking Your Django ApplicationUnbreaking Your Django Application
Unbreaking Your Django Application
 
Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...
 
Webinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionWebinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in Production
 
Webinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in ProductionWebinar: Diagnosing Apache Cassandra Problems in Production
Webinar: Diagnosing Apache Cassandra Problems in Production
 
C# Async/Await Explained
C# Async/Await ExplainedC# Async/Await Explained
C# Async/Await Explained
 
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in ProductionCassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
 
Cassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in ProductionCassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in Production
 
Cassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in ProductionCassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in Production
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
Advanced Operations
Advanced OperationsAdvanced Operations
Advanced Operations
 

Más de Viktor Sovietov (9)

TEDx KPI 2015 Blockchain
TEDx KPI 2015 BlockchainTEDx KPI 2015 Blockchain
TEDx KPI 2015 Blockchain
 
Ling on PIC32 microcontroller
Ling on PIC32 microcontrollerLing on PIC32 microcontroller
Ling on PIC32 microcontroller
 
Erlang On Xen: Redefining the Cloud Software Stack
Erlang On Xen: Redefining the Cloud Software StackErlang On Xen: Redefining the Cloud Software Stack
Erlang On Xen: Redefining the Cloud Software Stack
 
Optimizing Erlang Code for Speed
Optimizing Erlang Code for SpeedOptimizing Erlang Code for Speed
Optimizing Erlang Code for Speed
 
Erlang on Xen: Redefining the cloud software stack
Erlang on Xen:  Redefining the cloud software stackErlang on Xen:  Redefining the cloud software stack
Erlang on Xen: Redefining the cloud software stack
 
Erlang On Xen @ Kiev Functional Programming event, Aug 3 2013
Erlang On Xen @ Kiev Functional Programming event, Aug 3 2013Erlang On Xen @ Kiev Functional Programming event, Aug 3 2013
Erlang On Xen @ Kiev Functional Programming event, Aug 3 2013
 
Erlang Pro Ef Sf Presentation
Erlang Pro Ef Sf PresentationErlang Pro Ef Sf Presentation
Erlang Pro Ef Sf Presentation
 
Virtual Supercomputer
Virtual SupercomputerVirtual Supercomputer
Virtual Supercomputer
 
Massive Solutions Clustrx Os
Massive Solutions Clustrx OsMassive Solutions Clustrx Os
Massive Solutions Clustrx Os
 

Último

Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Último (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 

Java in High Frequency Trading

  • 2. Intro / Trading •Brief history of financial markets •Era of gesturing brokers/traders •Electronic markets, machines replacing humans •Modern trading almost completely automated (incl. crashes)
  • 3. Information flows, briefly Markets Markets Ticker plant market data processing marketdata, N x 10G Historical data storage Research clusterOrder stream Execution management Order/risk management
  • 5. Nuances... (incl. the devil) • There’s no such thing as current price • Inter-market arbitrage, faster wins • Good data stream is a thick data stream • ... and it works being diligently processed (only) • How thick? From GBs to TBs. Daily. • ... ah, all that jazz is high-dimensional jazz Complexity
 chart Development costs
  • 6. Alternatives (working ones) • FPGA! • Memory limitations... • Algorithmic limitations... • License and development costs... • Scaling issues.... • Unmanaged native code (C++ doom) • Long development cycle (debugging hell with imps) • Error prone • Complex business logic -> exponential costs growth • May be Rust? But not now (yet)…
  • 8. Java! Familiar, isn’t it? • Pros • Scales better in regards to complexity • Better performance at lower cost • JIT produces the code of actual load • ± the same result as native code if u know proper tricks • Better interoperability with non-real time parts • Easier deployment and configuration • Scales well for bandwidth • Rich tooling • Cons • Latency (damn GC, the sleeping dragon!) • Lack of control of JIT’s optimiser
  • 9. GC. Don’t wake the Dragon.
  • 10. GC. Don’t wake the Dragon. • #1 enemy • Dying is forbidden • At once we can call it to swipe garbage after initialisation • Preallocated and pooled, it’s how that all look alike • Allocation trees can become vast and GC can climb on them for ages, doing nothing • Critical JNI can keep that bstrd behind the bars • Frameworks? Didn’t hear that word • Most of JDK utility classes are sinful • Founding Fathers wrote their own, righteous utility classes
  • 12. Direct Memory Access • Off-heap memory can be used • Unsafe class to help allocate in OS heap • Unsafe reads from and writes to direct addresses • Manual memory management, finally! We kill them all when we have ordered to do that. • The framework to rule has been built • No type safety and no range checks mean that strong exorcism skills are required. Sometimes. • Yes, it can blast the whole world off • There’s a candy, though: better performance inside NUMA group.
  • 18. Off-heap object allocation • Everything fast is pricey • Could we allocate Java objects off-heap? • We developed the cure • Pre-allocated mutable Java object which contents can be copied off-heap creating new reference. Stairway to heaven. • This “template” object never dies and saves all references to off- heap copies • There’s no direct access to heaven, and JVM is catcher in rye • Setters have to be defined for all reference fields • Dura lex, but there are tricks • Transparent memory mapping w/o expl. serialization/parsing • Pointer math (we missed it in Java!) • Passing objects between threads
  • 22. The art of context switching • Random and significant delay • from 500µs to 10s of ms - long as dark ages • To prevent contextquakes threads are nailed down to physical CPU cores • Bless OS for system calls for thread affinity • But OS still can suspend your thread • So, OS is not your friend, no, bro • Avoid any interaction with this troublemaker (OS) • Including OS scheduler and shared IO
  • 24. Thread blocking • LockSupport class allows allocation-free block/unblock • Causes random and significant delays for both sleeper and waker though • There’s a cure: insomnia! And continuous activity. • Uses a lot of power, but it’s a winter, after all. And there’s no choice, threads are affine to CPU cores already. • System scheduler can not balance the load anymore, we have to do that manually • There are some patterns to help minimise the work involved
  • 27. IO • Specialized NICs with userspace API • Application has an exclusive ownership over a NIC • No interrupts>no kernel>no unpredictability • Non-blocking async reads from network • 10Gbit bypass NICs are cheap • No local IO (bye-bye, filesystem) • File access causes unpredictable jitter • Move all IO to the network, incl logging
  • 28. Summary • There’s no ideal environment to develop latency- restricted apps • Java is far from this ideal too • But the balance of features enables Java’s usage in this complex uneven area • ... and help to make some money too • Unfortunately, there’s no room for Scala (yet) :( • Thank you! (and we’re hiring) viktor@algotradinghub.com @vsovietov