Modelo de Actores

Concurrencia por paso de mensajes

Escrito por Darjus Kazakevic · revisado el

No shared memory, so no locks to get wrong.

Shared mutable state is what makes concurrent code hard. The Actor Model removes it. An actor owns its state, and nothing else can touch it. Other code can only send it messages. The actor handles them one at a time, so inside it you write simple single-threaded code with no locks.

Erlang/OTP (WhatsApp, RabbitMQ), Akka on the JVM and Microsoft Orleans (Halo's online services) are built this way. The cost is message design. Everything becomes asynchronous, replies need matching ids, and a message to a crashed actor is lost unless a supervisor restarts it.

En esencia — Give each actor private state and a mailbox, and process one message at a time, so concurrency is safe by design rather than by careful locking.

Cuándo recurrir a él

Recurre a él cuando

  • Many independent entities own mutable state: accounts, sessions, devices
  • Shared-memory locking keeps causing races and deadlocks
  • The same code should scale from one machine to a cluster

Reconsidéralo cuando

  • Mostly synchronous request and response, where every reply needs matching
  • One operation must update many entities atomically
  • Simple CRUD, where a database transaction already handles concurrency

The parts

Actor
Private state plus behaviour. An actor can send messages, create new actors and change how it handles the next message. Actors are cheap: an Erlang process starts with a few kilobytes, so one machine runs millions.
Mailbox
The queue of messages waiting for one actor. The actor takes one message, handles it fully, then takes the next. This order is what removes the need for locks.
Message
An immutable value sent to an actor's address. Sending never blocks and returns nothing; a reply is just another message sent back.
Supervisor
An actor whose job is to watch other actors and restart them when they crash. Erlang calls this “let it crash”: recover by restarting from a known state, not by defensive code everywhere.

Reglas de diseño

Never let an actor's state escape
Expose nothing but a mailbox. The safety comes from only the actor touching its state, one message at a time. Hand out a reference and you have shared memory again, without locks.
Supervise; do not send and hope
A message to a crashed actor is lost. Give actors a supervisor that restarts them, and put a timeout on every request that expects a reply.
Keep handlers short and non-blocking
An actor handles one message at a time, so one slow database call blocks its whole mailbox. Move slow work elsewhere and send the result back as a message.
Watch mailbox size
A mailbox grows when messages arrive faster than the actor handles them. Monitor its length, and add backpressure or more actors before memory runs out.

Errores habituales

  • Blocking calls inside a handler. One slow actor delays every message behind it.
  • Sharing a mutable object inside a message. The race conditions come back, now harder to see.
  • Unbounded mailboxes. Under load, memory grows until the node dies.
  • Request-and-wait everywhere. When one actor is slow, a chain of timeouts follows.