Slide 1

Slide 1 text

Fabian Stäber JayDay 2013 Distributed Java Caches Ehcache, Hazelcast, Infinispan

Slide 2

Slide 2 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 • Headquarters in Munich • Since 1984 • More than 200 employees • In-Memory DMS Consulting Team (mainly Terracotta) About ConSol*

Slide 3

Slide 3 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Why Are Caches Interesting? less interesting more interesting Local caches in hardware and software Cluster-Aware In-Memory Data Management Systems Cloud

Slide 4

Slide 4 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Java Cache Implementations Name: Hazelcast Company: Hazelcast, Inc. License: Free (Apache) / Commercial Name: Ehcache / Terracotta Company: Software AG License: Free (Apache) / Commercial Name: Infinispan Company: Red Hat, Inc. License: Free (LGPL)

Slide 5

Slide 5 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up

Slide 6

Slide 6 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up synchronized

Slide 7

Slide 7 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up Browser Browser Browser Local Cache App-Server

Slide 8

Slide 8 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up Browser Browser Browser Local Cache App-Server Local Cache App-Server Local Cache App-Server

Slide 9

Slide 9 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up Cache Server Browser Browser Browser App-Server App-Server Cache Server App-Server Cache Server

Slide 10

Slide 10 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up

Slide 11

Slide 11 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Example Application Generate an event for a user. Search events for a user.

Slide 12

Slide 12 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Example Application 12 User Events fabian 2013-05-05 18:26:31 „log in“ 2013-05-05 18:27:03 „change password“ 2013-05-05 18:27:43 „log out“ tobias 2013-05-05 18:26:31 „log in“ 2013-05-05 18:26:33 „fill in credit card data“ 2013-05-05 18:16:34 „log out“ jan 2013-05-05 13:26:30 „admin log in“ 2013-05-05 13:26:43 „disable account fabian“ Event Cache REST Servlet put get Browser Browser Browser Server Client POST GET

Slide 13

Slide 13 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up synchronized

Slide 14

Slide 14 text

DEMO ConSol* Consulting & Solutions Software GmbH 1 July 2013 Example Application mvn tomcat7:run-war -pl part01 -am verify

Slide 15

Slide 15 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Example Application ConcurrentMap: Thread-Safe without synchronized public void appendEvent(String user, String msg) { boolean success; do { map.putIfAbsent(user, UserEventList.emptyList()); UserEventList oldMsgList = map.get(user); UserEventList newMsgList = UserEventList.append(oldMsgList, msg); success = map.replace(user, oldMsgList, newMsgList); } while ( ! success ); }

Slide 16

Slide 16 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Example Application Objects that are stored in a cache should have the following properties: • Unmodifiable: Prevents accidental modification of a local copy in a distributed cache environment. • equals(Object) and hashCode(): Required by ConcurrentHashMap. • Serializable: Required when objects are transferred over the network in clustered installations, or when objects are written to off-heap memory. UserEventList list = UserEventList.emptyList(); UserEventList otherList = UserEventList.append(list, "A new event."); Usage of the UserEventList:

Slide 17

Slide 17 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up Browser Browser Browser Local Cache App-Server

Slide 18

Slide 18 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Local Cache: JSR 107 Cache cache = cacheManager.getCache(„Greg‘s Cache“); • Functionality similar to ConcurrentMap: atomic operations without synchronized blocks. • Backed by CacheLoader and CacheWriter • Manages Cache‘s Lifecycle. Optional: • Transaction Support (JTA)

Slide 19

Slide 19 text

DEMO ConSol* Consulting & Solutions Software GmbH 1 July 2013 Example Application

Slide 20

Slide 20 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Code • All implementations similar to ConcurrentMap. Performance: • Network performance not relevant for local-only deployments. • Performance heavily influenced by speed of Serialization. • Ehcache and Infinispan allow storing objects „by reference“ without serialization in local deployments. Local Cache - Results

Slide 21

Slide 21 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up Browser Browser Browser Local Cache App-Server Local Cache App-Server Local Cache App-Server

Slide 22

Slide 22 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Peer-to-Peer Cache Cache Cache Cache Cache Full Replication • Reads served from local node • Writes broadcast to each node • Expensive to implement atomic operations Cache Cache Cache Key 0x00 Key 0xFF Distributed Hash Tables • Read often served from remote node • Writes sent to defined set of nodes • Inexpensive to implement atomic operations • Different strategies with joining and leaving nodes.

Slide 23

Slide 23 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Peer-to-Peer Full Replication Distributed Hash Table Ehcache X Hazelcast X Infinispan X X How Do the Peers Find Each Other? • JGroups: Infinispan, Ehcache • UDP Multicast: Included in JGroups, standalone in Hazelcast • RMI: Ehcache • JMS: Ehcache, Infinispan Near Cache Invalidation

Slide 24

Slide 24 text

DEMO ConSol* Consulting & Solutions Software GmbH 1 July 2013 Peer-to-Peer mvn tomcat7:run-war -pl part03.infinispan -am verify \ -Dmaven.tomcat.port=8080 -Dinfinispan.config.filename=infinispan1.xml mvn tomcat7:run-war -pl part03.infinispan -am verify \ -Dmaven.tomcat.port=8080 -Dinfinispan.config.filename=infinispan1.xml mvn tomcat7:run-war -pl part03.ehcache -am verify \ -Dmaven.tomcat.port=8080 -Dehcache.config.filename=ehcache1.xml mvn tomcat7:run-war -pl part03.ehcache -am verify \ -Dmaven.tomcat.port=9090 -Dehcache.config.filename=ehcache2.xml

Slide 25

Slide 25 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Peer-to-Peer: Results Atomic Operations No Atomic Operations Ehcache X (explicit exception) Hazelcast X Infinispan X X (silently inconsistent) Applications with no requirements for atomic operations: • Scenario 1: Each key is only written once (WORM applications) • Scenario 2: Each key is only updated by a single Tomcat instance, e.g. Session data when session-aware load balancer is used.

Slide 26

Slide 26 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up Cache Server Browser Browser Browser App-Server App-Server Cache Server App-Server Cache Server

Slide 27

Slide 27 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Client-Server: Ehcache App- Server Terracotta Server Array Clients Stand-By Stripe Stand-By Stand-By Master Stand-By Stripe Stand-By Stand-By Master Stand-By Stripe Stand-By Stand-By Master App- Server

Slide 28

Slide 28 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Client-Server: Hazelcast Peer Hazelcast DHT in Server Mode Hazelcast Client-Only Mode App- Server App- Server Peer Peer Peer Peer Peer

Slide 29

Slide 29 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Client-Server: Infinispan Infinispan Node Hotrod Server App- Server Infinispan Hotrod Client App- Server Infinispan Hotrod Client Infinispan Node Hotrod Server Infinispan Node Hotrod Server

Slide 30

Slide 30 text

DEMO ConSol* Consulting & Solutions Software GmbH 1 July 2013 Client-Server

Slide 31

Slide 31 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up

Slide 32

Slide 32 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 • There is no simple answer to the question of which Java Cache implementation is the best. • Caches have a similar interface (ConcurrentMap), but very different underlying architectures. Wrap-Up

Slide 33

Slide 33 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 • Ehcache – Simplest solution for local or full-replication scenarios. • Hazelcast – Very easy to configure. Recommended for distributed hash table scenarios. • Infinispan – Most complex confugration, but most powerful. – No restrictions in free edition. Very Incomplete and Subjective Rating:

Slide 34

Slide 34 text

ConSol* Consulting & Solutions Software GmbH 1 July 2013 Wrap-Up Example Code: http://github.com/ConSol Text: http://labs.consol.de/java-caches