logoalt Hacker News

adrian_b • yesterday at 2:18 PM • 3 replies • view on HN

> Messages are sent directly between actors

Such a thing cannot exist whenever more than 1 actor can be executed simultaneously.

An actor may have a single input queue, and all other actors can send messages to that unique input queue.

In this case, because there is a 1-to-1 mapping between input queues and actors you could identify the input queues with the actors.

But inside the code implementing the actor, you must have a loop that extracts messages from the input queue, so you must be aware that the input queue exists and it is not the same thing as the actor.

If you had a bad actor implementation without a unique input queue, than you would need something like a separate mailbox for every other actor, where the other actors would deposit messages and each actor would have to poll continuously all such mailboxes, to catch the arrival of any messages, which would be very inefficient.

"Spawning a new actor per request" just means that the implementation of the actor is split into 2 parts, the loop that extracts messages from its input queue and a function that is invoked upon the arrival of a new message, having the message as argument.

Such a splitting makes sense, because the loop handling the input queue could be the same for all actors (but acting on different input queues), with only the function that is invoked for a message containing the part that is different for each actor.


Replies

spinningslate • yesterday at 2:45 PM

I think you and the parent are saying the same thing. The previous sentence to the one you quoted says:

> in actor systems, there's no intermediate queue-like channel that messages are sent to.

There are essentially two message passing models:

1. Multiple writer/single reader

2. Multiple writer/multiple reader

Where multiple/single means the number of actors/processes[/whatever] that can read or write to a queue.

Erlang and the Actor model are examples of the former. Multiple Actors can write to a given queue but only one Actor can read from it. From the writer's perspective, the queue is part of the reading Actor; the abstraction boundary encompasses the queue. Strictly speaking, the writer doesn't even know a queue exists: it just has a porthole it can push messages at.

Internally the Actor comprises 3 things: (1) the queue, (2) the read loop, and (3) the actions that get called in the read loop based on the received message. None of that is visible outside the Actor though. If you draw a diagram of an Actor system, it only shows Actors and the messages sent among them. Queues are hidden at that level.

Contrast that with the multiple reader/multiple writer scenario. This is the model used in service bus middleware like RabbitMQ. In this case the queues are external to the readers and writers. A system-level diagram would show the queues as first-class entities, alongside the components reading and writing to them.

➕ show 1 reply
eproxus • yesterday at 2:25 PM

That's exactly how Erlang works, except it doesn't poll continuously, it has a receive statement that blocks and waits for messages. The reason this can be made efficient is because the actor itself is not polling, the scheduler (the actual thread executing the actor) knows that the actor is waiting and executes other things while waiting for messages. It is much more reactive.

I'm guessing an OS built on the BEAM or a similar model would take a similar approach, where code executed by actors is actually run in schedulers, that can multiplex them and make the message passing efficient because they know what happens in the system.

antonvs • yesterday at 2:22 PM

The very next sentence after the one you quoted was, "Each actor manages its own private message queue." That covers everything you're saying, which is basically describing how actors are implemented.

The point is that it's not a shared message queue as in the COBOL case, or most enterprise queuing systems. An actor is a self-contained, isolated object with its own message handling and state, and it executes concurrently with all other actors.

As such, this is not quite correct:

> you must be aware that the input queue exists and it is not the same thing as the actor.

The term "actor" refers to the bundle of state, behavior (handler loop), and message queue.

You certainly must be aware of the existence of the queue though, but since you have to write a message handler loop for each actor, it would be difficult not to be.

➕ show 3 replies