SlideShare a Scribd company logo
1 of 10
Thread Safe Interprocess
Shared Memory in Java
(in 7 minutes)
Peter Lawrey
Principal Consultant
Higher Frequency Trading

January 2014

Thread Safe Interprocess Shared Memory in
Java

1
Shared Memory







Java naturally uses memory on the heap
(and stack)
Java can also access memory mapped files
These files can be accessed and shared by
multiple processes at once
They can be persisted, or stored on a tmpfs or
RAM drive

January 2014

Thread Safe Interprocess Shared Memory in Java

2
Thread Safe




Java's thread safe constructs are all on the
heap
You can use Unsafe and create off heap
thread safe constructs such as memory
barriers, volatile access and CAS operations

January 2014

Thread Safe Interprocess Shared Memory in Java

3
What is CAS?






CAS is Compare And Swap also known as
Compare And Set in some Java libraries
It is a thread safe operation which only
succeeds for one thread at a time and can be
used to build a lock
Is a single machine code instruction → fast

January 2014

Thread Safe Interprocess Shared Memory in Java

4
Why not use the heap?


Can support 100s of GB with no GC impact



Can share data structures between processes



Can persist data without extra overhead

This allows you to implement a simple
embedded database with sub micro-second
read/write latency

January 2014

Thread Safe Interprocess Shared Memory in Java

5
sun.misc.Unsafe
public native compareAndSetInt(
Object obj,
// can be null
long offset,
int expected,
int value);
Also compareAndSetLong
Likely to go away in Java 9
January 2014

Thread Safe Interprocess Shared Memory in Java

6
Bytes
OpenHFT/Java-Lang has a wrapper


64-bit replacement for ByteBuffer



Thread safe operations



Supports binary data, text parsing and object
serialization. e.g. ObjectInput/Ouput

January 2014

Thread Safe Interprocess Shared Memory in Java

7
Demo - LockingViaMMapMain
Two processes toggle flags in many locked records
One process flips false → true
The other flips true → false

Toggled 100,000,000 times with
an average delay of 49 ns
January 2014

Thread Safe Interprocess Shared Memory in
Java

8
Questions?
@PeterLawrey
github.com/OpenHFT
Thank you to
SkillsMatter – the host
RecWorks – the organisers
LJC – London Java Community
January 2014

Thread Safe Interprocess Shared Memory in Java

9
Questions?
@PeterLawrey
github.com/OpenHFT
Thank you to
SkillsMatter – the host
RecWorks – the organisers
LJC – London Java Community
January 2014

Thread Safe Interprocess Shared Memory in Java

9

More Related Content

What's hot

Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...
Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...
Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...
DataStax
 

What's hot (20)

Responding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in JavaResponding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in Java
 
Low latency for high throughput
Low latency for high throughputLow latency for high throughput
Low latency for high throughput
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5
 
Determinism in finance
Determinism in financeDeterminism in finance
Determinism in finance
 
Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)
 
Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016
 
Chronicle accelerate building a digital currency
Chronicle accelerate   building a digital currencyChronicle accelerate   building a digital currency
Chronicle accelerate building a digital currency
 
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
 
Java in High Frequency Trading
Java in High Frequency TradingJava in High Frequency Trading
Java in High Frequency Trading
 
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
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examples
 
Asynchronous Python with Twisted
Asynchronous Python with TwistedAsynchronous Python with Twisted
Asynchronous Python with Twisted
 
Twisted: a quick introduction
Twisted: a quick introductionTwisted: a quick introduction
Twisted: a quick introduction
 
Why Reactive Architecture Will Take Over The World (and why we should be wary...
Why Reactive Architecture Will Take Over The World (and why we should be wary...Why Reactive Architecture Will Take Over The World (and why we should be wary...
Why Reactive Architecture Will Take Over The World (and why we should be wary...
 
Testing Wi-Fi with OSS Tools
Testing Wi-Fi with OSS ToolsTesting Wi-Fi with OSS Tools
Testing Wi-Fi with OSS Tools
 
Cassandra compaction
Cassandra compactionCassandra compaction
Cassandra compaction
 
Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...
Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...
Tuning Speculative Retries to Fight Latency (Michael Figuiere, Minh Do, Netfl...
 
Distributed system coordination by zookeeper and introduction to kazoo python...
Distributed system coordination by zookeeper and introduction to kazoo python...Distributed system coordination by zookeeper and introduction to kazoo python...
Distributed system coordination by zookeeper and introduction to kazoo python...
 
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
 
High Availabiltity & Replica Sets with mongoDB
High Availabiltity & Replica Sets with mongoDBHigh Availabiltity & Replica Sets with mongoDB
High Availabiltity & Replica Sets with mongoDB
 

Viewers also liked

опорність
опорністьопорність
опорність
kdnz255
 

Viewers also liked (10)

Tech Lead (Java) - Job in London from abrs
Tech Lead (Java) - Job in London from abrsTech Lead (Java) - Job in London from abrs
Tech Lead (Java) - Job in London from abrs
 
What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)
 
Jni – java native interface
Jni – java native interfaceJni – java native interface
Jni – java native interface
 
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyStreams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the ugly
 
Legacy lambda code
Legacy lambda codeLegacy lambda code
Legacy lambda code
 
Android JNI
Android JNIAndroid JNI
Android JNI
 
Getting started with the JNI
Getting started with the JNIGetting started with the JNI
Getting started with the JNI
 
опорність
опорністьопорність
опорність
 
Blue Ocean Strategy case with zero forfait
Blue Ocean Strategy case with zero forfaitBlue Ocean Strategy case with zero forfait
Blue Ocean Strategy case with zero forfait
 
Inday LearnIn: What can SlideShare learn from other organisations?
Inday LearnIn: What can SlideShare learn from other organisations?Inday LearnIn: What can SlideShare learn from other organisations?
Inday LearnIn: What can SlideShare learn from other organisations?
 

Similar to Thread Safe Interprocess Shared Memory in Java (in 7 mins)

Apache ZooKeeper TechTuesday
Apache ZooKeeper TechTuesdayApache ZooKeeper TechTuesday
Apache ZooKeeper TechTuesday
Andrei Savu
 
Caching principles-solutions
Caching principles-solutionsCaching principles-solutions
Caching principles-solutions
pmanvi
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
Dmitry Buzdin
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
yearninginjava
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
Gaurav Maheshwari
 
Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01
MongoDB
 

Similar to Thread Safe Interprocess Shared Memory in Java (in 7 mins) (20)

Java Threading
Java ThreadingJava Threading
Java Threading
 
J threads-pdf
J threads-pdfJ threads-pdf
J threads-pdf
 
Java 2 computer science.pptx
Java 2 computer science.pptxJava 2 computer science.pptx
Java 2 computer science.pptx
 
Hadoop Vectored IO
Hadoop Vectored IOHadoop Vectored IO
Hadoop Vectored IO
 
Stay fresh
Stay freshStay fresh
Stay fresh
 
Apache ZooKeeper TechTuesday
Apache ZooKeeper TechTuesdayApache ZooKeeper TechTuesday
Apache ZooKeeper TechTuesday
 
Heapoff memory wtf
Heapoff memory wtfHeapoff memory wtf
Heapoff memory wtf
 
ImpalaToGo design explained
ImpalaToGo design explainedImpalaToGo design explained
ImpalaToGo design explained
 
Caching principles-solutions
Caching principles-solutionsCaching principles-solutions
Caching principles-solutions
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
 
Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01Keeping data-safe-webinar-2010-11-01
Keeping data-safe-webinar-2010-11-01
 
0f0cef_1dac552af56c4338ab0672859199e693.pdf
0f0cef_1dac552af56c4338ab0672859199e693.pdf0f0cef_1dac552af56c4338ab0672859199e693.pdf
0f0cef_1dac552af56c4338ab0672859199e693.pdf
 
From oc_filecache to a flexible and scalable OC namespace
From oc_filecache to a flexible and scalable OC namespaceFrom oc_filecache to a flexible and scalable OC namespace
From oc_filecache to a flexible and scalable OC namespace
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Chapter 1 (1).pptx
Chapter 1 (1).pptxChapter 1 (1).pptx
Chapter 1 (1).pptx
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Thread Safe Interprocess Shared Memory in Java (in 7 mins)

  • 1. Thread Safe Interprocess Shared Memory in Java (in 7 minutes) Peter Lawrey Principal Consultant Higher Frequency Trading January 2014 Thread Safe Interprocess Shared Memory in Java 1
  • 2. Shared Memory     Java naturally uses memory on the heap (and stack) Java can also access memory mapped files These files can be accessed and shared by multiple processes at once They can be persisted, or stored on a tmpfs or RAM drive January 2014 Thread Safe Interprocess Shared Memory in Java 2
  • 3. Thread Safe   Java's thread safe constructs are all on the heap You can use Unsafe and create off heap thread safe constructs such as memory barriers, volatile access and CAS operations January 2014 Thread Safe Interprocess Shared Memory in Java 3
  • 4. What is CAS?    CAS is Compare And Swap also known as Compare And Set in some Java libraries It is a thread safe operation which only succeeds for one thread at a time and can be used to build a lock Is a single machine code instruction → fast January 2014 Thread Safe Interprocess Shared Memory in Java 4
  • 5. Why not use the heap?  Can support 100s of GB with no GC impact  Can share data structures between processes  Can persist data without extra overhead This allows you to implement a simple embedded database with sub micro-second read/write latency January 2014 Thread Safe Interprocess Shared Memory in Java 5
  • 6. sun.misc.Unsafe public native compareAndSetInt( Object obj, // can be null long offset, int expected, int value); Also compareAndSetLong Likely to go away in Java 9 January 2014 Thread Safe Interprocess Shared Memory in Java 6
  • 7. Bytes OpenHFT/Java-Lang has a wrapper  64-bit replacement for ByteBuffer  Thread safe operations  Supports binary data, text parsing and object serialization. e.g. ObjectInput/Ouput January 2014 Thread Safe Interprocess Shared Memory in Java 7
  • 8. Demo - LockingViaMMapMain Two processes toggle flags in many locked records One process flips false → true The other flips true → false Toggled 100,000,000 times with an average delay of 49 ns January 2014 Thread Safe Interprocess Shared Memory in Java 8
  • 9. Questions? @PeterLawrey github.com/OpenHFT Thank you to SkillsMatter – the host RecWorks – the organisers LJC – London Java Community January 2014 Thread Safe Interprocess Shared Memory in Java 9
  • 10. Questions? @PeterLawrey github.com/OpenHFT Thank you to SkillsMatter – the host RecWorks – the organisers LJC – London Java Community January 2014 Thread Safe Interprocess Shared Memory in Java 9