Erlang / Erlang Advanced Interview questions
Explain the internal working of selective receive and why message order in the queue matters?
A receive block doesn't necessarily take the first message in the mailbox — it scans the
queue in arrival order looking for the first message that matches any of its clauses, skipping over (but
leaving in place) messages that don't match, then removes only the one it matched.
flowchart LR
A[Mailbox: M1, M2, M3] --> B{Does M1 match a clause?}
B -->|No, skip| C{Does M2 match?}
C -->|Yes| D[Remove M2, bind it, continue with M1 and M3 still queued]
This is powerful — you can wait for a specific reply while ignoring unrelated traffic — but it
has a cost: the scan has to walk past every skipped message each time receive runs. If a process
accumulates a large backlog of messages that never match a narrow pattern, every subsequent
receive call gets progressively slower, since it re-scans that growing, unmatched backlog every
time. This is one of the classic causes of a process that seems to "slow down" under load without an obvious
CPU spike.
More Related questions...