Patterns
🎭

Actor Model Coordination

Asynchronous message-passing coordination between independent actors

Complexity: highPattern

Core Mechanism

The Actor Model coordinates independent actors that communicate exclusively via asynchronous message passing. Each actor encapsulates its own state and behavior, processes one message at a time, can create new actors, and can send messages to known addresses. Supervision hierarchies handle failures through isolation and restarts, and location transparency enables distributed placement without changing messaging semantics.

Workflow / Steps

  1. Define actor roles, responsibilities, and message protocols (types, headers, routing keys).
  2. Design supervision tree: parent actors supervise children with restart strategies and backoff.
  3. Choose dispatching and mailboxes (bounded where possible) and configure routing/sharding.
  4. Implement actor handlers as pure, non-blocking message processors; externalize I/O via async APIs.
  5. Persist critical actor state (event sourcing/snapshots) if durability or replay is required.
  6. Deploy with location transparency; scale via partitioning/sharding and consumer groups per shard.
  7. Observe with tracing/metrics; enforce SLAs with backpressure, circuit breakers, and timeouts.

Best Practices

Keep actors small and single‑purpose; treat them like lightweight processes with clear protocols.
Use bounded mailboxes and apply backpressure; avoid unbounded queues and hot partitions.
Make handlers non‑blocking; offload I/O to async tasks; avoid shared mutable state between actors.
Model failures explicitly with supervision strategies (restart, resume, stop, escalate).
Use sharding/partitioning keys that balance load; avoid hotspots; enable rebalancing.
Persist state with event sourcing/snapshots where durability matters; ensure idempotency on replay.
Prefer location transparency; do not encode physical addresses in protocol designs.
Instrument with per‑actor metrics (mailbox depth, processing latency, restarts) and distributed tracing.

When NOT to Use

Workloads requiring strict shared‑memory transactions or global locks across many entities.

Ultra low‑latency single‑threaded paths where mailbox scheduling and messaging overhead dominate.

Simple CRUD services where synchronous RPC with a database is sufficient and easier to operate.

Teams without experience in concurrent/distributed debugging and supervision can accrue complexity fast.

Common Pitfalls

Unbounded actor creation and mailbox growth → memory pressure and GC pauses.

Blocking calls inside actors → deadlocks, throughput collapse, and missed SLAs.

Assuming in‑order delivery across the system; failing to handle retries and duplicates.

Hot sharding keys causing skew; missing rebalancing; poor addressability design.

Restart storms from improper supervision or non‑idempotent side effects on replay.

Key Features

Asynchronous message passing with mailboxes and backpressure controls
State encapsulation and single‑threaded execution per actor
Supervision hierarchies and fault isolation with restart policies
Location transparency and dynamic actor creation
Cluster sharding/partitioning for scale‑out
Persistence via event sourcing and snapshots (optional)

KPIs / Success Metrics

Mailbox depth distribution and time‑in‑queue; p50/p95/p99 processing latency per actor.
Throughput (msgs/s) per shard; CPU/memory utilization per node.
Restart rate, failure domains impacted, and successful recovery time.
Message loss/duplicate rate (with/without persistence); dead letter queue incidence.
Shard rebalancing time; hotspot detection; load skew metrics.
Cost and efficiency: messages per dollar, tokens per task (for LLM‑backed actors).

Token / Resource Usage

LLM tokens scale with per‑message context and number of actor hops. Use compact schemas, summaries, and references to external state instead of full transcripts to control prompt size.

Bound retries and apply early‑exit heuristics on high confidence; cache frequent tool/LLM results.

System resources: cap mailboxes, limit parallelism per shard, and monitor persistence I/O for event‑sourced actors.

Best Use Cases

Real‑time coordination with fault isolation (e.g., chat/agent assistants per session, IoT/device control).

Distributed stream processing and pipelines requiring stateful, independent workers.

Online gaming, trading, or telemetry where entities map naturally to actors.

Large multi‑agent systems where supervision trees and actor sharding provide resilience and scale.

References & Further Reading

Tools & Libraries

  • Erlang/OTP, Elixir GenServer/OTP, Akka Typed, Akka.NET, Microsoft Orleans, Dapr Actors
  • Ray Actors, Cloudflare Durable Objects (actor‑like), CAF (C++ Actor Framework), Proto.Actor, Actix

Patterns

closed

Loading...

Built by Kortexya