93
@ljacomet #ehcachedvx Caching reboot javax.cache & Ehcache 3

Caching reboot: javax.cache & Ehcache 3

Embed Size (px)

Citation preview

@ljacomet#ehcachedvx

Caching reboot

javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Agenda

@ljacomet#ehcachedvx

Agenda• Why a cache?

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

• Caching in an application

• Cache aside

• Cache through

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

• Caching in an application

• Cache aside

• Cache through

• Resilience strategy

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

• Caching in an application

• Cache aside

• Cache through

• Resilience strategy

• Extras

@ljacomet#ehcachedvx

Who am I?• Louis Jacomet

• Software engineer, closer to 40 than 20 and still coding!

• Engineer at Terracotta (Software AG) since 2013

• Working on Ehcache OS and EE mostly

• team manager and fiddling in infrastructure

• No idea how to do a (nice) UI - especially a web one

• Enjoys concurrency, thinking hard about APIs, …

Why a cache?

@ljacomet#ehcachedvx

Why a cache?

@ljacomet#ehcachedvx

What were these numbers?

@ljacomet#ehcachedvx

What were these numbers?• Minute:• L1$cache$access$$$$$$$$$$$$$$$0,5$s$$$One$heartbeat$

• Branch$misprediction$$$$$$$$$$5$$$s$$$Yawn$

• L2$cache$access$$$$$$$$$$$$$$$7$$$s$$$Long$yawn$

• Mutex$lock/unlock$$$$$$$$$$$$25$$$s$$$Coffee$preparation

@ljacomet#ehcachedvx

What were these numbers?• Minute:• L1$cache$access$$$$$$$$$$$$$$$0,5$s$$$One$heartbeat$

• Branch$misprediction$$$$$$$$$$5$$$s$$$Yawn$

• L2$cache$access$$$$$$$$$$$$$$$7$$$s$$$Long$yawn$

• Mutex$lock/unlock$$$$$$$$$$$$25$$$s$$$Coffee$preparation

• Hour:• RAM$access$$$$$$$$$$$$$$$$$$100$$$s$$$Brushing$your$teeth$

• Compressing$1K$$$$$$$$$$$$$$$50$$$min$One$TV$show$episode

@ljacomet#ehcachedvx

What were these numbers?• Minute:• L1$cache$access$$$$$$$$$$$$$$$0,5$s$$$One$heartbeat$

• Branch$misprediction$$$$$$$$$$5$$$s$$$Yawn$

• L2$cache$access$$$$$$$$$$$$$$$7$$$s$$$Long$yawn$

• Mutex$lock/unlock$$$$$$$$$$$$25$$$s$$$Coffee$preparation

• Hour:• RAM$access$$$$$$$$$$$$$$$$$$100$$$s$$$Brushing$your$teeth$

• Compressing$1K$$$$$$$$$$$$$$$50$$$min$One$TV$show$episode

• Day:• 2KB$sent$on$1GB$network$$$$$$$5,5$h$$$Your$work$afternoon

@ljacomet#ehcachedvx

What were these numbers?

@ljacomet#ehcachedvx

What were these numbers?• Week:• Random$read$SSD$$$$$$$$$$$$$$$$$1,7$d$$$$$$$A$weekNend$

• Sequence$read$1$MB$from$RAM$$$$$2,9$d$$$$$$$A$long$weekNend$

• Round$trip$in$datacenter$$$$$$$$5,8$d$$$$$$$Holidays$

• Sequence$read$1MB$from$SSD$$$$$11,6$d$$$$$$$Two$weeks$delivery

@ljacomet#ehcachedvx

What were these numbers?• Week:• Random$read$SSD$$$$$$$$$$$$$$$$$1,7$d$$$$$$$A$weekNend$

• Sequence$read$1$MB$from$RAM$$$$$2,9$d$$$$$$$A$long$weekNend$

• Round$trip$in$datacenter$$$$$$$$5,8$d$$$$$$$Holidays$

• Sequence$read$1MB$from$SSD$$$$$11,6$d$$$$$$$Two$weeks$delivery

• Year:• Seek$on$rotational$HDD$$$$$$$$$16,5$weeks$$$School$semester$

• Sequence$read$1MB$from$HDD$$$$$$7,8$months$$Almost$a$baby

@ljacomet#ehcachedvx

What were these numbers?• Week:• Random$read$SSD$$$$$$$$$$$$$$$$$1,7$d$$$$$$$A$weekNend$

• Sequence$read$1$MB$from$RAM$$$$$2,9$d$$$$$$$A$long$weekNend$

• Round$trip$in$datacenter$$$$$$$$5,8$d$$$$$$$Holidays$

• Sequence$read$1MB$from$SSD$$$$$11,6$d$$$$$$$Two$weeks$delivery

• Year:• Seek$on$rotational$HDD$$$$$$$$$16,5$weeks$$$School$semester$

• Sequence$read$1MB$from$HDD$$$$$$7,8$months$$Almost$a$baby

• Decade:• Data$roundNtrip$around$the$world$4,8$years$$PhD$thesis

@ljacomet#ehcachedvx

So what is a cache?

@ljacomet#ehcachedvx

So what is a cache?• Data structure holding a temporary copy of some data

@ljacomet#ehcachedvx

So what is a cache?• Data structure holding a temporary copy of some data

• Trade off between higher memory usage for reduced latency

@ljacomet#ehcachedvx

So what is a cache?• Data structure holding a temporary copy of some data

• Trade off between higher memory usage for reduced latency

• Targets :

• Data which is reused

• Data which is expensive to compute or retrieve

@ljacomet#ehcachedvx

Ehcache (3)

@ljacomet#ehcachedvx

Ehcache (3)• New version, fully integrated with JSR-107

• Breaks compatibility with 2.x line

@ljacomet#ehcachedvx

Ehcache (3)• New version, fully integrated with JSR-107

• Breaks compatibility with 2.x line

• Developed in the open

• https://github.com/ehcache/ehcache3

@ljacomet#ehcachedvx

Ehcache (3)• New version, fully integrated with JSR-107

• Breaks compatibility with 2.x line

• Developed in the open

• https://github.com/ehcache/ehcache3

• Public dev meeting, almost weekly, on hangouts on air

• Recordings available on Youtube

JSR-107 - javx.caching

@ljacomet#ehcachedvx

JSR-107

@ljacomet#ehcachedvx

JSR-107• Ancient JSR

• Started in 2001

• Approved a year ago (March 2014)

@ljacomet#ehcachedvx

JSR-107• Ancient JSR

• Started in 2001

• Approved a year ago (March 2014)

• javax.caching API + TCK + reference implementation

• Eases integration of a cache in frameworks

@ljacomet#ehcachedvx

JSR-107• Ancient JSR

• Started in 2001

• Approved a year ago (March 2014)

• javax.caching API + TCK + reference implementation

• Eases integration of a cache in frameworks

• Ongoing discussions about a version 2.0

• Async API for example

@ljacomet#ehcachedvx

JSR-107 : Features

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

•CacheEntryListener

• Created/Updated

• Removed/Expired

• Get old value

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

•CacheEntryListener

• Created/Updated

• Removed/Expired

• Get old value

•Entry processor

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

•CacheEntryListener

• Created/Updated

• Removed/Expired

• Get old value

•Entry processor

•Annotations

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

•CacheEntryListener

• Created/Updated

• Removed/Expired

• Get old value

•Entry processor

•Annotations

•MBeans, exposing

• Configuration

• Statistics

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

•CacheEntryListener

• Created/Updated

• Removed/Expired

• Get old value

•Entry processor

•Annotations

•MBeans, exposing

• Configuration

• Statistics

•No capacity control !

@YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx

Demo

@YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx

And my XML??

@ljacomet#ehcachedvx

JSR-107 configuration• Programmatic only

• Enter the world of provider extensions

• Ehcache 3 expects CacheManager URI to resolve to a configuration file

• From there, multiple scenarios are possible

@YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx

Demo

Caching in an application

@ljacomet#ehcachedvx

public BusinessObject computeAndWin(String param1, String param2) { String key = createKey(param1, param2); BusinessObject cachedResult = cache.get(key); if (cachedResult == null) { cachedResult = loadAndCompute(param1, param2); cache.put(key, cachedResult); } return cachedResult;}

Cache Aside

@ljacomet#ehcachedvx

Cache Aside : not so easy

@ljacomet#ehcachedvx

Cache Aside : not so easy• Requires synchronisation between

• cache

• and system of record

@ljacomet#ehcachedvx

Cache Aside : not so easy• Requires synchronisation between

• cache

• and system of record

• JSR-107 has no locking option

@ljacomet#ehcachedvx

Cache Aside : not so easy• Requires synchronisation between

• cache

• and system of record

• JSR-107 has no locking option

• Potential to be hairy code quite fast

@ljacomet#ehcachedvx

Cache Aside: options

@ljacomet#ehcachedvx

Cache Aside: options• Rely on the abstraction of your framework

• Spring Caching

• Hibernate

• …

@ljacomet#ehcachedvx

Cache Aside: options• Rely on the abstraction of your framework

• Spring Caching

• Hibernate

• …

• Alternatives ?

@ljacomet#ehcachedvx

public BusinessObject computeAndWin(String param1, String param2) { return cache.get(createKey(param1, param2));}

Cache Through

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

@ljacomet#ehcachedvx

Cache Through

@ljacomet#ehcachedvx

Cache Through• get* => CacheLoader

• cache miss indicates no data available

@ljacomet#ehcachedvx

Cache Through• get* => CacheLoader

• cache miss indicates no data available

• put* => CacheWriter

• Each use means writing to the system of record

@ljacomet#ehcachedvx

Cache Through• get* => CacheLoader

• cache miss indicates no data available

• put* => CacheWriter

• Each use means writing to the system of record

• Constraints: APIs of CacheLoader / CacheWriter

@ljacomet#ehcachedvx

Cache Through : JSR-107

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

• putIfAbsent(K key, V value): boolean

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

• putIfAbsent(K key, V value): boolean

• Ignores CacheLoader but not CacheWriter

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

• putIfAbsent(K key, V value): boolean

• Ignores CacheLoader but not CacheWriter

• Ehcache 3 allows to tweak this behaviour through configuration

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

• putIfAbsent(K key, V value): boolean

• Ignores CacheLoader but not CacheWriter

• Ehcache 3 allows to tweak this behaviour through configuration

• Behaviour consistent across atomic operations

@ljacomet#ehcachedvx

Cache Through : Write Behind

@ljacomet#ehcachedvx

Cache Through : Write Behind• Asynchronous writes to the the system of record

• User thread no longer suffers from the latency of the write

@ljacomet#ehcachedvx

Cache Through : Write Behind• Asynchronous writes to the the system of record

• User thread no longer suffers from the latency of the write

• Introduces its own complexities

• Write queue: persistent or not?

• Mutation guarantee: once and only once? at least once?

• What about cache eviction?

Resilience strategy

@ljacomet#ehcachedvx

Resilience strategy

@ljacomet#ehcachedvx

Resilience strategy• Proposition:

“A cache level error should not be the cause for

a user level error “

@ljacomet#ehcachedvx

Resilience strategy• Proposition:

“A cache level error should not be the cause for

a user level error “

P.S. This is ongoing development and subject to change

@ljacomet#ehcachedvx

Ehcache 3

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

• In memory cache => No exceptions in the execution thread

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

• In memory cache => No exceptions in the execution thread

• Default is to log errors, will be configurable / replaceable

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

• In memory cache => No exceptions in the execution thread

• Default is to log errors, will be configurable / replaceable

• Advanced setups (Distributed + write behind for example)

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

• In memory cache => No exceptions in the execution thread

• Default is to log errors, will be configurable / replaceable

• Advanced setups (Distributed + write behind for example)

• Force user to think about the use cases where the cache can become inconsistent

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

• In memory cache => No exceptions in the execution thread

• Default is to log errors, will be configurable / replaceable

• Advanced setups (Distributed + write behind for example)

• Force user to think about the use cases where the cache can become inconsistent

• Will be required to configure a handler for these cases

Ehcache 3 and Terracotta

@ljacomet#ehcachedvx

Offheap solution

@ljacomet#ehcachedvx

Offheap solution• Solving Garbage Collector pauses on large heaps

@ljacomet#ehcachedvx

Offheap solution• Solving Garbage Collector pauses on large heaps

• Enables extreme cache scale up

• Multi TerraBytes … as long as your server allows it

@ljacomet#ehcachedvx

Offheap solution• Solving Garbage Collector pauses on large heaps

• Enables extreme cache scale up

• Multi TerraBytes … as long as your server allows it

• Production tested implementation

• 2.0.0 available on GitHub

• https://github.com/Terracotta-OSS/offheap-store

@ljacomet#ehcachedvx

Clustering solution

@ljacomet#ehcachedvx

Clustering solution• Sharing of data across multiple JVMs

@ljacomet#ehcachedvx

Clustering solution• Sharing of data across multiple JVMs

• Terracotta 4.3.0 again has an OpenSource offering

• Integration with Ehcache 2.10.0

@ljacomet#ehcachedvx

Clustering solution• Sharing of data across multiple JVMs

• Terracotta 4.3.0 again has an OpenSource offering

• Integration with Ehcache 2.10.0

• Ehcache 3 will have clustering option in OpenSource

• Code starting to appear on GitHub

@YourTwitterHandle@YourTwitterHandle@ljacomet#ehcachedvx

Questions ?