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

Aspect
State Replication
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:

  1. Model your microservice state using ADM

  2. Annotate your main class for State Replication

  3. Provide a state factory

  4. Write message handlers that operate on state

  5. 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:

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:

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 storeName form a cluster

  • One 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:

Clustering Requires Persistence: When clustering is enabled, persistence must also be enabled. The transaction log is used to initialize new cluster members that connect to the primary.

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

  1. Single Parent Restriction: An entity can only appear in one location in the state tree (throws IllegalStateException if violated)

  2. No Multiple Fields of Same Type: Cannot have multiple non-embedded entity fields of the same type in a parent

  3. No State Tree Cycles: Cycles (including self-references) are not supported

  4. No Primitive Collections: Collections must use boxed types (e.g., Integer not int)

  5. 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.


Core Concepts

Development

Configuration

Operations


Next Steps

  1. Start modeling: Define your state in model.xml following ADM syntax

  2. Create application class: Annotate with @AppHAPolicy and provide state factory

  3. Write handlers: Implement message handlers that operate on state

  4. Configure storage: Enable clustering and persistence in DDL

  5. Test failover: Verify state consistency and message retransmission

  6. Tune performance: Adjust threading and batching based on load testing

  7. Monitor in production: Track transaction log size and replication latency

Last updated