37
JCache (JSR107) David Brimley The standard for Java Caching

JCache (JSR107) - QCon London 2015

Embed Size (px)

Citation preview

Page 1: JCache (JSR107) - QCon London 2015

JCache (JSR107)

David Brimley

The standard for Java Caching

Page 2: JCache (JSR107) - QCon London 2015

Who is this guy?

Senior Solutions Architect @ Hazelcast

Java Developer for 17 years

@dbrimley

In Memory Data Grids for 8 years

Worked for Investment Banks

Started out coding Basic on a Commodore Vic-20

My fav computer was my old BBC Model B

Long suffering Spurs fan.

HSBC, Nomura, Credit Suisse, BZW, RBoS

Oracle Coherence 4 YearsPivotal Gemfire 4 Years

David Brimley

Runs HUGL (Hazelcast User Group London)

Low latency trading systems

Page 3: JCache (JSR107) - QCon London 2015

What we’ll cover…• Why Caching? • Introduction to JCache. • Using Caches. • Entry Processors. • Listeners. • Annotations. • Missing Bits. • Questions.

Page 4: JCache (JSR107) - QCon London 2015

Who uses caching?

Quick Show of hands

Page 5: JCache (JSR107) - QCon London 2015

Why Caching?

Page 6: JCache (JSR107) - QCon London 2015

Why Caching?The Speed Factor (Memory .v. Disk)

L1 cache reference 0.5 ns

Branch mispredict 5.0 ns

L2 cache reference 7.0 ns

Mutex lock/unlock 25.00 ns

Main memory reference 100.00 ns

Compress 1k bytes with ZupZ 3,000.00 ns

Send 1k bytes over 1Gpbs network 10,000.00 ns 0.01 ms

Read 4k randomly from SSD 150,000.00 ns 0.15 ms

Read 1 MB sequentially from memory 250,000.00 ns 0.25 ms

Round trip within same datacenter 500,000.00 ns 0.50 ms

Read 1 MB sequentially from SSD 1,000,000.00 ns 1.00 ms

Disk seek 10,000,000.00 ns 10.00 ms

Read 1 MB sequentially from disk 20,000,000.00 ns 20.00 ms

Send packet CA->Netherlands->CA 150,000,000.00 ns 150.00 ms

https://gist.github.com/jboner/2841832

Page 7: JCache (JSR107) - QCon London 2015

Why Caching?

Graph of Memory Prices Decreasing with Time (1957-2014)

http://www.jcmit.com/mem2014.htm

The Cost Factor

Page 8: JCache (JSR107) - QCon London 2015

Why Caching?Lots of Use Cases

Slow Database

HTTP Proxy

Browser

Persistence Framework

Disk

Application

Hard to scale Database

Lots of Cache Types

Local Cache Data Grid

Document Store JPA Second Level

JPA First Level

Page 9: JCache (JSR107) - QCon London 2015

Introduction to JCache

Page 10: JCache (JSR107) - QCon London 2015

Introduction to JCache• Caching for the Java Platform

• Produced via the JCP / JSR107

• Ratified March 2014

• 10 years in the making.

• Works with SDK.

• Spec Leads: Greg Luck, Brian Oliver

Page 11: JCache (JSR107) - QCon London 2015

java.util.Map (Java 6/7)

Key-Value Based API

Supports Atomic Updates

Entries Don’t Expire

Entries Aren’t Evicted

Entries Stored On-Heap

Store-By-Reference

https://gist.github.com/jboner/2841832

Introduction to JCacheMap .v. Cache

javax.cache.Cache (Java 6)

Key-Value Based API

Supports Atomic Updates

Entries May Expire

Entries May Be Evicted

Entries Stored Anyware (ie: topologies)

Store-By-Value and Store-By-Reference

Supports Integration (ie: Loaders/Writers)

Supports Observation (ie: Listeners

Entry Processors

Statistics

Page 12: JCache (JSR107) - QCon London 2015

JCache makes NO assumptions about

topology.

Single Process or Distributed

Introduction to JCache

Page 13: JCache (JSR107) - QCon London 2015

Introduction to JCache

• JCP Project http://jcp.org/en/jsr/detail?id=107

• Source Code https://github.com/jsr107

• Forum https://groups.google.com/forum/?fromgroups#!forum/jsr107

Page 14: JCache (JSR107) - QCon London 2015

JSR107 TCK Compliant Vendors

Introduction to JCache

https://jcp.org/aboutJava/communityprocess/implementations/jsr107/index.html

I pity the fool that claims to support JCache without

TCK compliance

As of 5th March 2015

Page 15: JCache (JSR107) - QCon London 2015

Introduction to JCacheSomething to remember when comparing vendors of distributed caches…

Do NOT Benchmark with a single instance!

Page 16: JCache (JSR107) - QCon London 2015

Using Caches

Page 17: JCache (JSR107) - QCon London 2015

Using Caches

<dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> <version>1.0.0</version> </dependency>

Maven Central

Page 18: JCache (JSR107) - QCon London 2015

Using CachesClasses

Caching “service loader”

CachingProvider “SPI Implementation”

CacheManager “manager of caches”

Cache “interface to a Cache”

Page 19: JCache (JSR107) - QCon London 2015

Using CachesClasses

Code Walkthrough

Page 20: JCache (JSR107) - QCon London 2015

Entry Processors

Page 21: JCache (JSR107) - QCon London 2015

Entry ProcessorsCompute in place

• Atomic Operations in situ of data.• Eliminate Round-Trips (if distributed)

• Lock Free• Needs Serialisation (if distributed)

Application Application

Cache Cache

Page 22: JCache (JSR107) - QCon London 2015

Entry ProcessorsClasses

Code Walkthrough

Page 23: JCache (JSR107) - QCon London 2015

Listeners

Page 24: JCache (JSR107) - QCon London 2015

ListenersCallbacks for events in Cache

• Follows Observer Pattern

• Interface per event type

• Created, Updated, Removed, Expired

• Iterations of Events passed back

• MutableCacheEntryListenerConfiguration

Page 25: JCache (JSR107) - QCon London 2015

ListenersClasses

Code Walkthrough

Page 26: JCache (JSR107) - QCon London 2015

Cache Loaders / Writers

Page 27: JCache (JSR107) - QCon London 2015

Cache Loaders / WriterCache Misses / Cache Persist

Application

Cache

Backing Store

Page 28: JCache (JSR107) - QCon London 2015

Cache Loaders / WriterClasses

Code Walkthrough

Page 29: JCache (JSR107) - QCon London 2015

Annotations

Page 30: JCache (JSR107) - QCon London 2015

AnnotationsSpring 4.1, Guice, CDI

• @CacheResult

• @CachePut

• @CacheRemove

• @CacheRemoveAll

Page 31: JCache (JSR107) - QCon London 2015

AnnotationsClasses

Code Walkthrough

Page 32: JCache (JSR107) - QCon London 2015

Missing Bits

Page 33: JCache (JSR107) - QCon London 2015

Missing BitsThe Future of JCache

• Async API

• Query

• JEE 8

• Servlet 4.0 (Session Cache)

• Java 8 Lang Features (Lambda/Streams)

Page 34: JCache (JSR107) - QCon London 2015

Questions ?

Page 35: JCache (JSR107) - QCon London 2015
Page 36: JCache (JSR107) - QCon London 2015

HAZELCAST• Free London Training • Hazelcast User Group• Visit our Booth

Visit our Booth 5th Floor Get Free Training (Canary Wharf, Thursday 12th March) http://hazelcast.com/events/

Join the Hazelcast User Group London (HUGL) http://www.meetup.com/Hazelcast-User-Group-London-HUGL/

Download Hazelcast, the leading open source IMDG http://www.hazelcast.org

Page 37: JCache (JSR107) - QCon London 2015

THANK YOU.@dbrimley

[email protected]