Blog

    Akka Actors for Microservices: Per-Patient Message Ordering and the License Check Before You Adopt

    By GauravSeptember 15, 20269 mins read
    On this page

    Akka actors for microservices give each patient, order or device its own small unit of state that handles one message at a time, which makes event ordering easier to reason about. Before a team adopts Akka, though, it has to settle a licensing question that changed in 2022.

    Startup founders and engineering leaders will find how per-patient actors keep clinical events in sequence, where the ordering guarantee stops, how sharding and persistence fit, and what to check in the license before production.

    What are Akka actors for microservices?

    Akka actors for microservices are lightweight objects that own private state and process messages from a mailbox one at a time. In a healthcare service, one actor per patient can apply admissions, orders and results in sequence. Agnotic Technologies builds these services for clients and checks the Akka license terms before any code ships.

    Planning an actor-based service?

    We design the actor model, the event store and the license decision with your team.

    Akka's message delivery reliability rules give at-most-once delivery and, for a given pair of actors, guarantee that messages sent directly from the first to the second will not be received out of order. Akka notes that with a non-FIFO mailbox, such as a priority mailbox, the processing order can differ from the order messages were enqueued.

    That guarantee is narrower than many teams assume. It covers one sender talking to one receiver. Two upstream services that both write to the same patient actor can still interleave, so the design has to decide which source sets the order.

    Why does per-patient ordering matter?

    Per-patient ordering matters because clinical events change meaning when they arrive out of sequence. A discharge processed before its admission, or a cancellation applied before the order it cancels, leaves the record wrong even though every message was delivered.

    • Admission, transfer and discharge: a transfer that lands before the admission has no bed to move from.
    • Orders and cancellations: a cancel that overtakes its order leaves an active order nobody wants.
    • Results and corrections: a corrected lab value must replace the preliminary one, never the reverse.
    • Consent changes: a withdrawal of consent has to stop later sharing, so it cannot wait behind queued messages.

    A shared database with row locks can enforce this too, but under load the locks become the bottleneck. An actor per patient serializes work for that patient only, while other patients proceed in parallel.

    How does an actor per patient work?

    An actor per patient works by routing every message that carries a patient key to the one actor that owns that key. The actor holds the current state, applies each message in turn and replies or emits events.

    • Step 1, key: choose a stable identifier, usually an internal patient ID rather than a medical record number that can be merged.
    • Step 2, route: send every command for that key through the same entry point so it reaches the same actor.
    • Step 3, apply: the actor validates the command against its current state and either accepts or rejects it.
    • Step 4, record: accepted changes are written as events before the actor's in-memory state is updated.
    • Step 5, publish: downstream services read the events rather than querying the actor directly.

    Because only one message is processed at a time inside an actor, the business rules do not need locks. The hard part moves to routing, persistence and the rules for messages that arrive late or twice.

    Where does the ordering guarantee stop?

    The ordering guarantee stops at the boundary of a single sender and receiver pair using a first-in, first-out mailbox. Anything outside that pair needs its own rule, written into the actor's logic.

    • Several senders: an ADT feed and a scheduling service writing to one actor have no order between them.
    • Retries: a sender that times out and resends can deliver the second copy after a later message.
    • Delivery itself: at-most-once means a message can be lost, so critical commands need acknowledgement and resend.
    • Priority mailboxes: they reorder by design and should not be used where sequence carries meaning.

    The usual fix is a sequence number from the source system. The actor stores the last sequence it applied, ignores duplicates and parks messages that arrive early until the gap is filled or a timeout raises an exception for a person to review.

    Cluster sharding and persistence

    Cluster sharding and persistence let a per-patient design run on more than one node and survive restarts. Akka describes Cluster Sharding as a way to distribute actors across several nodes and reach them by a logical identifier, with each entity actor running in only one place.

    Persistence stores each accepted change as an event, so a restarted actor rebuilds its state by replaying its own history. Snapshots shorten that replay for patients with long records.

    • Shard key: derive it from the patient ID so all messages for one patient land on one shard.
    • Rebalancing: plan for actors moving between nodes during deploys, and test that in-flight messages are not lost.
    • Event schema: version every event type from the first release, because old events are replayed for years.
    • Retention: decide how long events are kept and how a deletion request is honored in an append-only store.

    What should you check in the Akka license?

    You should check which Akka version you would run, which license covers it and whether your use counts as production under that license. The answer changed with Akka 2.7, so a design copied from an older project may carry a cost nobody planned for.

    Lightbend announced on September 7, 2022 that it was changing the license on all Akka modules from Apache 2.0 to the Business Source License (BSL) 1.1, starting with Akka 2.7, and that the source for each version would be released under Apache 2.0 three years after that version.

    Akka's license page says that, outside an Additional Use Grant for Play Framework, the Business Source License allows only non-production use, and production use requires a commercial license from the licensor. For Akka 2.11.0 it lists a Change Date of November 12, 2029, after which that version moves to the Apache License 2.0.

    • Version: list every Akka module and its version in the build, including modules pulled in by other libraries.
    • License text: read the license file for that exact version, including its Change Date.
    • Commercial terms: if production use needs a license, get the terms in writing before the service goes live.
    • Alternative: Apache Pekko, an Apache Software Foundation project, describes itself as a fork of Akka 2.6.x made before the license change.

    This is a procurement and legal decision as much as a technical one. The engineering team's job is to give the people making it an accurate dependency list and an honest view of migration effort.

    Akka, Pekko or no actor toolkit?

    Choosing an actor approach for a per-patient service
    OptionSuitsWatch for
    Akka under a commercial licenseTeams that want vendor support and current Akka releasesLicense cost and terms tied to production use
    Apache PekkoTeams that want an Apache 2.0 toolkit with an API close to Akka 2.6Package name changes and a separate release cycle
    No actor toolkitServices where a queue partitioned by patient key gives enough orderingLocking or partition logic written and tested by hand

    Whichever option you pick, the ordering rules, sequence numbers and event schema stay the same. Those are the parts worth designing carefully first.

    When is an actor model the wrong choice?

    An actor model is the wrong choice when the service is mostly stateless request handling, or when nobody on the team has run an actor cluster in production. The model moves complexity into routing, supervision and persistence.

    • Simple CRUD services: a database transaction is easier to read and test.
    • Reporting and analytics: they read across many patients, which fights the per-patient boundary.
    • Small teams: debugging message flows across a cluster needs tracing and experience.
    • Partitioned streams already in place: a stream partitioned by patient key may already give the ordering you need.

    Testing a per-patient actor service

    Testing a per-patient actor service means testing order, duplicates and restarts, not just the happy path. Most defects show up only when messages arrive in an unexpected sequence.

    • Reorder tests: send the same messages in shuffled order and check the actor either corrects or rejects them.
    • Duplicate tests: deliver every message twice and confirm the state changes once.
    • Restart tests: stop a node mid-stream and confirm actors rebuild the same state from events.
    • Load tests: many patients in parallel, plus a few very busy patients, to find mailbox build-up.

    What are the common mistakes with Akka actors for microservices?

    The common mistakes with Akka actors for microservices come from trusting the ordering guarantee beyond its scope and from treating the license as a later problem.

    • Assuming global order: two senders to one patient actor are treated as if they were one stream.
    • Blocking inside actors: a synchronous database or HTTP call stalls every message queued for that patient.
    • Unversioned events: a renamed field breaks replay for every patient created before the change.
    • Merged identifiers: actors keyed by a record number split one patient's history when records are merged.
    • License found late: production rollout waits on a commercial agreement nobody budgeted.

    What does an actor design mean for your build plan?

    An actor-based service front-loads design work: the patient key, the sequence rule, the event schema and the license decision. Those choices are hard to change once events are stored.

    • Settle the license route before the first sprint, with the dependency list attached.
    • Agree the source of sequence numbers with every upstream system.
    • Design and version the event schema, and plan how it will be replayed.
    • Budget for cluster operations: deploys, rebalancing, monitoring and on-call runbooks.

    How does Agnotic build actor-based services?

    Agnotic designs and builds actor-based services for clients, from the patient key and event schema to cluster deployment and tests for ordering and restarts. We also prepare the dependency and license list your legal team needs to choose between Akka and Pekko.

    Keep every patient event in order

    We build the actor model, the event store and the tests that prove ordering holds.

    Sources

    Frequently Asked Questions

    Founders and engineering leaders planning a service where events for one patient, order or device must be applied in sequence, and who are weighing an actor toolkit against queues and database locks.

    Talk to an engineer about your project

    Tell us which events must stay in order and which systems send them. We will propose the actor design and the license route.

    Go deeper

    Services and solutions on this topic