Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Distributed caching with java JCache

1,966 views

Published on

This presentation provides a comparison of types of caches highlighting the use of distributed caching. This is followed by an introduction to JCache API. Code samples are at [2].

This is the presentation I did at Java Colombo JUG. [2]

[1] https://github.com/kasunbg/jcache-samples

[2] http://www.meetup.com/java-colombo/events/223811796/

Published in: Software
  • Be the first to comment

Distributed caching with java JCache

  1. 1. Distributed Caching with Java Kasun Gajasinghe Senior Software Engineer WSO2
  2. 2. ● What & Why for (Distributed) Caching ● Different types of Caches ● Cache access patterns ● Java JCache specification ● Demo ● Q&A Agenda
  3. 3. (... but why do I want to Cache?)O O O source: http://no7scramblecross.blogspot.com/2009/04/speaking-of-cats.html
  4. 4. Caching? … Distributed? Gotcha!! source: http://matome.naver.jp/odai/2140238539583350401/2140238875587055003
  5. 5. ● 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
  6. 6. ● improve the response times by reducing data access latency ● avoid cost of repeatedly creating objects ● reduces IO overhead Caching
  7. 7. ● limited memory size ● consistency - consistency between cached data vs actual data ● durability - cache expiration/invalidation ● scalability Caching considerations
  8. 8. Types of caches ● local cache ● replicated cache ● distributed cache
  9. 9. Local Cache ● Local Cache a cache that is local to (completely contained within) a particular cluster node
  10. 10. Local Cache - example
  11. 11. Local Cache - with TTL
  12. 12. Local Cache Pros: ● simplicity ● performance ● no serialization/deserialization overhead Cons: ● not fault-tolerant ● scalability
  13. 13. Replicated cache ● a cache that replicates its data to all cluster nodes
  14. 14. GET in replicated cache Each cluster node (JVM) accesses the data from its own memory, i.e. a local read.
  15. 15. PUT in replicated cache Pushing the new version of the data to all other cluster nodes
  16. 16. 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
  17. 17. Distributed Cache a cache that partitions its data among all cluster nodes
  18. 18. GET in Distributed Cache Access often must go over the network to another cluster node
  19. 19. Resolving known limitation of replicated cache: PUT in Distributed Cache
  20. 20. 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
  21. 21. Failover in Distributed Cache Failover involves promoting backup data to be primary storage.
  22. 22. 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)
  23. 23. Cache Access Patterns ● cache aside ● read-through ● write-through
  24. 24. 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
  25. 25. 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.
  26. 26. Java JCache specification ● Standard Java Caching API ● javax.cache.* ● Implementation can be local or distributed
  27. 27. JCache API ● javax.cache.Cache ● javax.cache.CacheManager ● javax.cache.spi.CachingProvider
  28. 28. JCache API
  29. 29. JCache API - Annotations • Use the cache
  30. 30. JCache API - Annotations • Put into the cache
  31. 31. JCache API - Annotations • Remove from the cache
  32. 32. 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
  33. 33. JCache implementations • Hazelcast • Oracle Coherence • Pivotal Gemfire • Ehcache • Infinispan • Gridgain/Ignite • Couchbase
  34. 34. Q&A
  35. 35. THANK YOU!
  36. 36. 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

×