Prefer Call Over Cast to Provide Back Pressure

Intuitively, you might think that it’s best to use the one-way handle_cast to send messages that don’t need responses. For example, the :add_template message doesn’t really need a response. We just trust that the template was added successfully. If it’s not, something has gone horribly wrong. There’s nothing we can do beyond crashing the server and reporting the reasons for the crash back to the user.

Interestingly, handle_cast is rarely the best option for sending messages. In this section, we’ll look at one of the reasons why. They are called serializability and back pressure. Let’s explore why.

As you probably know, each Elixir process has a message queue. We’ll call it the mailbox. Unlike ...

Get Designing Elixir Systems With OTP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.