You are previewing The Definitive Guide to Terracotta: Cluster the JVM™ for Spring, Hibernate, and POJO Scalability.
O'Reilly logo
The Definitive Guide to Terracotta: Cluster the JVM™ for Spring, Hibernate, and POJO Scalability

Book Description

Get the definitive guide on all the fundamentals of Terracotta as well as user secrets, recipes, and prepackaged frameworks.

Written by Terracotta CTO Ari Zilka and his team, The Definitive Guide to Terracotta: Cluster the JVM for Spring, Hibernate and POJO Scalability covers the following:

  • High Availability (HA) nth degree scaling and clustering for traditional J2EE and Java EE 5 applications (using Seam or other application) as well as Spring-based enterprise applications

  • Everyday Terracotta using its prepackaged frameworks and integration recipes, including configuration and customization for your application tuning, no matter the scale

  • Power user secrets available, including config modules, customized advanced performance tuning, SDLC, Maven, and more

Table of Contents

  1. Copyright
  2. About Terracotta, Inc.
  3. About the Authors
  4. About the Technical Reviewer
  5. Introduction
    1. How This Book Is Structured
    2. Prerequisites
    3. Contacting the Authors
  6. 1. Theory and Foundation Forming a Common Understanding
    1. 1.1. Definition of the Terracotta Framework
      1. 1.1.1. Network Attached Storage Similarities to Terracotta
        1. 1.1.1.1. A Layer of Abstraction
        2. 1.1.1.2. Adding Networked File Storage
        3. 1.1.1.3. Parallels to Terracotta
      2. 1.1.2. Transparency of Memory Location
      3. 1.1.3. Putting Transparency and Clustering Together
    2. 1.2. Honoring the Java Memory Model
    3. 1.3. Being a Service Has Advantages
      1. 1.3.1. Availability
      2. 1.3.2. Scalability
      3. 1.3.3. Avoiding Bottlenecks
    4. 1.4. Use Cases
      1. 1.4.1. Distributed Caching
      2. 1.4.2. Database Offload
      3. 1.4.3. Session Replication
      4. 1.4.4. Workload Partitioning
    5. 1.5. Summary
  7. 2. History of Terracotta
    1. 2.1. Approaches to Scaling
      1. 2.1.1. Scale the Database
      2. 2.1.2. In-Memory Replication
      3. 2.1.3. All Paths Lead to Partitioning
    2. 2.2. Terracotta's Origin in a Web Monster
      1. 2.2.1. The Application Is a Giant Computer
    3. 2.3. Terracotta Makes L2 General Purpose
      1. 2.3.1. Original L2 Was Intrusive
      2. 2.3.2. We Needed More Transparency
      3. 2.3.3. Transparency Equals General Purpose
      4. 2.3.4. Transparent Scalability with Availability
      5. 2.3.5. Transparent Clustering Service Revisited
      6. 2.3.6. Transparency Delivers Higher Scalability
    4. 2.4. Clustered Application Servers and Clustered Caches
    5. 2.5. Summary
  8. 3. Jumping Into Terracotta
    1. 3.1. Hello Clustered World
      1. 3.1.1. Clustering HelloClusteredWorld
      2. 3.1.2. Why HelloClusteredWorld Works
      3. 3.1.3. Alternatives to Terracotta
    2. 3.2. A Tour Through Terracotta
      1. 3.2.1. Downloading and Installing Terracotta
      2. 3.2.2. Terracotta Installation Layout
      3. 3.2.3. Terracotta Scripts
      4. 3.2.4. Step-by-Step Hello Clustered World in Unix
        1. 3.2.4.1. Creating a Project Skeleton
        2. 3.2.4.2. Creating HelloClusteredWorld.java
        3. 3.2.4.3. Creating the Terracotta Configuration
        4. 3.2.4.4. Making the Terracotta Server Run in Persistent Mode
        5. 3.2.4.5. Configuring the Terracotta Client
        6. 3.2.4.6. Configuring Roots
        7. 3.2.4.7. Configuring Instrumentation
        8. 3.2.4.8. Configuring Locks
        9. 3.2.4.9. Reviewing the Configuration
        10. 3.2.4.10. Starting the Terracotta Server
        11. 3.2.4.11. Starting Two Clustered Instances of HelloClusteredWorld
        12. 3.2.4.12. Viewing Runtime Data in the Terracotta Administration Console
        13. 3.2.4.13. High Availability
        14. 3.2.4.14. Heap Durability
      5. 3.2.5. Step-by-Step Hello Clustered World in Eclipse
    3. 3.3. Implications for Every Developer
    4. 3.4. Summary
  9. 4. POJO Clustering
    1. 4.1. All About Objects and Roots
    2. 4.2. Clustered Objects
    3. 4.3. Virtual Heap and Memory Management
    4. 4.4. Distributed Garbage Collection
    5. 4.5. Managing Object Changes and Coordinating Threads
    6. 4.6. Locks
      1. 4.6.1. Autolocks and Named Locks
      2. 4.6.2. Lock Level
      3. 4.6.3. Distributed wait() and notify()
    7. 4.7. Transactions
    8. 4.8. Distributed Method Invocation
    9. 4.9. Transparency and Bytecode Instrumentation
    10. 4.10. Portability
    11. 4.11. Boot JAR Classes
    12. 4.12. Physically vs. Logically Managed Objects
    13. 4.13. Nonportable and Logically Managed Classes
    14. 4.14. Portability Contexts
      1. 4.14.1. Field Change
      2. 4.14.2. Logical Action
      3. 4.14.3. Object Graph Traversal
    15. 4.15. Transient Fields and On-Load Behavior
      1. 4.15.1. Initializing Transient Fields on Load
    16. 4.16. Clustered POJOs Step by Step
      1. 4.16.1. What Do We Mean by POJO?
      2. 4.16.2. What's a Clustered POJO?
      3. 4.16.3. Why Clustered POJOs?
    17. 4.17. An Example of Clustered POJOs
      1. 4.17.1. The Example Classes
        1. 4.17.1.1. The ProductImpl Class
        2. 4.17.1.2. The Catalog Class
        3. 4.17.1.3. The ShoppingCartImpl Class
        4. 4.17.1.4. The ActiveShoppingCarts Class
        5. 4.17.1.5. The Roots Class
        6. 4.17.1.6. The Main Class
      2. 4.17.2. Clustering Requirements for the Example
      3. 4.17.3. Configuration for the Example
      4. 4.17.4. Running the Example
      5. 4.17.5. What Just Happened
      6. 4.17.6. What the Example Shows
    18. 4.18. Summary
  10. 5. Caching
    1. 5.1. The Pain of Caching
      1. 5.1.1. The Pain of Large Data Sets
      2. 5.1.2. The Pain of Staleness
      3. 5.1.3. The Pain of Duplication
    2. 5.2. How Terracotta Can Help
    3. 5.3. A Simple Example of Caching
    4. 5.4. Distributed Caching
    5. 5.5. Transparent Distributed Caching
    6. 5.6. Caching with Maps
      1. 5.6.1. Unsorted Maps
      2. 5.6.2. Sorted Maps
      3. 5.6.3. Autolocked Collections
        1. 5.6.3.1. Downloading and Installing the Autolocked Collections TIM
        2. 5.6.3.2. Using Autolocked Collections
      4. 5.6.4. Bulk Loading a Map
      5. 5.6.5. Locking and Maps
      6. 5.6.6. Maps of Maps
    7. 5.7. A Deeper Look at Caching
      1. 5.7.1. Eviction and Expiration
      2. 5.7.2. Persistence
      3. 5.7.3. Distributed Caching
      4. 5.7.4. Partitioned Data
      5. 5.7.5. Leveraging Terracotta
    8. 5.8. Caching with Ehcache
      1. 5.8.1. Rewriting the Cache Example
      2. 5.8.2. Using Ehcache with Terracotta
        1. 5.8.2.1. Enhancements and Configuration
        2. 5.8.2.2. Downloading and Installing Ehcache Integration
        3. 5.8.2.3. Using Ehcache with Terracotta
    9. 5.9. Summary
  11. 6. Hibernate with Terracotta Using Terracotta to Offload the Database
    1. 6.1. Object-Relational Mapping
    2. 6.2. How Does Hibernate Work?
    3. 6.3. Typical Hibernate Invocation
    4. 6.4. Using Terracotta to Improve Hibernate
    5. 6.5. How Terracotta Improves Hibernate Performance
      1. 6.5.1. Basic Steps for Improving Performance of a Hibernate Application
      2. 6.5.2. Baseline Performance
      3. 6.5.3. Add Hibernate to Save Three DB Calls
      4. 6.5.4. Enable the Second-Level Cache to Save Five SQL Calls
      5. 6.5.5. Clustering the Session Objects
      6. 6.5.6. Benchmarking the Enhancements
    6. 6.6. Configuring Hibernate to Work with Terracotta
      1. 6.6.1. Configuring a Second-Level Cache
        1. 6.6.1.1. Configuring Ehcache
        2. 6.6.1.2. Enabling Ehcache Second-Level Caching in Hibernate
        3. 6.6.1.3. Configuring Terracotta
      2. 6.6.2. Configuring Terracotta for Use with Detached Instances
        1. 6.6.2.1. Refactoring Your Application
        2. 6.6.2.2. Configuring Terracotta to Use the Hibernate Integration Module
        3. 6.6.2.3. Storing the Detached Instances
          1. 6.6.2.3.1. Store the Detached Instances in the HTTP Session Context
          2. 6.6.2.3.2. Store the Detached Objects in a HashMap
          3. 6.6.2.3.3. Store the Detached Objects in a Clustered Cache
        4. 6.6.2.4. Reattaching the Detached Instances
        5. 6.6.2.5. Suggestions for Further Reading
    7. 6.7. Debugging, Testing, and Tuning Concerns
      1. 6.7.1. Verifying Ehcache Configuration Clustering
      2. 6.7.2. Tuning the Second-Level Cache
      3. 6.7.3. Tuning Detached Instances
    8. 6.8. Summary
  12. 7. Extending HTTP Sessions with Terracotta
    1. 7.1. What Are HTTP Sessions?
      1. 7.1.1. Server-Side State Management
      2. 7.1.2. Automatic Time-Out of Stale Data
      3. 7.1.3. Manual Invalidation to Clear Out Data
      4. 7.1.4. Events to Coordinate State Modification
      5. 7.1.5. Session State Persistence
    2. 7.2. Distributed HTTP Sessions
      1. 7.2.1. Motivation for Using HTTP Sessions
        1. 7.2.1.1. Accessing Sessions State from Other Nodes
        2. 7.2.1.2. Dynamic Horizontal Scale-Out to Handle Traffic Spikes
        3. 7.2.1.3. High Availability in Case of Server Failure
      2. 7.2.2. Alternatives to HTTP Sessions
        1. 7.2.2.1. Distributed Caches
        2. 7.2.2.2. A Database As a Central Store
    3. 7.3. Distributed HTTP Sessions with Terracotta
      1. 7.3.1. How Terracotta Sessions Work
        1. 7.3.1.1. Transparently Plugs into Web Containers
        2. 7.3.1.2. Appropriate Lock Granularity and Isolation
      2. 7.3.2. Benefits
        1. 7.3.2.1. No Serialization
        2. 7.3.2.2. Rolling Upgrades
        3. 7.3.2.3. Fine-Grained Changes
        4. 7.3.2.4. Simultaneous Linear Scalability and High Availability
        5. 7.3.2.5. No Need to Set Attributes After Each Change
        6. 7.3.2.6. Easy to Set Up
    4. 7.4. Using Terracotta and HTTP Sessions
      1. 7.4.1. Shared Session State
        1. 7.4.1.1. Package and Configure the Web Application
        2. 7.4.1.2. Install and Run the Application with the Terracotta Sessions Configurator
        3. 7.4.1.3. Trying the Example
      2. 7.4.2. HTTP Sessions with Structured Session Attributes
        1. 7.4.2.1. Packaging and Configuring the Web Application
        2. 7.4.2.2. Manually Configuring and Installing the Example into Tomcat
        3. 7.4.2.3. Verifying Fine-Grain Change Detection
    5. 7.5. Understanding HTTP Sessions with Terracotta
      1. 7.5.1. Supported Platforms
      2. 7.5.2. Architecture Considerations
        1. 7.5.2.1. The Effects of a Round-Robin Load Balancer
        2. 7.5.2.2. Using a Sticky Load Balancer
        3. 7.5.2.3. Running a Terracotta Cluster
    6. 7.6. Summary
  13. 8. Clustering Spring
    1. 8.1. A Sample: Spring Beans Without a Database
    2. 8.2. Running the Example on a Single JVM
    3. 8.3. Running the Example Clustered with Terracotta
    4. 8.4. A Note on DI and Terracotta
    5. 8.5. Growing Spring Beans
    6. 8.6. Clustering Spring Beans
    7. 8.7. Running with Clustered Spring Beans
    8. 8.8. Terracotta Session Clustering for Spring
    9. 8.9. Summary
  14. 9. Integration Modules
    1. 9.1. Clustering by Configuration
    2. 9.2. Using TIMs
      1. 9.2.1. Clustering a Synchronized Map
      2. 9.2.2. Importing a TIM
      3. 9.2.3. Module Repositories
      4. 9.2.4. The Terracotta Forge
    3. 9.3. Creating a TIM
      1. 9.3.1. TIM Anatomy
        1. 9.3.1.1. Creating a Manifest
        2. 9.3.1.2. Terracotta Configuration
      2. 9.3.2. Creating a Simple TIM
      3. 9.3.3. Creating TIMs with Maven
        1. 9.3.3.1. Module Versioning
      4. 9.3.4. Including Code in a TIM
        1. 9.3.4.1. Defining a Bundle Activator
        2. 9.3.4.2. Configuration with Code
        3. 9.3.4.3. Class Replacement and Modification
        4. 9.3.4.4. Using Maven to Create TIMs with Code
    4. 9.4. Summary
  15. 10. Thread Coordination
    1. 10.1. Terracotta and Thread Coordination
    2. 10.2. Clustering Threads
      1. 10.2.1. Thread Coordination Using Concurrency Primitives
      2. 10.2.2. Thread Coordination Using the java.util.concurrent Package
      3. 10.2.3. Thread Coordination and JMX Cluster Events
      4. 10.2.4. Thread Coordination with Queues
    3. 10.3. Summary
  16. 11. Grid Computing Using Terracotta
    1. 11.1. What Are Grids?
      1. 11.1.1. Compute Grids vs. Data Grids
      2. 11.1.2. How Grids Handle Scalability
      3. 11.1.3. How Grids Handle Failover and High Availability
      4. 11.1.4. Use Cases
    2. 11.2. Introducing the Master/Worker Pattern
    3. 11.3. Master/Worker in Java
      1. 11.3.1. Using the Java Language's Thread Coordination Primitives
      2. 11.3.2. Using the java.util.concurrent Abstractions
      3. 11.3.3. Using the CommonJ Work Manager Specification
    4. 11.4. Getting Started: Naïve Single Work Queue Implementation
      1. 11.4.1. Implementing a Single-JVM Naïve Master/Worker Container
        1. 11.4.1.1. What About My Work?
      2. 11.4.2. Clustering Our Master/Worker with Terracotta
    5. 11.5. Handling Real-World Challenges
    6. 11.6. Refactoring for Large Workloads, Routing, and Work Failover
      1. 11.6.1. Minimizing Contention and Maximizing Locality of Reference
      2. 11.6.2. Routing Strategies
      3. 11.6.3. Dealing with Work Failure and Recovery
      4. 11.6.4. Refactoring the Worker
      5. 11.6.5. Update the Terracotta Configuration
      6. 11.6.6. Using the Master/Worker System
      7. 11.6.7. Running the Master/Worker System
    7. 11.7. Refactoring Work Batching, Worker Failover, and Dynamic Worker Management
      1. 11.7.1. Work Batching
      2. 11.7.2. Dynamic Worker Management
      3. 11.7.3. Worker Failover
      4. 11.7.4. How to Use and Run the Fault-Tolerant Master/Worker System
    8. 11.8. Building a Distributed Web Spider
    9. 11.9. When Not to Use the Master/Worker Pattern
    10. 11.10. Summary
  17. 12. Visualizing Applications
    1. 12.1. Observing an Application
    2. 12.2. Visualization Tools and Architecture
    3. 12.3. The Sample Inventory Application
      1. 12.3.1. Modifying to Generate Load
        1. 12.3.1.1. Copying the Sample
        2. 12.3.1.2. Pounder Methods
        3. 12.3.1.3. Tuning the Locks
        4. 12.3.1.4. Using Separate Roots
        5. 12.3.1.5. Updating the CLI
        6. 12.3.1.6. Updating the Terracotta Configuration
        7. 12.3.1.7. Compile and Run the Application
      2. 12.3.2. Taking Performance Snapshots
    4. 12.4. Potential Bottlenecks
      1. 12.4.1. Recalling the Motherboard
      2. 12.4.2. Inside the Terracotta Server
        1. 12.4.2.1. SEDA
        2. 12.4.2.2. Terracotta's SEDA Pipeline
      3. 12.4.3. Looking for Locality Issues
      4. 12.4.4. Specific Bottlenecks in inventory.pounder
        1. 12.4.4.1. Broadcasting Changes
        2. 12.4.4.2. Lock Acquisition
    5. 12.5. Visualizing the Bottlenecks
    6. 12.6. Managing Production Environments
    7. 12.7. Summary