State Replication Template
Overview
State Replication is the simpler of Talon's two High Availability models. With State Replication, Talon automatically replicates changes to your microservice's state and the outbound messages emitted by your message handlers. In the event of failover to a backup or cold start recovery from a transaction log, your microservice's state is available at the same point where processing left off, and the engine will retransmit any outbound messages that were left in doubt as a result of the failure.
How State Replication Works
With State Replication:
State is modeled using ADM - Define your state using the Application Data Model XML
State changes are automatically replicated - The Talon runtime tracks and replicates all state modifications
Transparent state management - State is managed by the runtime as a tree of POJOs
Atomic transactions - State changes and outbound messages form atomic units of work
Automatic failover - Backup instances maintain synchronized state for seamless takeover
Key Features
Automatic Replication: Runtime handles state synchronization across cluster members
Transparent State Tree: State organized as an object tree, automatically tracked by runtime
ADM-Based Modeling: State defined using same XML modeling language as messages
Built-in Consistency: Guaranteed state consistency across all instances
Transaction Integration: State changes and message sends are atomic
Disk Persistence: Transaction log enables cold start recovery
When to Use State Replication
State Replication is ideal when:
You want automatic state synchronization with minimal code
State can be modeled using ADM primitives and collections
You don't need complex custom state logic or inheritance
Simplicity and ease of development are priorities
You need guaranteed state consistency across instances
Comparison with Event Sourcing
State Management
Runtime manages (transparent)
Application manages (opaque POJOs)
State Modeling
ADM XML required
No modeling required
Replication
State deltas replicated
Inbound messages replicated
Latency
Slightly higher (state tracking overhead)
Lower (no state tracking)
Complexity
Simpler application code
More complex (determinism requirements)
Recovery
State reconstructed from log
State reconstructed by message replay
See Consensus Models for detailed comparison.
Building a State Replication Microservice
Creating a State Replication microservice involves five main steps:
Model your microservice state using ADM
Annotate your main class for State Replication
Provide a state factory
Write message handlers that operate on state
Configure storage (clustering and persistence)
Step 1: Model Application State
Define your microservice's state using the ADM modeling language in your model.xml file. The state model defines entities that form a tree structure with a single root object.
Example State Model:
Important Modeling Considerations:
The state tree must have a single root entity
Entities can contain simple fields, entity fields, and collections
See State Tree Limitations for restrictions
See The Modeling Language for complete modeling guide
Step 2: Annotate Main Class for State Replication
Use the @AppHAPolicy annotation to declare that your microservice uses State Replication:
Step 3: Provide a State Factory
The AEP engine needs to be able to create your application's initial state. Provide a state factory method annotated with @AppStateFactoryAccessor:
Important: The state factory should return an empty, uninitialized object. The platform will:
Invoke the factory during initialization with a
nullargument to determine the state root typeSubsequently invoke it on receipt of a MessageView when state hasn't been created
Manage the state tree after creation - don't store references to the state yourself
Step 4: Write Message Handlers
Message handlers receive both the inbound message and the state root object. The handler updates state and sends outbound messages as an atomic transaction:
Key Points:
State changes and message sends form an atomic transaction
All modifications to state are automatically tracked and replicated
No need for manual state serialization or replication code
State root is passed to every handler method
Step 5: Configure Storage
Configure storage in your DDL to enable clustering and persistence.
Register Object Factories
Both message factories and state factories must be registered with the runtime:
DDL Configuration:
Programmatic Registration Alternative:
Enable Clustering
Clustering allows multiple instances to discover each other and form an HA cluster:
Key clustering concepts:
Instances with same
storeNameform a clusterOne instance elected as primary via leadership election
Primary establishes messaging and invokes handlers
Backups maintain synchronized state for failover
Enable Persistence
Persistence logs the replication stream to disk for cold start recovery:
See Storage Configuration for complete configuration reference.
State Tree Limitations
State Replication has several important restrictions that developers must understand. These are documented in detail in Programming Fundamentals - State Tree Limitations:
Key Restrictions
Single Parent Restriction: An entity can only appear in one location in the state tree (throws
IllegalStateExceptionif violated)No Multiple Fields of Same Type: Cannot have multiple non-embedded entity fields of the same type in a parent
No State Tree Cycles: Cycles (including self-references) are not supported
No Primitive Collections: Collections must use boxed types (e.g.,
Integernotint)No Inheritance: Inheritance not supported; use entity inlining for polymorphism
Workarounds
For Single Parent Restriction: Store entities in a Map and reference by ID:
See Programming Fundamentals for complete details and additional workarounds.
Related Documentation
Core Concepts
Consensus Models - Understanding State Replication vs Event Sourcing
Transactions - How transactions work with state replication
Application Lifecycle - State creation and initialization
Development
Modeling Messages & State - Complete ADM modeling guide
Programming Fundamentals - State tree limitations and restrictions
Handling Messages - Writing message handlers
Configuration
Storage Configuration - Complete storage configuration reference
Configuring Threading - Optimize replication performance
Operations
Transaction Log Tool - Browse and analyze transaction logs
Querying Transaction Logs - XPQL query language
Next Steps
Start modeling: Define your state in
model.xmlfollowing ADM syntaxCreate application class: Annotate with
@AppHAPolicyand provide state factoryWrite handlers: Implement message handlers that operate on state
Configure storage: Enable clustering and persistence in DDL
Test failover: Verify state consistency and message retransmission
Tune performance: Adjust threading and batching based on load testing
Monitor in production: Track transaction log size and replication latency
Last updated

