Your SlideShare is downloading. ×
0
Cold Hard Cache
    Alex Miller (@puredanger)
Agenda

• Why cache?


• Ehcache


• Hibernate 2nd level cache


• Terracotta


• The future....
Why cache?

• Temporal locality


• Non-uniform distribution
Temporal locality




Items:
Temporal locality


Cache:

Items:
Temporal locality

Hits:               0%
Cache:

Items:
Temporal locality

Hits:               0%
Cache:

Items:

Items:
Temporal locality

Hits:               0%
Cache:

Items:

Items:

Cache:
Temporal locality

Hits:               0%
Cache:

Items:

Items:

Cache:
Hits:               65%
Non-uniform distribution
              Web page hits, ordered by rank
      3200                                       100...
Non-uniform distribution
              Web page hits, ordered by rank
      3200                                       100...
Cache is always good right?

• Watch out for:


   • Amdahl’s law


   • Memory


   • Concurrency


   • Copy cost
Reduce latency

            Page build   Data retrieval

  2000


  1500


  1000


   500


     0
            No cache  ...
Database offload




Operations
Per Second




                  Amount of Data
DBs are sized to peak load




Operations
Per Second




                         Amount of Data
Strive to downsize DBs



                   Frequently accessed app data:
                   Shared Memory (Transactional...
Ehcache
Ehcache history

• First created in 2003 by Greg Luck


• Most widely used Java cache - 100k’s of deployments


• Apache 2...
Ehcache Architecture
Ehcache Features

• In-memory and spill-to-disk storage


• Cache bootstrap loaders


• Cache replication via listener API...
Ehcache 1.6 performance
Ehcache Performance vs memcached
Hibernate Second-Level Cache
Hibernate Caching


                      Application Thread            Application Thread



                           S...
Entity and collection caches

• Entity and collection cache regions


• Mark a Hibernate entity or a collection in an enti...
Query Cache

• Query cache regions


   • Mark HQL, Criteria, Query as cacheable


   • Store result set id values


• Tim...
Terracotta as cache
DistributedCache

• High-throughput clustered coherent cache


• Simple interface - basically ConcurrentMap


• Eviction o...
DistributedCache Example

 CacheConfig config = CacheConfigFactory.newConfig();
 config.setMaxTTISeconds(30 * 60)
       ....
DistributedCache features

• Built on high throughput ConcurrentDistributedMap


• Expiration based on either TTI or TTL

...
Terracotta Hibernate Second Level Cache

 • Easy integration and configuration


 • Supports entity, collection, and query ...
Enabling Second Level Cache

• Mark your entities with a cache concurrency strategy


  • In hibernate.cfg.xml:   <cache u...
Enabling Second Level Cache
• Define the tc-hibernate-cache.xml in your classpath

<?xml version=”1.0” encoding=”UTF-8”?>
<...
New Hibernate cache visibility
Performance - Read-Only Comparison


                                           Throughput                                ...
The future of caching...
Wonder twin powers, activate!



      Terracotta                                          Ehcache




• “Standard” cache ...
Single node and replicated Ehcache

• Same license, code base, and API


• Better visibility


• Better performance testin...
Clustered Ehcache

• Short release for initial integration (probably Ehcache 1.7)


   • Clustered store - partial API sup...
Hibernate 2nd level cache

• More efficient in-memory and total count eviction (3.1.1)


• Better visibility of memory cond...
Thanks!

• Terracotta Open Source JVM clustering:

   • http://www.terracotta.org

• Apress: “The Definitive Guide to Terra...
Upcoming SlideShare
Loading in...5
×

Cold Hard Cache

3,401

Published on

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

Published in: Technology, News & Politics
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,401
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
123
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Transcript of "Cold Hard Cache"

  1. 1. Cold Hard Cache Alex Miller (@puredanger)
  2. 2. Agenda • Why cache? • Ehcache • Hibernate 2nd level cache • Terracotta • The future....
  3. 3. Why cache? • Temporal locality • Non-uniform distribution
  4. 4. Temporal locality Items:
  5. 5. Temporal locality Cache: Items:
  6. 6. Temporal locality Hits: 0% Cache: Items:
  7. 7. Temporal locality Hits: 0% Cache: Items: Items:
  8. 8. Temporal locality Hits: 0% Cache: Items: Items: Cache:
  9. 9. Temporal locality Hits: 0% Cache: Items: Items: Cache: Hits: 65%
  10. 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. 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. 12. Cache is always good right? • Watch out for: • Amdahl’s law • Memory • Concurrency • Copy cost
  13. 13. Reduce latency Page build Data retrieval 2000 1500 1000 500 0 No cache With cache
  14. 14. Database offload Operations Per Second Amount of Data
  15. 15. DBs are sized to peak load Operations Per Second Amount of Data
  16. 16. Strive to downsize DBs Frequently accessed app data: Shared Memory (Transactional) Operations Per Second Business Record Data : Database Amount of Data
  17. 17. Ehcache
  18. 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. 19. Ehcache Architecture
  20. 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. 21. Ehcache 1.6 performance
  22. 22. Ehcache Performance vs memcached
  23. 23. Hibernate Second-Level Cache
  24. 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. 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. 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. 27. Terracotta as cache
  28. 28. DistributedCache • High-throughput clustered coherent cache • Simple interface - basically ConcurrentMap • Eviction options • TTI, TTL • Max in-memory size, max total size limits
  29. 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. 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. 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. 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. 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. 34. New Hibernate cache visibility
  35. 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. 36. The future of caching...
  37. 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. 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. 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. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×