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

9
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

description

A lightening talk on using Shared Memory in Java is a Thread Safe manner and why you would want to.

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

Page 1: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

Thread Safe InterprocessShared Memory in Java

(in 7 minutes)

Peter LawreyPrincipal Consultant

Higher Frequency Trading

January 2014 Thread Safe Interprocess Shared Memory in Java

1

Page 2: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

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

Page 3: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

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

Page 4: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

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

Page 5: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

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

Page 6: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

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

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

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

Page 8: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

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

Page 9: Thread Safe Interprocess Shared Memory in Java (in 7 mins)

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