Cold Hard Cache
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Cold Hard Cache

on

  • 4,946 views

An examination of caching, Ehcache, Hibernate second level-cache, Terracotta caching, and the future.

An examination of caching, Ehcache, Hibernate second level-cache, Terracotta caching, and the future.

Statistics

Views

Total Views
4,946
Views on SlideShare
4,892
Embed Views
54

Actions

Likes
6
Downloads
122
Comments
0

2 Embeds 54

http://tech.puredanger.com 41
http://www.slideshare.net 13

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Cold Hard Cache Presentation Transcript

  • 1. Cold Hard Cache Alex Miller (@puredanger)
  • 2. Agenda • Why cache? • Ehcache • Hibernate 2nd level cache • Terracotta • The future....
  • 3. Why cache? • Temporal locality • Non-uniform distribution
  • 4. Temporal locality Items:
  • 5. Temporal locality Cache: Items:
  • 6. Temporal locality Hits: 0% Cache: Items:
  • 7. Temporal locality Hits: 0% Cache: Items: Items:
  • 8. Temporal locality Hits: 0% Cache: Items: Items: Cache:
  • 9. Temporal locality Hits: 0% Cache: Items: Items: Cache: Hits: 65%
  • 10. Non-uniform distribution Web page hits, ordered by rank 3200 100% 2400 75% 1600 50% 800 25% 0 0% Page views, ordered by rank Pageviews per rank % of total hits per rank
  • 11. Non-uniform distribution Web page hits, ordered by rank 3200 100% 2400 75% 1600 50% 800 25% 0 0% Page views, ordered by rank Pageviews per rank % of total hits per rank
  • 12. Cache is always good right? • Watch out for: • Amdahl’s law • Memory • Concurrency • Copy cost
  • 13. Reduce latency Page build Data retrieval 2000 1500 1000 500 0 No cache With cache
  • 14. Database offload Operations Per Second Amount of Data
  • 15. DBs are sized to peak load Operations Per Second Amount of Data
  • 16. Strive to downsize DBs Frequently accessed app data: Shared Memory (Transactional) Operations Per Second Business Record Data : Database Amount of Data
  • 17. Ehcache
  • 18. Ehcache history • First created in 2003 by Greg Luck • Most widely used Java cache - 100k’s of deployments • Apache 2.0 license • JSR 107 Java cache implementation
  • 19. Ehcache Architecture
  • 20. Ehcache Features • In-memory and spill-to-disk storage • Cache bootstrap loaders • Cache replication via listener API - RMI, JGroups, JMS • Cache server with REST and SOAP APIs • Servlet caching filter API • Hibernate second-level cache support
  • 21. Ehcache 1.6 performance
  • 22. Ehcache Performance vs memcached
  • 23. Hibernate Second-Level Cache
  • 24. Hibernate Caching Application Thread Application Thread Session Session 1st Level Cache Cache Cache Cache Concurrency Concurrency Concurrency Hibernate Strategy Strategy Strategy CacheProvider 2nd Level Cache Cache Cache Cache Region Region Region Database
  • 25. Entity and collection caches • Entity and collection cache regions • Mark a Hibernate entity or a collection in an entity as @Cacheable • Specify a cache concurrency strategy • ReadOnly, ReadWrite, NonstrictReadWrite, Transactional • Turn on second level caching in the Hibernate config
  • 26. Query Cache • Query cache regions • Mark HQL, Criteria, Query as cacheable • Store result set id values • Timestamp cache region - last update time for each entity type • Useful for caching natural key lookups (non-primary key) • ...but lots of hidden issues
  • 27. Terracotta as cache
  • 28. DistributedCache • High-throughput clustered coherent cache • Simple interface - basically ConcurrentMap • Eviction options • TTI, TTL • Max in-memory size, max total size limits
  • 29. DistributedCache Example CacheConfig config = CacheConfigFactory.newConfig(); config.setMaxTTISeconds(30 * 60) .setMaxTTLSeconds(2 * 60 * 60); DistributedCache<String, Person> cache = config.newCache(); Person person = new Person(.......); cache.put(“Alex”, person); Person cached = cache.get(“Alex”);
  • 30. DistributedCache features • Built on high throughput ConcurrentDistributedMap • Expiration based on either TTI or TTL • Both in-memory and total target max limits • Automatic memory management of caches • Coherent clustered cache
  • 31. Terracotta Hibernate Second Level Cache • Easy integration and configuration • Supports entity, collection, and query cache regions • Supports read-only, read-write, and nonstrict-read-write cache concurrency strategies • Hibernate-specific tooling • High performance with cache coherency
  • 32. Enabling Second Level Cache • Mark your entities with a cache concurrency strategy • In hibernate.cfg.xml: <cache usage="read-write"/> • With annotations: @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) • hibernate.cfg.xml • <property name="cache.use_second_level_cache">true</property> • <property name="cache.provider_class"> org.terracotta.hibernate.TerracottaHibernateCacheProvider</property>
  • 33. Enabling Second Level Cache • Define the tc-hibernate-cache.xml in your classpath <?xml version=”1.0” encoding=”UTF-8”?> <terracotta-hibernate-cache-configuration> <default-configuration> <time-to-idle-seconds>7200</time-to-idle-seconds> <time-to-live-seconds>7200</time-to-live-seconds> </default-configuration> <cache> <region-name>org.terracotta.authinator.domain.Account</region-name> <!-- as many region-names here as you want --> <configuration> <time-to-idle-seconds>600</time-to-idle-seconds> <time-to-live-seconds>600</time-to-live-seconds> </configuration> </cache> </terracotta-hibernate-cache-configuration> • Add the Terracotta Hibernate cache provider jar to your classpath • -cp terracotta-hibernate-cache-1.0.0.jar • Add the Terracotta Hibernate cache agent jar to your command line • -javaagent:terracotta-hibernate-agent-1.0.0.jar
  • 34. New Hibernate cache visibility
  • 35. Performance - Read-Only Comparison Throughput Latency 200K 100 Transactions per second 80 150K Avg Latency (ms) 60 100K 40 50K 20 0K 0 Database IMDG EhcacheTerracotta Database IMDG EhcacheTerracotta
  • 36. The future of caching...
  • 37. Wonder twin powers, activate! Terracotta Ehcache • “Standard” cache apis (Ehcache / JSR 107) • Low latency local cache • High throughput clustered cache • Coherent caching with options to degrade for greater performance • Support for both “copy” and “shared object” caching
  • 38. Single node and replicated Ehcache • Same license, code base, and API • Better visibility • Better performance testing -> improved concurrency and performance • Smooth migration path to...
  • 39. Clustered Ehcache • Short release for initial integration (probably Ehcache 1.7) • Clustered store - partial API support • Smooth upgrade from single node or replicated Ehcache • New management and visibility features
  • 40. Hibernate 2nd level cache • More efficient in-memory and total count eviction (3.1.1) • Better visibility of memory conditions • Improved query caching • Improved performance of core Terracotta (lock manager and memory manager)
  • 41. Thanks! • Terracotta Open Source JVM clustering: • http://www.terracotta.org • Apress: “The Definitive Guide to Terracotta” • by Ari Zilka, Alex Miller, Geert Bevin, Jonas Boner, Orion Letizi, Taylor Gautier • 2nd edition in progress.... • Alex Miller • @puredanger • http://tech.puredanger.com