↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Building Distributed Systems with Elixir — 02: Correlated Request–Reply

The article explains that a PID tells the server where to send a response, but it does not identify which request the response belongs to. To solve that, the client creates a unique reference with make_ref/0 and sends it with the request, while the server returns the same reference in the reply.

The client then uses selective receive with ^ref to match only the reply for that request, leaving other messages in the mailbox. The pattern works with one client or many, and the article closes by pointing to process monitoring as the next step.

Reading notes
#

  • A PID identifies the process, not an individual request.
  • make_ref/0 creates a unique request identifier.
  • The server receives {:request, from, ref, value} and replies with {:reply, ref, result}.
  • The client matches replies with {:reply, ^ref, result}.
  • Unmatched messages stay in the mailbox, which is selective receive.
  • One process can send several requests and keep the same PID while each request gets a different reference.
  • The pattern also works when multiple client processes talk to the same server.
  • Processes are for concurrency and isolation, while references are for identifying operations.
  • The next topic in the series is process monitoring and :DOWN messages.

Series
#

Building Distributed Systems in Elixir, by krishnadaspc. Research: Distributed Systems Patterns.