Message delivery order guarantees for GenServer

I know there is call and cast. The difference is that call blocks the caller until a reply is made and cast returns right away not blocking the caller. However the scenario im interested in is the following.

Given two processes A and B. A send two messages A1 and A2 to B and it want to send them as fast as possible but it must also guarantee that A1 is handled before A2 by B.

Now the problem I have with this is that if I use call A must wait until A1 is processed by B, I want to use the message queue of B to send messages. And if i use cast there is no order guarantees. A2 could get processed before A1.

What is the idiomatic Elixir code for this case?

The order should be guarantee by B’s mailbox arrival order…

I do not think it is related to call or cast.

When You cast, You simply have no return message.

Where did You get this?

And if i use cast there is no order guarantees. A2 could get processed before A1.

That is just my guess since cast is async I assumed it did not have this guarantee… But it would be very nice if it has.

From Erlang’s doc

10.8 Is the order of message reception guaranteed?

Yes, but only within one process.

If there is a live process and you send it message A and then message B, it’s guaranteed that if message B arrived, message A arrived before it.

On the other hand, imagine processes P, Q and R. P sends message A to Q, and then message B to R. There is no guarantee that A arrives before B. (Distributed Erlang would have a pretty tough time if this was required!)

PS. It’s not the same async as in JS :slight_smile:

2 Likes