Backend Development 13 min read

COLA Stateless State Machine: A Lightweight, Thread‑Safe Alternative to Spring StateMachine for Java Backend Order Processing

The article explains why Spring StateMachine is heavyweight and thread‑unsafe for high‑concurrency order management, introduces the open‑source COLA stateless state machine as a simpler, lock‑free solution, and provides detailed design principles, core concepts, and extensive Java code examples.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
COLA Stateless State Machine: A Lightweight, Thread‑Safe Alternative to Spring StateMachine for Java Backend Order Processing

In typical backend projects the state‑machine pattern is rarely used, but in e‑commerce the sheer number and complexity of order states make manual get/set handling error‑prone and hard to debug. The author experienced these problems with Spring StateMachine, which, although well documented, is heavy, requires complex code to change a single order state, and its instances are stateful, forcing developers to use locks or ThreadLocal for thread safety.

After a painful experience, the author discovered the COLA architecture’s own state‑machine implementation, which is stateless, lightweight, and thread‑safe, making it suitable for high‑concurrency scenarios such as order management.

COLA State Machine Overview

COLA is an open‑source project on GitHub. Its design focuses on two core ideas: a minimal state‑transition machine without support for nested or parallel states, and a completely stateless implementation that can be shared as a singleton across all requests.

The DSL (Domain Specific Language) used by COLA makes state transitions expressive and readable, similar to how regular expressions serve as a DSL for pattern matching.

Core Concepts

State, Event, Transition, External Transition, Internal Transition, Condition, Action, StateMachine.

Implementation Details

The machine uses a fluent interface to enforce the order of method calls when building transitions. For example:

StateMachineBuilder<States, Events, Context> builder = StateMachineBuilderFactory.create();
builder.externalTransition()
        .from(States.STATE1)
        .to(States.STATE2)
        .on(Events.EVENT1)
        .when(checkCondition())
        .perform(doAction());

State machines are registered in a ConcurrentHashMap keyed by a machine ID, allowing fast lookup and reuse:

static Map<String, StateMachine> stateMachineMap = new ConcurrentHashMap<>();

Triggering a transition is as simple as:

stateMachine.fireEvent(States.STATE1, Events.EVENT1, new Context("1"));

The article provides a series of JUnit test cases demonstrating external, internal, multi‑branch, and concurrent transitions, as well as handling of unmet conditions and duplicate definitions, illustrating how COLA’s stateless nature avoids the performance penalties of locking required by Spring StateMachine.

Conclusion

COLA’s minimalist, stateless design makes it an excellent choice for small‑to‑medium sized state‑machine needs such as order management, especially when advanced features like nested or parallel states are unnecessary.

backendJavaconcurrencySpringState MachineCOLAStateless
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.