Presented at JavaOne 2015.
JSR107, aka the Temporary Caching API for the Java Platform, has now been finalized almost 2 years ago. We've heard all about its ease of use and capabilities. But there is much left unaddressed. The good news is that the EG is looking at addressing many of the current shortcomings... But what do you do now? Go for proprietary APIs?!
Ehcache, the de facto caching API for 10 years now, has gone through a major API revamp: Ehcache3. One major theme, beyond its usual ease of use, was JSR107. Natively integrating it, but also looking beyond. With close to no API tie-ins, Ehcache3 lets you extend the JSR107 API transparently to go beyond the specification: topology-wise: whether you want to go offheap and scale up, or scale out by clustering your caches; functionality-wise: using transactional caches, automatic resource control or even using a write-behind cache to scale out writes...
Best of all is that this isn't only minimally intrusive, it is also all free to use and available as part of the open-source Ehcache v3 that has been GA'ed earlier this year...
1. Ehcache 3
JSR-107 on Steroids
alex snaps — principal software engineer at terracotta
@alexsnaps
2. A little about me
• Introducing new bugs to the world for 30+ years
• Coding in Java for 16+ years
• Works at Terracotta for 6+ years
• DSO, Ehcache, Quartz, …
• OSS Contributor
• Into API design, concurrency, distributed systems…
3. A little of history
[014] - Easy Hibernate Cache 0.5
by Greg Luck (http://freshmeat.net/users/gregrluck/)
Saturday, November 29th 2003 12:54
Software Development :: Libraries :: Java Libraries
About: Easy Hibernate Cache is a fast and simple, pure Java, in-process
cache, which acts as a pluggable cache for Hibernate 2.1. It has a small
memory footprint, minimal dependencies, and full documentation.
License: The Apache License
4. A little of history
• Terracotta founded in 2003, in San Francisco
• Open sourced DSO in 2006
• Acquires Ehcache in Aug. 2009
• Releases Ehcache 2.0 Mar. 2010
• Software AG acquires Terracotta in 2011
• JSR-107 Final Mar. 2014 (13 years after the JSR review ballot!)
• Work on Ehcache 3.0 begins…
6. JSR 107
• 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
7. Caching
• Data structure holding temporary copy of data
• Trades memory usage for reduced latency
• Targets:
• Data which is reused
• Data which is expensive to retrieve / compute
• Keeps the data close(r) to the processing unit
13. Tiered Storage
• Very similar to Ehcache 2.x
• Lowest & slowest tier is the “authority”
• Higher & faster tiers effectively act as caches
• Available today:
• Heap,
• Off-Heap,
• Disk
14. Store by value vs. by ref
• JSR 107 “store modes”
• “by value” (default),
• “by reference” (optional)
• What about “I don’t care” ?
• Introducing Ehcache org.ehcache.spi.copy.Copier
15. Custom Serialization
• Defaults to Java Serialization for
• JSR 107’s store-by-value
• Storing data elsewhere than the heap
• Introducing org.ehcache.spi.serialization.Serializer
16. Proper Typing
• Why is that important?
• No one wants to catch ClassCastException
• Application lifecycle!
• Ehcache records the key & value types in all its stores!
• But… there is a MutableConfiguration bug
17. CacheLoader & -Writer
• Ehcache’s CacheLoader/Writer API
• Enables the Cache through pattern
• Consistently across all Cache operations
• Single CacheLoaderWriter interface
• Throughput favoring Cache, not SoR
18. WriteBehind
• Scale out writes to your SoR
• Batching and/or coalescing them
• Even simpler than with Ehcache 2
• No more sizing gotcha!
20. Expiry Policy
• JSR 107’s ExpiryPolicy
• No contextual information about the Expiry calculated
• Works for simple expiry (Accessed, Created, Modified, …)
• Introducing the org.ehcache.expiry.Expiry
21. Eviction Prioritizer & Veto
• Eviction not addressed by 107 at all
• org.ehcache.config.EvictionPrioritizer
• org.ehcache.config.EvictionVeto
• Both are hints though!
22. Transactions
• Full blown XA Resource
• including recovery support
• Developed as a independent Ehcache module
• Currently (still?) included in the main “distribution”
24. Automatic Resource Control
• Size your on heap caches in natural units
• n bytes
• Percentage of the (maximal) available VM heap
• Define “resource pools” shared across caches
25. Clustering
• Terracotta Server Array version 5.0 (work in progress)
• Single stripe available as FOSS
• Uses our offheap-store library
• Passive fail-over
• Ehcache’s consistency mode:
• Strong
• Eventual
• Proper life cycle (finally!)
26. 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
27. … and moar!
• Looking at Hibernate 2nd Level Cache (through 107!)
• Advanced features to Spring (& Spring Boot)
• … your ideas!?