Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Distributed Caches Shootout

Distributed Caches Shootout

Distributed caches have evolved into an independent branch of Big Data solutions:
When it comes to fast read and write access, distributed caches are the solution of choice. JSR 107 is an attempt to standardize an API for accessing caches, and the use of caches in transactional contexts.

The talk will introduce three cache implementations:

* Ehcache / Terracotta
* Hazelcast
* Infinispan

Based on a simple example application, the basic functionality is presented, and the specific strengths and weaknesses of the different cache architectures are highlighted.

---

JayDay 2013, http://www.jayday.de/

Fabian Stäber

July 01, 2013
Tweet

More Decks by Fabian Stäber

Other Decks in Programming

Transcript

  1. 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*
  2. 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
  3. 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)
  4. ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table

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

    of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up synchronized
  6. 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
  7. 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
  8. 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
  9. ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table

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

    Application Generate an event for a user. Search events for a user.
  11. 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
  12. ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table

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

    Example Application mvn tomcat7:run-war -pl part01 -am verify
  14. 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 ); }
  15. 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:
  16. 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
  17. 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)
  18. 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
  19. 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
  20. 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.
  21. 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
  22. 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
  23. 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.
  24. 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
  25. 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
  26. 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
  27. 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
  28. ConSol* Consulting & Solutions Software GmbH 1 July 2013 Table

    of Contents • Example Application • ConcurrentMap • Local Cache • Peer-to-Peer • Client-Server • Wrap-Up
  29. 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
  30. 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:
  31. ConSol* Consulting & Solutions Software GmbH 1 July 2013 Wrap-Up

    Example Code: http://github.com/ConSol Text: http://labs.consol.de/java-caches