2. @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
3. @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?
6. @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
8. @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
9. @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
10. @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 !
11. @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)
12. @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
14. @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
15. @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)
26. @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
27. @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
29. @ljacomet#DevoxxMA #ehcache
Lifecycle - explicit
• CacheManager
• init / close methods
• create and close caches
• PersistentCacheManager
• Maintenance mode
• Destroy cache data
30. @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
32. @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
45. @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
74. @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
75. @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
77. @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
78. @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