cd /news/developer-tools/the-erlang-queue-module-in-elixir · home topics developer-tools article
[ARTICLE · art-11746] src=jola.dev ↗ pub= topic=developer-tools verified=true sentiment=· neutral

The Erlang :queue module in Elixir

The article explains that Elixir uses Erlang's `:queue` module, which provides a double-ended FIFO (first-in, first-out) data structure for efficiently adding and removing items from both ends. It notes that using queues can feel awkward due to argument ordering differences and exposed internal representation, and warns that `:queue.len/1` must traverse the entire queue, so users with large queues should track length separately. The article includes examples of creating, inserting into, and removing items from a queue, highlighting that an empty queue returns the `:empty` atom.

read3 min views28 publishedOct 18, 2019

The Erlang :queue module in Elixir Elixir doesn’t provide its own data structures, instead, it uses the ones provided by Erlang. Many of them are wrapped by Elixir modules and have shorthand syntax, to make them easier to work with. You’ve seen []

, {}
, and %{}

, and the modules List , Tuple , and Map . Elixir also exposes “structs”, which are maps with special behaviors associated with them. Some structs are treated as opaque, like MapSet , with its own set of functions to interact with it. But Erlang has a few more data types that, because they’re not wrapped in Elixir, you might not have been introduced to yet. This article is about the one I’ve had the most use of, :queue . Erlang queues are similar to lists but double-ended, meaning you can efficiently insert items to both the front and the rear of it. It is also known as a “first in first out”, or FIFO, data structure. The Erlang documentation describes it: Queues are double-ended. The mental picture of a queue is a line of people (items) waiting for their turn. The queue front is the end with the item that has waited the longest. The queue rear is the end an item enters when it starts to wait. If instead using the mental picture of a list, the front is called head and the rear is called tail. A common use case is where you want to temporarily store items and eventually take them back out in the order that you put them in. Using them can feel a bit awkward, partly because Erlang orders arguments differently than Elixir, and partly because the internal representation of queues is exposed. Note that the docs clearly state that you should treat it as an opaque type. Here’s some examples of how to use it

iex(38)> q = :queue.new()
{[], []}
iex(33)> q = :queue.in("a", q)
{["a"], []}
iex(34)> q = :queue.in("b", q)
{["b"], ["a"]}
iex(35)> q = :queue.in("c", q)
{["c", "b"], ["a"]}

When getting items back out you need to keep track of both the item and the queue.

iex(36)> {{:value, value3}, q} = :queue.out(q)
{{:value, "a"}, {["c"], ["b"]}}
iex(37)> {{:value, value2}, q} = :queue.out(q)
{{:value, "b"}, {[], ["c"]}}
iex(37)> {{:value, value3}, q} = :queue.out(q)
{{:value, "c"}, {[], []}}
If the queue is empty you get the :empty

atom instead.

iex(39)> :queue.out(q)
{:empty, {[], []}}

One thing to note about queues is that they don’t keep track of their length themselves, so :queue.len/1 has to traverse the entirety of the queue. If you’re working with very large queues and frequently need to check the size, consider keeping track of it separately, or creating your own wrapped queue module. Check out the documentation for more information and functions. Written by Johanna Larsson. Thoughts on this post? Find me on Bluesky at @jola.dev. If you like my writing, consider supporting me on Github Sponsors and get a monthly newsletter with content from my blog. Related posts Highest Random Weight in Elixir A description of HRW/rendezvous hashing and the HRW elixir library. bunnyx: a bunny.net Elixir client library A best-practice Elixir library for interacting with the bunny.net API Building for the joy of building My path into programming and why I've been obsessed for 20 years.

── more in #developer-tools 4 stories · sorted by recency
── more on @erlang 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/the-erlang-queue-mod…] indexed:0 read:3min 2019-10-18 ·