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

Caching reboot: javax.cache & Ehcache 3

Embed Size (px)

Text of Caching reboot: javax.cache & Ehcache 3

Page 1: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Caching reboot

javax.cache & Ehcache 3

Page 2: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Agenda

Page 3: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Agenda• Why a cache?

Page 4: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

Page 5: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

• Caching in an application

• Cache aside

• Cache through

Page 6: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

• Caching in an application

• Cache aside

• Cache through

• Resilience strategy

Page 7: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Agenda• Why a cache?

• JSR-107

• APIs and features

• Provider extensions

• Caching in an application

• Cache aside

• Cache through

• Resilience strategy

• Extras

Page 8: Caching reboot: javax.cache & Ehcache 3

@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, …

Page 9: Caching reboot: javax.cache & Ehcache 3

Why a cache?

Page 10: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Why a cache?

Page 11: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

What were these numbers?

Page 12: Caching reboot: javax.cache & Ehcache 3

@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

Page 13: Caching reboot: javax.cache & Ehcache 3

@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

Page 14: Caching reboot: javax.cache & Ehcache 3

@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

Page 15: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

What were these numbers?

Page 16: Caching reboot: javax.cache & Ehcache 3

@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

Page 17: Caching reboot: javax.cache & Ehcache 3

@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

Page 18: Caching reboot: javax.cache & Ehcache 3

@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

Page 19: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

So what is a cache?

Page 20: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

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

Page 21: Caching reboot: javax.cache & Ehcache 3

@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

Page 22: Caching reboot: javax.cache & Ehcache 3

@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

Page 23: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Ehcache (3)

Page 24: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

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

• Breaks compatibility with 2.x line

Page 25: Caching reboot: javax.cache & Ehcache 3

@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

Page 26: Caching reboot: javax.cache & Ehcache 3

@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

Page 27: Caching reboot: javax.cache & Ehcache 3

JSR-107 - javx.caching

Page 28: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107

Page 29: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107• Ancient JSR

• Started in 2001

• Approved a year ago (March 2014)

Page 30: Caching reboot: javax.cache & Ehcache 3

@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

Page 31: Caching reboot: javax.cache & Ehcache 3

@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

Page 32: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107 : Features

Page 33: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

Page 34: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

Page 35: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

Page 36: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

•CacheEntryListener

• Created/Updated

• Removed/Expired

• Get old value

Page 37: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

JSR-107 : Features•CacheManager / Cache

•Expiration

• Creation/Access/Update

•Integration

• CacheLoader / Writer

•CacheEntryListener

• Created/Updated

• Removed/Expired

• Get old value

•Entry processor

Page 38: Caching reboot: javax.cache & Ehcache 3

@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

Page 39: Caching reboot: javax.cache & Ehcache 3

@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

Page 40: Caching reboot: javax.cache & Ehcache 3

@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 !

Page 41: Caching reboot: javax.cache & Ehcache 3

@[email protected]@ljacomet#ehcachedvx

Demo

Page 42: Caching reboot: javax.cache & Ehcache 3

@[email protected]@ljacomet#ehcachedvx

And my XML??

Page 43: Caching reboot: javax.cache & Ehcache 3

@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

Page 44: Caching reboot: javax.cache & Ehcache 3

@[email protected]@ljacomet#ehcachedvx

Demo

Page 45: Caching reboot: javax.cache & Ehcache 3

Caching in an application

Page 46: Caching reboot: javax.cache & Ehcache 3

@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

Page 47: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Aside : not so easy

Page 48: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Aside : not so easy• Requires synchronisation between

• cache

• and system of record

Page 49: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Aside : not so easy• Requires synchronisation between

• cache

• and system of record

• JSR-107 has no locking option

Page 50: Caching reboot: javax.cache & Ehcache 3

@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

Page 51: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Aside: options

Page 52: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

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

• Spring Caching

• Hibernate

• …

Page 53: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

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

• Spring Caching

• Hibernate

• …

• Alternatives ?

Page 54: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

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

Cache Through

Page 55: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

Page 56: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

Page 57: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

Page 58: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

Page 59: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through

Cache

Application code

RDBMS

Page 60: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through

Page 61: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through• get* => CacheLoader

• cache miss indicates no data available

Page 62: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through• get* => CacheLoader

• cache miss indicates no data available

• put* => CacheWriter

• Each use means writing to the system of record

Page 63: Caching reboot: javax.cache & Ehcache 3

@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

Page 64: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through : JSR-107

Page 65: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

Page 66: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

• putIfAbsent(K key, V value): boolean

Page 67: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through : JSR-107• Peculiar behaviours

• putIfAbsent(K key, V value): boolean

• Ignores CacheLoader but not CacheWriter

Page 68: Caching reboot: javax.cache & Ehcache 3

@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

Page 69: Caching reboot: javax.cache & Ehcache 3

@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

Page 70: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Cache Through : Write Behind

Page 71: Caching reboot: javax.cache & Ehcache 3

@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

Page 72: Caching reboot: javax.cache & Ehcache 3

@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?

Page 73: Caching reboot: javax.cache & Ehcache 3

Resilience strategy

Page 74: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Resilience strategy

Page 75: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Resilience strategy• Proposition:

“A cache level error should not be the cause for

a user level error “

Page 76: Caching reboot: javax.cache & Ehcache 3

@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

Page 77: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Ehcache 3

Page 78: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

Page 79: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Ehcache 3• Handle errors internally as much as possible

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

Page 80: Caching reboot: javax.cache & Ehcache 3

@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

Page 81: Caching reboot: javax.cache & Ehcache 3

@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)

Page 82: Caching reboot: javax.cache & Ehcache 3

@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

Page 83: Caching reboot: javax.cache & Ehcache 3

@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

Page 84: Caching reboot: javax.cache & Ehcache 3

Ehcache 3 and Terracotta

Page 85: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Offheap solution

Page 86: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Offheap solution• Solving Garbage Collector pauses on large heaps

Page 87: Caching reboot: javax.cache & Ehcache 3

@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

Page 88: Caching reboot: javax.cache & Ehcache 3

@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

Page 89: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Clustering solution

Page 90: Caching reboot: javax.cache & Ehcache 3

@ljacomet#ehcachedvx

Clustering solution• Sharing of data across multiple JVMs

Page 91: Caching reboot: javax.cache & Ehcache 3

@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

Page 92: Caching reboot: javax.cache & Ehcache 3

@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

Page 93: Caching reboot: javax.cache & Ehcache 3

@[email protected]@ljacomet#ehcachedvx

Questions ?