[erlang-questions] Message send guarantees

Dmitry Kakurin <>
Fri Jan 27 03:25:49 CET 2017


From: Joe Armstrong <>

Sent: Thursday, January 26, 2017 5:29 AM

On Wed, Jan 25, 2017 at 12:38 AM, Dmitry Kakurin
<> wrote:
> When I execute "pid ! msg" and it returns, what are the guarantees if pid
> and self are on the same node?
> Can I assume that by the time it returns, msg is in the pid's queue? And as
> a result all messages sent to pid afterwards will be queued after msg?
>
> The reason I'm asking is because I need a guarantee of proper message
> ordering in the following scenario when all processes involved are local to
> a node:
> 1. A --msg1--> C (process A sends msg1 to process C)
> 2. A --msg2--> B
> 3. B --msg2--> C (process B simply proxies msg2 from A to C)
>
> I need a guarantee that msg2 will always appear in C's queue after msg1.

This cannot be guaranteed

Thanks Joe and others. I understand now that it's fruitless to pursue a direction where I'd try to somehow artificially establish this guarantee.

I have to re-evaluate my constraints then. All along my main constraint was that C can be any gen_server, it's not owned by me and I cannot change it.
If I could change C then the following would work:
1. A --msg1--> C
2. A --ping--> C
3. A <--pong-- C
4. A --msg2--> B
5. B --msg2--> C

All we need from C is to respond "pong" to "ping" message. In this case A knows msg2 is enqueued after msg1 because A has observed "pong" in response to "ping" that was sent after msg1. The ordering is as desired here.
Now the only problem I have to solve is: what can be used as a benign message that I can send to any gen_server such that it would reply (to emulate ping/pong exchange above)?
Looking thru gen_server source code I see that it's processing sys messages. So I can use get_state message as "ping" and response with state as "pong". Except it may not be really benign perf-wise if amount of state kept by C is significant.
There is also get_status, but I don't understand how it's processed and if I can use it for ping/pong purposes. Can I? Is there a better message?

Do you have a better idea for ping/pong messages on C? Or how to solve this in general?

Thank you, Dmitry.

P.S. I know that in this simplified example I could piggyback on msg1 if it returns something but in real code I cannot, so let's assume it's a one-way message (a cast, not a call). So I'm OK with ping/pong.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170127/18fbffec/attachment.html>


More information about the erlang-questions mailing list