Scaling Hibernate with Terracotta

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Scaling Hibernate with Terracotta - Presentation Transcript

    1. Scaling Hibernate with Terracotta Alex Miller (@puredanger)
    2. Architecture and Scale
    3. Applications in Three Tiers Client Server Database
    4. How do we scale? Client Client Client Server Server Server Database
    5. But what about failover?
    6. Option 1: Servers share state Client Client Client Server Server Server Database
    7. How? • RMI • JMS • Custom (JGroups, etc)
    8. Option 2: Database (“stateless”) Client Client Client Server Server Server Database
    9. Stateless == “State in the database” • Database load • Serialization to and from the database • Network bandwidth • Object / relational mapping due to impedance mismatch
    10. Option 3: Terracotta! Client Client Client Server Server Server Terracotta Terracotta Terracotta Terracotta Terracotta Database Server Server Instance Instance
    11. Why is this better? • Hub and spoke vs peer to peer • Avoid object / relational translation • Avoid serialization • Reduce database overload • Programming model you already know • Focus on your app, not scalability and high availability
    12. What state should go in Terracotta? Appropriateness Memory Terracotta Database Data Lifetime
    13. How does Terracotta work?
    14. Core Concepts • Roots • Instrumented Classes • Locks • Integration Modules
    15. Clustered Heap App App app = new App(); app.run();
    16. Clustered Heap App EmployeeManager employeeMgr = new EmployeeManager(); Employee Manager Object lock Object lock = new Object(); Map<String,Employee> = new HashMap<String,Employee>(); HashMap employees
    17. Clustered Heap App Employee " U9 9 4 1 1 " M anag er Ob ject lock Employee.AC T IV E = = 1 Employee " Gandalf" HashM ap employees Employee employee = new Employee( “U99411”, “Gandalf”, Employee.ACTIVE);
    18. Clustered Heap App employeeMgr.addEmployee(employee); Employee "U99411" Manager Object lock Employee.ACT IVE == 1 "U99411" Employee "Gandalf" HashMap employees synchronized(lock) { employees.put(e.getId(), e); }
    19. Clustered Heap App Employee "U99411" Manager Object lock Employee.ACT IVE == 1 "U99411" Employee "Gandalf" HashMap employees "U23526" Employee "U23526" Employee.ACT IVE = 1 "U99411"
    20. Configuration • Roots • app.App.employeeMgr - a root of the clustered heap • Instrumented classes • app.* - instrument app classes at load time • Locks • employeeMgr.addEmployee() -> cluster synchronized blocks
    21. Integration Modules • Pre-built configuration • Integration with specific 3rd-party libraries • Hibernate • Spring • Ehcache • Quartz • Lucene • many more...
    22. Scaling Hibernate • Hibernate Second Level Cache • Clustered detached entities • Close session, detach entities • Cluster those entities in Terracotta • Merge with new session later
    23. 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
    24. 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
    25. 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
    26. 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
    27. 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>
    28. 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
    29. Tooling Demo
    30. 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
    31. How else is Terracotta used? • Distributed cache • Clustered HTTP sessions • Batch processing • Grid • Messaging and events
    32. Who Uses It? • e-Commerce • Online gaming • Financial services • Travel & leisure • Social networking
    33. 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 • Alex Miller • @puredanger • http://tech.puredanger.com

    + Alex MillerAlex Miller, 3 months ago

    custom

    1122 views, 3 favs, 1 embeds more stats

    Terracotta (an open source technology) provides a c more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1122
      • 1110 on SlideShare
      • 12 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 26
    Most viewed embeds
    • 12 views on http://tech.puredanger.com

    more

    All embeds
    • 12 views on http://tech.puredanger.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories