Message Prioritization inside GenServer


Is there a way to send a message to a Genserver process such that the message skips all other messages in the genserver’s message queue, and will be the next message to be processed?

Or would the solution involve using a custom queue in front of the genserver worker that exposes an API to achieve this?


You can use something called a selective receive. The basic idea is to just pattern match on the priority message first and the rest of them later. There are some drawbacks doing this especially for large mailboxes. Also note, that you must do this in your own processes and can’t use GenServer behaviour (as far as I know).

Some links I found.

If you google for selective receive you may found more information.

An example:

def check_important_messages_first() do
    receive do
        {:important, message} ->
    after 0 ->
        receive do
          {:important, message} ->
         {:normal, message} ->

Thanks for the helpful links @cmkarlsson! I should have mentioned though that the process I was referring to was indeed a genserver :frowning: I will update my OP to reflect this.

I dunno efficiency of this solution, but here’s idea: receive all messages and store it in priority queue, then process them one by one from this queue. Higher priority events will appear first to be processed.

1 Like

So you are suggesting the custom queue in front of the worker approach? I think that is probably the way to go. Are you aware of any good libraries that tackle this use case?

Unfortunately, no. That was just abstract idea, but I’m sure there’s library for that.

I couldn’t find any in particular, so I decided to use this: to simulate 3 queues with 3 priorities and store them in the GenServer’s state.

GenServer’s process in order, that is the purpose of their design.

There are alternative GenServer’s out in the erlang world (and thus useable via Elixir) that are Priority based genservers, so you can send messages and process them in different orders (first by priority then by received order).

So @OvermindDL1, you’d consider managing priority state explicitly via queues stored in GenServer state bad practice? I want to use elixir modules as much as I can and managing priority manually doesn’t seem too hard.

Creating and managing a priority queue which you update on new messages is probably more expensive than just processing the messages in order.


def handle(state),
  do: handle([:high, :medium, :low], state).
def handle([], state) do
    receive do
      {_priority, msg} ->
        do_it(msg, state)

def handle([priority | tail], state) do
    receive do
      {priority, msg} ->
        do_it(msg, state)
    after 0 ->
      handle(tail, state)

def do_it(msg, state) do
  # ...



A normal process can send messages to a GenServer - it would just mean that the priority management is in a separate process (which really shouldn’t be a big deal - Tasks for example are raw processes.) If the priority management process uses a blocking call/3 to the GenServer it wouldn’t select the next message from the process mailbox until the call returns.

Not necessarily, but you’d need to have both cast’s and call’s return immediately with a 0-sleep, so that all messages will be received and buffered and then you can handle a given message in the 0-sleep timeout function.

In general absolutely this, but there are definite use-cases where a priority queue is very important.

You don’t have to return a message to a call immediately, you are just supposed to do it ‘sometime within the timeout’. :slight_smile:

Alternately promote the priority of messages-in-waiting in order to return most, if not all responses “sometime within the timeout”- which means “managing priority” becomes much more complicated and is tightly coupled to what the GenServer is actually doing.