Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang/OTP: Synchronous vs. Asynchronous messaging

One of the things that attracted me to Erlang in the first place is the Actor model; the idea that different processes run concurrently and interact via asynchronous messaging.

I'm just starting to get my teeth into OTP and in particular looking at gen_server. All the examples I've seen - and granted they are tutorial type examples - use handle_call() rather than handle_cast() to implement module behaviour.

I find that a little confusing. As far as I can tell, handle_call is a synchronous operation: the caller is blocked until the callee completes and returns. Which seems to run counter to the async message passing philosophy.

I'm about to start a new OTP application. This seems like a fundamental architectural decision so I want to be sure I understand before embarking.

My questions are:

  1. In real practice do people tend to use handle_call rather than handle_cast?
  2. If so, what's the scalability impact when multiple clients can call the same process/module?
like image 803
sfinnie Avatar asked May 17 '11 13:05

sfinnie


People also ask

Is WhatsApp synchronous or asynchronous?

Therefore, Chatting on WhatsApp is an example of Synchronous communication. Asynchronous communication refers to any form of communication in which one person sends information and the recipients must wait a certain amount of time to process it and respond.

Is SMS asynchronous communication?

Asynchronous Messaging, also called “async messaging,” is a communication method where a message is placed in a queue and does not require an immediate response to move forward with processing. Samples of asynchronous messages include email and SMS, where both parties are free to respond on their own time.

What is the difference between synchronous and asynchronous messages?

While synchronous messaging is a live person-to-person conversation, asynchronous communication doesn't require both parties to be present and speaking at the same time. This is great for the customer because they are able to start, pause, and resume a conversation around their life.

Why the asynchronous messaging is preferred over synchronous messaging?

Asynchronous communication is better for working with different time zones as it creates a permanent record of ideas, decisions, and discussions. Basically, synchronous communications happen in real-time, where asynchronous communications happen over a period of time.


1 Answers

  1. Depends on your situation.

    If you want to get a result, handle_call is really common. If you're not interested in the result of the call, use handle_cast. When handle_call is used, the caller will block, yes. This is most of time okay. Let's take a look at an example.

    If you have a web server, that returns contents of files to clients, you'll be able to handle multiple clients. Each client have to wait for the contents of files to be read, so using handle_call in such a scenario would be perfectly fine (stupid example aside).

    When you really need the behavior of sending a request, doing some other processing and then getting the reply later, typically two calls are used (for example, one cast and the one call to get the result) or normal message passing. But this is a fairly rare case.

  2. Using handle_call will block the process for the duration of the call. This will lead to clients queuing up to get their replies and thus the whole thing will run in sequence.

    If you want parallel code, you have to write parallel code. The only way to do that is to run multiple processes.

So, to summarize:

  • Using handle_call will block the caller and occupy the process called for the duration of the call.
  • If you want parallel activities to go on, you have to parallelize. The only way to do that is by starting more processes, and suddenly call vs cast is not such a big issue any more (in fact, it's more comfortable with call).
like image 112
Adam Lindberg Avatar answered Oct 20 '22 04:10

Adam Lindberg