Upload
louis-jacomet
View
937
Download
0
Embed Size (px)
Citation preview
@ljacomet#DevoxxMA #ehcache
Ehcache 3 JSR-107 on steroids
Louis Jacomet - Lead Software Engineer
@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
@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?
@ljacomet#DevoxxMA #ehcache
Agenda•Caching and JSR-107
•Ehcache past and present
•Ehcache 3 future
@ljacomet#DevoxxMA #ehcache
Caching and JSR-107
@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
@ljacomet#DevoxxMA #ehcache
Caches all over the place
CPU
Browser
Network
Disk
@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
@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
@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 !
@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)
@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
@ljacomet#DevoxxMA #ehcache
Ehcache past and present
@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
@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)
@ljacomet#DevoxxMA #ehcache
Overflow model
Heap
Disk
@ljacomet#DevoxxMA #ehcache
Heap
Disk
Overflow model
@ljacomet#DevoxxMA #ehcache
Disk
Heap
Overflow model
@ljacomet#DevoxxMA #ehcache
Disk
Heap
Overflow model
@ljacomet#DevoxxMA #ehcache
Disk
Heap
Overflow model
@ljacomet#DevoxxMA #ehcache
Authoritative tier model
Heap
Disk
@ljacomet#DevoxxMA #ehcache
Disk
Heap
Authoritative tier model
@ljacomet#DevoxxMA #ehcache
Heap
Disk
Authoritative tier model
@ljacomet#DevoxxMA #ehcache
Heap
Disk
Authoritative tier model
@ljacomet#DevoxxMA #ehcache
Heap
Disk
Authoritative tier model
@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
@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
@ljacomet#DevoxxMA #ehcache
Ehcache 3 The future is here
@ljacomet#DevoxxMA #ehcache
Lifecycle - explicit• CacheManager
• init / close methods
• create and close caches
• PersistentCacheManager
• Maintenance mode
• Destroy cache data
@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
@ljacomet#DevoxxMA #ehcache
Serialization• Required as soon as we move away from the
heap
• Fully customisable in Ehcache 3
@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
@ljacomet#DevoxxMA #ehcache
Cache through - miss
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - miss
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - miss
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - miss
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - miss
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - hit
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - hit
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - hit
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - write
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - write
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - write
Application Cache
Database
@ljacomet#DevoxxMA #ehcache
Cache through - write
Application Cache
Database
@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
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind - eviction
(K1, V1)
(K1, V1)
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind - eviction
(K1, V1)
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind - eviction
(K1, V1)
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind - eviction
(K1, V1)
@ljacomet#DevoxxMA #ehcache
Application Cache
Database
queue
Write behind - eviction
(K1, V1)
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Application Cache
queue
Database
@ljacomet#DevoxxMA #ehcache
Write behind - failure
Database
@ljacomet#DevoxxMA #ehcache
Database
Write behind - failure
@ljacomet#DevoxxMA #ehcache
queue
Database
Write behind - failure
@ljacomet#DevoxxMA #ehcache
queue
Database
Write behind - failure
@ljacomet#DevoxxMA #ehcache
queue
Database
Write behind - failure
@ljacomet#DevoxxMA #ehcache
Expiry• Offers context
@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
@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
@ljacomet#DevoxxMA #ehcache
Clustering using Terracotta
@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
@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
@ljacomet#DevoxxMA #ehcache
Q&AThank you for your time