Distributed Caching
with Java
Kasun Gajasinghe
Senior Software Engineer
WSO2
● What & Why for (Distributed) Caching
● Different types of Caches
● Cache access patterns
● Java JCache specification
● Demo
● Q&A
Agenda
(... but why do I
want to Cache?)
O O O
source: http://no7scramblecross.blogspot.com/2009/04/speaking-of-cats.html
Caching? … Distributed?
Gotcha!!
source: http://matome.naver.jp/odai/2140238539583350401/2140238875587055003
● Application performance - get around
bottlenecks arising from using data that is
expensive to retrieve
○ DB or web service
○ data that is hard to calculate
○ monetary aspects
Why software Caching
● improve the response times by reducing data
access latency
● avoid cost of repeatedly creating objects
● reduces IO overhead
Caching
● limited memory size
● consistency - consistency between cached
data vs actual data
● durability - cache expiration/invalidation
● scalability
Caching considerations
Types of caches
● local cache
● replicated cache
● distributed cache
Local Cache
● Local Cache a cache that is local to
(completely contained within) a particular
cluster node
Local Cache - example
Local Cache - with TTL
Local Cache
Pros:
● simplicity
● performance
● no serialization/deserialization overhead
Cons:
● not fault-tolerant
● scalability
Replicated cache
● a cache that replicates its data to all cluster
nodes
GET in replicated cache
Each cluster node (JVM) accesses the data from its own
memory, i.e. a local read.
PUT in replicated cache
Pushing the new version of the data to all other cluster
nodes
Replicated Cache
Pros:
● best read performance
● fault–tolerant
● linear performance scalability for reads
Cons:
● poor write performance
● memory consumption
● additional network load
● poor and limited scalability for writes
Distributed Cache
a cache that partitions its data among all
cluster nodes
GET in Distributed Cache
Access often must go over the network to another cluster
node
Resolving known limitation of replicated cache:
PUT in Distributed Cache
PUT in Distributed Cache
● the data is being sent to a primary cluster node
and a backup cluster node if backup count is 1
● modifications to the cache are not considered
complete until all backups have acknowledged
receipt of the modification, i.e. slight
performance penalty
● such overhead guarantees that data consistency
is maintained and no data is lost
Failover in Distributed Cache
Failover involves promoting backup data to be
primary storage.
Distributed Cache
Pros:
● linear performance scalability for reads and writes
● fault–tolerant
Cons:
● increased latency of reads (due to network round-trip
and serialization/deserialization expenses)
Cache Access Patterns
● cache aside
● read-through
● write-through
Cache Aside Pattern
● application is responsible of reading and writing to the
storage.
● cache doesn’t interact with storage
● the cache is ‘kept aside’ as a faster and more scalable in-
memory data store
Read-through/Write-through
● application treats the cache as the main data
store, and reads/writes data from/to it
● cache is responsible to read/write the data
into the actual storage.
Java JCache specification
● Standard Java Caching API
● javax.cache.*
● Implementation can be local or distributed
JCache API
● javax.cache.Cache
● javax.cache.CacheManager
● javax.cache.spi.CachingProvider
JCache API
JCache API - Annotations
• Use the cache
JCache API - Annotations
• Put into the cache
JCache API - Annotations
• Remove from the cache
javax.cache.expiry.
● AccessedExpiryPolicy: Expires after a given set of time measured from creation of the cache entry, the expiry timeout is
updated on accessing the key.
● CreatedExpiryPolicy: Expires after a given set of time measured from creation of the cache entry, the expiry timeout is
never updated.
● EternalExpiryPolicy: Never expires, this is the default behavior, similar to ExpiryPolicy to be set to null.
● ModifiedExpiryPolicy
● TouchedExpiryPolicy
JCache API - Expiry Policies
JCache implementations
• Hazelcast
• Oracle Coherence
• Pivotal Gemfire
• Ehcache
• Infinispan
• Gridgain/Ignite
• Couchbase
Q&A
THANK YOU!
1. http://docs.hazelcast.org/docs/latest/manual/html-single/hazelcast-
documentation.html#hazelcast-jcache
2. https://spring.io/blog/2014/04/14/cache-abstraction-jcache-jsr-107-
annotations-support
References

Distributed caching with java JCache