Ephemeral Actors: Scaling Request Handling Without Blocking
An actor handles one message at a time. Its mailbox is a queue, and messages are taken in turn. That is what makes its state safe without locks, but it also means a slow message holds every message behind it. For an actor that serves independent requests, this is a bottleneck.
The mailbox bottleneck
data Search = Search
data SearchMsg = Query String (Reply [String])
@route("search")
instance Actor Search where
type Message Search = SearchMsg
receive ctx s msg = case msg of
Query text r -> respond r (lookedUp text) >> continue s -- 200 ms on average, 2 s under load
If 100 queries arrive together, the 100th waits for the 99 before it. Tail latency grows with concurrency.
The @ephemeral attribute
@route("search")
@ephemeral
instance Actor Search where
...
The methods are unchanged. An ephemeral actor handles every message at once, in a fiber of its own, starting from the actor’s first state. A hundred queries are a hundred fibers.
What changes
- No state carried between messages. Each message starts from
initial; whatever a method returns is dropped. Keep state in other actors (a cache, a repository) and ask them. - No become, no stash. They describe how one message affects the next, which an ephemeral actor does not have.
- Failures stay with the message. A message that fails tells its asker; the actor itself does not stop, so there is nothing for a supervisor to restart.
When to use it
Use @ephemeral |
Keep a normal actor |
|---|---|
| Stateless request handlers | Anything whose state changes with its messages |
| Fan-out work (search, rendering, calls to other services) | Ordering matters between messages |
| One slow request must not hold the rest | A bounded or prioritized mailbox is the point |
Fibers, not threads
A fiber is the runtime’s own, cheap to start and parked when it waits on an ask, while the others run. An ephemeral actor’s hundred requests are a hundred fibers on the runtime’s scheduler, not a hundred operating-system threads.