Distributed Caching, Using the JCACHE API and ehcache

50
2007 JavaOne SM Conference | Session TS-6175 | TS-6175 Distributed Caching Using the JCACHE API and ehcache, Including a Case Study on Wotif.com Greg Luck Maintainer, ehcache—http://ehcache.sf.net Member, JSR 107 JCACHE Expert Group Chief Architect, Wotif.com—http://wotif.com

Transcript of Distributed Caching, Using the JCACHE API and ehcache

Page 1: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 |

TS-6175

Distributed Caching Using the JCACHE API and ehcache, Including a Case Study on Wotif.comGreg LuckMaintainer, ehcache—http://ehcache.sf.netMember, JSR 107 JCACHE Expert GroupChief Architect, Wotif.com—http://wotif.com

Page 2: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 2

Goal of This Talk What You Will Gain

Learn how to:- Use ehcache to make your apps fast- Use General, Hibernate, and Web caches- Configure caches as distributed caches for horizontal scaling- Abstract the Cache Provider with Java™ Specification Request (JSR) 107 JCache- Apply ehcache to real-world problems

Page 3: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 3

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 4: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 4

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 5: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 5

Introduction● Since 2003● Probably the most widely used Java platform Cache● Apache 2.0 License● Integrated with Spring and Hibernate● Java Development Kit (JDK™) 1.4 and higher

(from ehcache 1.2.4)● Web Caching, Persistence Caching, General

Purpose and Cache Constructs● Distributed Caching implementation in 2006● JCACHE implementation released 2007

About ehcache

Page 6: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 6

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 7: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 7

How much faster will caching make an application?

Make Your Application 10–1,000 Times Faster

● It depends on a multitude of factors being:● How many times a cached piece of data can and

is reused by the application● The proportion of the response time that is

alleviated by caching● In applications that are I/O bound, which is most

business applications, most of the response time is getting data from a database; Therefore the speed up mostly depends on how much reuse a piece of data gets

Page 8: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 8

Cache Efficiency

Make Your Application 10–1,000 Times Faster

● Factors which affect the efficiency of a cache are:● Required Liveness of Data● Proportion of total data cached● Read/Write ratio● Caching in a single VM vs. Caching Clusters

Page 9: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 9

How to calculate entire system speedup: Amdahl’s Law

Make Your Application 10–1,000 Times Faster

● Amdahl’s law, after Gene Amdahl, is used to find the system speed up from a speed up in part of the system● 1/ ((1 - Proportion Sped Up) + Proportion Sped Up /

Speed up)

● Things to Watch:● For a web application the “system” should include

browser render time and network latency

Page 10: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 10

Speed up from a Web Page Cache

Make Your Application 10–1,000 Times Faster

Uncached page time: 2 secondsCache retrieval time: 2msProportion: 100%The expected server-side system speedup is thus: 1 / ((1 - 1) + 1 / 1000) = 1 / (0 + .001) = 1,000 times system speedupThe to the browser “system” speedup is much less

Page 11: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 11

Speed up from a Database Level Cache

Make Your Application 10–1,000 Times Faster

Uncached page time: 2 secondsDatabase time: 1.5 secondsCache retrieval time: 2msProportion: 75% (1.5/2)The expected system speedup is thus: 1 / ((1 - .75) + .75 / (1500/2))) = 1 / (.25 + .75/750) = 3.98 times system speedup

Page 12: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 12

Make Your Application 10–1,000 Times Faster

put/set get remove/delete0

5001000150020002500300035004000450050005500600065007000

ehcache-1.2.4 memoryehcache-1.2.4 diskmemcached-1.2.1

Speed compared with Memcached (smaller is better)

Page 13: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 13

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 14: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 14

General Purpose Caching

1.Add ehcache Java Archive (JAR) to your classpath

2.Place configuration in ehcache.xml and add it to your classpath

3.Create a CacheManagerCacheManager manager = CacheManager.create();

4.Reference a Cacheehcache sampleCache = manager.getCache();

5.Use itsampleCache.put(new Element(“key”, “value”));sampleCache.get(“key”);

Step by Step

Page 15: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 15

General Purpose Caching<ehcache>

<diskStore path="java.io.tmpdir"/>

...

<cache name="sampleCache1" maxElementsInMemory="10000" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /></ehcache>

ehcache.xml Configuration

Page 16: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 16

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 17: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 17

Step by StepWeb Caching

1.Use or Subclass SimpleCachingFilter or SimplePageFragmentCachingFilter

2.Configure filter in web.xml3.Create a mapping in web.xml4.Configure a cache entry matching the filter name

Page 18: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 18

Example ScenarioWeb Caching

/index.jsp

/include/footer.jsp

Page 19: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 19

Filter Configuration in web.xml<filter>

<filter-name>SimplePageCachingFilter</filter-name><filter-class>net.sf.ehcache.constructs.web.filter.

SimplePageCachingFilter</filter-class>

</filter>

<filter-mapping> <filter-name>SimplePageCachingFilter</filter-name> <url-pattern>/index.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>

Full Page Cache

Page 20: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 20

Add Cache to ehcache Configuration<ehcache>

<diskStore path="java.io.tmpdir"/>

...

<cache name="SimplePageCachingFilter" maxElementsInMemory="10000" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /></ehcache>

Full Page Cache

Page 21: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 21

Filter Configuration in web.xml<filter>

<filter-name>SimplePageFragmentCache</filter-name><filter-class>net.sf.ehcache.constructs.web.filter.

SimplePageFragmentCachingFilter</filter-class>

</filter>

<!-- Page Fragment Cache --><filter-mapping>

<filter-name>SimplePageFragmentCachingFilter</filter-name>

<url-pattern>/include/Footer.jsp</url-pattern> </filter-mapping>

Page Fragment Cache

Page 22: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 22

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 23: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 23

OverviewJPA/Hibernate Caching

● Hibernate can either be used directly either or via the Hibernate Java Persistence API Provider in Hibernate 3.2

● ehcache is a CacheProvider for Hibernate 2.x and 3.x

● Simple Country persistent object/EntityExample

Page 24: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 24

Step by StepJPA/Hibernate Caching

1.Configure Hibernate to use ehcache2.Configure the Country object’s cacheability; this

can be done in a number of ways3.Decide whether to use defaults, or to configure

specific cache settings for the Country object cache in ehcache.xml

Page 25: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 25

Configure Hibernate to use ehcacheJPA/Hibernate Caching

hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider

<!-- optional configuration file parameter -->net.sf.ehcache.configurationResourceName=/name_of_configuration_resource

Add the following to hibernate.properties:

Page 26: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 26

Native Hibernate Mapping File ConfigurationJPA/Hibernate Caching

<cache usage="transactional|read-write|nonstrict-read-write|read-only" region="RegionName" include="all|non-lazy" />

<hibernate-mapping>

<class name="com.somecompany.someproject.domain.Country" table="ut_Countries" dynamic-update="false" dynamic-insert="false">

<cache usage="read-write|nonstrict-read-write|read-only" /></class>...</hibernate-mapping>

Page 27: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 27

Entity Configuration With JPA and Hibernate AnnotationsJPA/Hibernate Caching

@Entity...@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)public Class Country {

private String name;

...

}

Page 28: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 28

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 29: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 29

FeaturesDistributed Caching for Clusters

1.Pluggable distribution mechanism. 2.Comes with an RMI-based replicator. JGroups

coming. Easy to add others.3.Multiple “clusters”4.Replication set per Cache. Options:

1.Replicate adds2.Replicate removes3.Replicate update by copy or invalidate4.Async or Sync

5.Bootstrapping from existing Caches in a cluster

Page 30: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 30

Distributed Caching for ClustersArchitecture

Page 31: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 31

RMI-Based Replication Distributed Caching for Clusters

1.Multicast Peer Discovery<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446/>

2.RMI Listener<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="port=40001"/>

Page 32: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 32

Set Replication Per CacheDistributed Caching for Clusters

<cache ...><cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true,

replicateUpdates=true,replicateUpdatesViaCopy=true,replicateRemovals=true,asynchronousReplicationIntervalMillis=1000"/>...

</cache>

Page 33: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 33

Set Bootstrapping Per CacheDistributed Caching for Clusters

<cache ...><bootstrapCacheLoaderFactory class="

net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>

</cache>

Page 34: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 34

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 35: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 35

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 36: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 36

Description and Status

JSR 107—JCACHE: Java Temporary Caching API

● Formed 20/3/2001● Abstracts user from the Cache Providers in much

the same way as Java DataBase Connectivity (JDBC™) does for databases

● Many think it is moribund● Too important not to be finalized● Forthcoming ehcache-1.3 Implementation can be

used with interfaces in net.sf.jsr107cache● JCache expert group has reactivated and will be

conducting meetings this week on the spec

Page 37: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 37

Basic UsageJSR 107—JCACHE

1.Add ehcache-1.3 and jsr107cache jar to your classpath

2.Place configuration in ehcache.xml and add it to your classpath

3.Specify a CacheFactory in META-INF/services/net.sf.jsr107cache.CacheFactorynet.sf.ehcache.jcache.JCacheFactory

4.Create a CacheManagerCacheManager manager = CacheManager.getInstance();

Page 38: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 38

Basic Usage (Cont.)JSR 107—JCACHE

5.Create a Cache Map env = new HashMap(); env.put("name", "test4"); env.put("maxElementsInMemory", "1000"); env.put("overflowToDisk", "true"); env.put("eternal", "true"); env.put("timeToLiveSeconds", "0"); env.put("timeToIdleSeconds", "0"); cache = manager.getCacheFactory().createCache(env);

6.Reference a CacheCache sampleCache = manager.getCache();

or manager.addCache(ehcache); Cache cache = new JCache(ehcache, null);

7.Use itsampleCache.put(“key”, “value”));sampleCache.get(“key”);

Page 39: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 39

AgendaIntroductionMake Your Application 10–1,000 Times FasterGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersStep-by-Step Coding Demo Do It With Standards—JSR 107 JCACHEMaking It Real: A Wotif.com Case Study

Page 40: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 40

Key facts and figuresWotif.com Case Study

● Australia’s No. 1 Hotel Booking Site● Rapidly Growing elsewhere● 1,000,000 users● 10,000 concurrent users● 3.6 million room nights sold per year● 9,000 hotels● Very Large Pages by design (some > 1.5MB)

Source: http://blogs.sun.com/stories/entry/wotif http://www.networksasia.net/ena/article/articleDetail.jsp?id=393040 http://info.wotif.com/pdf/Wotif.com_Facts_Figures.pdf

Page 41: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 41

Screen shots—Home PageWotif.com Case Study

Source: http://wotif.com

Page 42: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 42

Screen shots—Search ResultsWotif.com Case Study

Source: http://www.wotif.com/search/Simple?refine=simpleSearch&country=1&region=1&viewType=all

● + 15 more screenfuls for a total of 1.5MB● Support many of these per second

Page 43: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 43

Technology PlatformWotif.com Case Study

● Sun AMD64 Servers● RHEL5● 64-bit Java technology● GlassFish™ Project● Open Message Queue● Oracle and MySQL● Java Platform, Enterprise Edition (Java EE

platform) 5 with Enterprise JavaBeans™ (EJB™) 3, JavaServer Pages™ (JSP™) 2

● Also lots of open source

Page 44: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 44

ehcacheWotif.com Case Study

● 150 caches● Hibernate 2 and Hibernate 3 Persistent Object

and Collection Caches● Distributed Page Caches in a “Customer” cluster● AJAX XML Response Caches● Distributed Java Object Caches in a “Supplier”

cluster● Uses SelfPopulating and Blocking Cache● Web Cache Full Page and Fragment Web Filters● Disk Store and Persistent Disk Stores

Page 45: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 45

How we came to implement a CacheWotif.com Case Study● Started with Apache JCS. It had some serious threading

bugs 3 years ago. Submitted a patch which was ignored by the maintainer.

● Mentioned on the Hibernate mailing list about JCS problems and linked the patch for people to apply.

● Gavin King suggested forking JCS. On the basis that Hibernate would use the forked version ehcache was born. It originally stood for Easy Hibernate Cache.

● ehcache’s evolution mirrors Wotif.com’s own need for caching features with the progression of MemoryStore -> DiskStore -> SelfPopulatingCache -> Web Response Caching -> Persistent Disk Store -> Distributed Caching.

Page 46: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 46

DEMO1. Web App on GlassFish Project without caching2. Add caching to a single instance3. Use distributed caching with n instances

Page 47: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 47

Summary

● Most apps will benefit from caching● You can calculate the speed up● ehcache is easy to use directly● You can also benefit from ehcache indirectly

through frameworks that use it● Distributed ehcache is a small configuration step● Abstract yourself from Caching Providers

using JCACHE

Page 48: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 48

For More Information

● Project Website:http://ehcache.sf.net

● Downloadable Book: http://ehcache.sourceforge.net/EhcacheUserGuide.pdf

● My Email:[email protected]

Page 49: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 | 49

Q&AGreg [email protected]

Page 50: Distributed Caching, Using the JCACHE API and ehcache

2007 JavaOneSM Conference | Session TS-6175 |

TS-6175

Distributed Caching Using the JCACHE API and ehcache, Including a Case Study on Wotif.comGreg LuckMaintainer, ehcache—http://ehcache.sf.netMember, JSR 107 JCACHE Expert GroupChief Architect, Wotif.com—http://wotif.com