79
@ljacomet #DevoxxMA #ehcache Ehcache 3 JSR-107 on steroids Louis Jacomet - Lead Software Engineer

Ehcache 3: JSR-107 on steroids at Devoxx Morocco

Embed Size (px)

Citation preview

Page 1: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Ehcache 3 JSR-107 on steroids

Louis Jacomet - Lead Software Engineer

Page 2: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Who am I?• Louis Jacomet

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

• @ Terracotta (Software AG) since 2013

• Working on Ehcache mostly

• Team manager and fiddling in infrastructure

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

• Enjoys concurrency, API design and hard problems

Page 3: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

And what about you?• Who knows nothing about caching?

• Who already uses caching in production?

• Who had production problems related to caching?

• Who knows about JCache?

Page 4: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Agenda•Caching and JSR-107

•Ehcache past and present

•Ehcache 3 future

Page 5: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Caching and JSR-107

Page 6: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Caching• Data structure holding a temporary copy of data

• Trade off between higher memory usage and reduced latency

• Targets

• Data which is reused

• Data which is expensive to compute / retrieve

Page 7: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Caches all over the place

CPU

Browser

Network

Disk

Page 8: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

What should I cache?• Keys

• Proper equals/hashcode

• Immutable

• Small(ish)

• Value

• Proper equals/hashcode

• Ideally immutable

• Think concurrent otherwise

• Size matters

Page 9: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

JSR-107 javax.cache

• Java Community Process driven standard

• Specifies API and semantics for temporary, in-memory caching of Java objects, including object creation, shared access, spooling, invalidation, and consistency across JVM's

Page 10: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

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 11: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

JSR-107 gotcha’s• MutableConfiguration

• Always invoke MutableConfiguration.setTypes(Class, Class)

• Otherwise Cache only knows about Object

• Adding a CacheLoader factory is not enough - need to invoke setReadThrough(boolean)

Page 12: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

JSR-107 gotcha’s (2)• Compare and Swap operations with a CacheLoader

• Loader is ignored

• putIfAbsent checks Cache only, not underlying system of record

• Results in a weird contract

Page 13: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Ehcache past and present

Page 14: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

A bit of history• Announced in November 2003 on

• v1.0 released on September 2004

• Only a few days before Java 5 and generics

• Acquired in 2009 by

• v2.0 released in March 2010

• Terracotta acquired in 2011 by

Page 15: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Storage model: tiers• Multiple storage tiers

• Heap: like objects in a regular hash map

• Off-heap: binary form stored outside of Garbage Collector reach

• Disk: binary form stored on disk

• Clustered: binary form stored on other server(s)

Page 16: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Overflow model

Heap

Disk

Page 17: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Heap

Disk

Overflow model

Page 18: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Disk

Heap

Overflow model

Page 19: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Disk

Heap

Overflow model

Page 20: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Disk

Heap

Overflow model

Page 21: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Authoritative tier model

Heap

Disk

Page 22: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Disk

Heap

Authoritative tier model

Page 23: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Heap

Disk

Authoritative tier model

Page 24: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Heap

Disk

Authoritative tier model

Page 25: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Heap

Disk

Authoritative tier model

Page 26: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Why the new model?• Predictable latency

• Every put pays the price of the lower / slowest tier

• No degradation when a higher tier gets full

• Preserves fast(er) access for hot set

• When a mapping is accessed it moves to the fastest tier available

Page 27: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Not a cache feature?• Search

• What does searching on a hot set means?

• Even without interacting with the cache, two execution of the same query could return different results

• Pinning

• Prevent eviction from cache entries

• May adversly effect the ability of the cache to respect size constraints

Page 28: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Ehcache 3 The future is here

Page 29: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Lifecycle - explicit• CacheManager

• init / close methods

• create and close caches

• PersistentCacheManager

• Maintenance mode

• Destroy cache data

Page 30: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache<K, V> for proper typing• Ehcache 2.x not generic, was time to change

that

• Needs to be known at runtime

• No one wants a ClassCastException

• Cache data may live beyond application lifecycle

• Usage show that frequently key and values are of the same type in a given cache

Page 31: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Serialization• Required as soon as we move away from the

heap

• Fully customisable in Ehcache 3

Page 32: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

CacheLoaderWriter• Enables the cache-through pattern

• Consistent across all cache operations

• Single interface

• Loader only - make sure write related methods throw

• Writer only - what?

• Anyone having a good use case for this - talk to us

Page 33: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - miss

Application Cache

Database

Page 34: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - miss

Application Cache

Database

Page 35: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - miss

Application Cache

Database

Page 36: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - miss

Application Cache

Database

Page 37: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - miss

Application Cache

Database

Page 38: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - hit

Application Cache

Database

Page 39: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - hit

Application Cache

Database

Page 40: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - hit

Application Cache

Database

Page 41: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - write

Application Cache

Database

Page 42: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - write

Application Cache

Database

Page 43: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - write

Application Cache

Database

Page 44: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Cache through - write

Application Cache

Database

Page 45: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind• Enables scaling your writes

• Putter does not pay for write latency

• Batching and coalescing options

• Simplified compared to 2.x - no more sizing gotchas

Page 46: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 47: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 48: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 49: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 50: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 51: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 52: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 53: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 54: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind

Page 55: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind - eviction

(K1, V1)

(K1, V1)

Page 56: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind - eviction

(K1, V1)

Page 57: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind - eviction

(K1, V1)

Page 58: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind - eviction

(K1, V1)

Page 59: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Application Cache

Database

queue

Write behind - eviction

(K1, V1)

Page 60: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 61: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 62: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 63: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 64: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 65: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 66: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 67: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Application Cache

queue

Database

Page 68: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Write behind - failure

Database

Page 69: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Database

Write behind - failure

Page 70: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

queue

Database

Write behind - failure

Page 71: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

queue

Database

Write behind - failure

Page 72: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

queue

Database

Write behind - failure

Page 73: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Expiry• Offers context

Page 74: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Eviction Veto and Prioritizer• Veto

• Enables to suggest a veto on certain mappings

• Prioritizer

• Allows to sort sample before picking eviction victim

• Not applicable to all stores

• Both are hints only - capacity always takes priority

Page 75: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Transactions• Full blown XA resource

• includes recovery support

• Needs work for transaction managers integration

• Code isolated in its own module

• Currently part of the main Ehcache 3 jar

Page 76: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Clustering using Terracotta

Page 77: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Automatic Resource Control• Enables sizing in natural units

• n bytes on heap

• Percentage of the max heap available

• Define resource pools at the cache manager

• Cross eviction amongst caches

• Enables auto balancing

Page 78: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Resilience strategy• A cache is to be resilient

• Yet… sh*t happens

• Storage failures

• SLA violations (timeouts?)

• Very explicit contract at this time

• To be refined when clustering is wrapping up

Page 79: Ehcache 3: JSR-107 on steroids at Devoxx Morocco

@ljacomet#DevoxxMA #ehcache

Q&AThank you for your time