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

JCache - It's finally here

Embed Size (px)

Citation preview

JCache (JSR107)

David Brimley

The standard for Java Caching

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

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

Who uses caching?

Quick Show of hands

Why Caching?

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

Why Caching?

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

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

The Cost Factor

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

Introduction to JCache

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

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

JCache makes NO assumptions about

topology.

Single Process or Distributed

Introduction to JCache

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

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

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

Do NOT Benchmark with a single instance!

Using Caches

Using Caches

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

Maven Central

Using CachesClasses

Caching “service loader”

CachingProvider “SPI Implementation”

CacheManager “manager of caches”

Cache “interface to a Cache”

Using CachesClasses

Code Walkthrough

Entry Processors

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

Entry ProcessorsClasses

Code Walkthrough

Listeners

ListenersCallbacks for events in Cache

• Follows Observer Pattern

• Interface per event type

• Created, Updated, Removed, Expired

• Iterations of Events passed back

• MutableCacheEntryListenerConfiguration

ListenersClasses

Code Walkthrough

Cache Loaders / Writers

Cache Loaders / WriterCache Misses / Cache Persist

Application

Cache

Backing Store

Cache Loaders / WriterClasses

Code Walkthrough

Annotations

AnnotationsSpring 4.1, Guice, CDI

• @CacheResult

• @CachePut

• @CacheRemove

• @CacheRemoveAll

AnnotationsClasses

Code Walkthrough

Missing Bits

Missing BitsThe Future of JCache

• Async API

• Query

• JEE 8

• Servlet 4.0 (Session Cache)

• Java 8 Lang Features (Lambda/Streams)

Questions ?

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

THANK YOU.@dbrimley

[email protected]