{"slug": "the-erlang-queue-module-in-elixir", "title": "The Erlang :queue module in Elixir", "summary": "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.", "body_md": "The Erlang :queue module in Elixir\nElixir 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 []\n, {}\n, and %{}\n, and the modules List\n, Tuple\n, and Map\n. Elixir also exposes “structs”, which are maps with special behaviors associated with them. Some structs are treated as opaque, like MapSet\n, 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\n.\nErlang 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:\nQueues 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.\nA 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.\nUsing 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.\nHere’s some examples of how to use it\niex(38)> q = :queue.new()\n{[], []}\niex(33)> q = :queue.in(\"a\", q)\n{[\"a\"], []}\niex(34)> q = :queue.in(\"b\", q)\n{[\"b\"], [\"a\"]}\niex(35)> q = :queue.in(\"c\", q)\n{[\"c\", \"b\"], [\"a\"]}\nWhen getting items back out you need to keep track of both the item and the queue.\niex(36)> {{:value, value3}, q} = :queue.out(q)\n{{:value, \"a\"}, {[\"c\"], [\"b\"]}}\niex(37)> {{:value, value2}, q} = :queue.out(q)\n{{:value, \"b\"}, {[], [\"c\"]}}\niex(37)> {{:value, value3}, q} = :queue.out(q)\n{{:value, \"c\"}, {[], []}}\nIf the queue is empty you get the :empty\natom instead.\niex(39)> :queue.out(q)\n{:empty, {[], []}}\nOne thing to note about queues is that they don’t keep track of their length themselves, so :queue.len/1\nhas 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.\nCheck out the documentation for more information and functions.\nWritten 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.\nRelated posts\nHighest Random Weight in Elixir\nA description of HRW/rendezvous hashing and the HRW elixir library.\nbunnyx: a bunny.net Elixir client library\nA best-practice Elixir library for interacting with the bunny.net API\nBuilding for the joy of building\nMy path into programming and why I've been obsessed for 20 years.", "url": "https://wpnews.pro/news/the-erlang-queue-module-in-elixir", "canonical_source": "https://jola.dev/posts/erlang-queue-module-elixir", "published_at": "2019-10-18 00:00:00+00:00", "updated_at": "2026-05-23 15:51:20.940083+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Erlang", "Elixir", "MapSet"], "alternates": {"html": "https://wpnews.pro/news/the-erlang-queue-module-in-elixir", "markdown": "https://wpnews.pro/news/the-erlang-queue-module-in-elixir.md", "text": "https://wpnews.pro/news/the-erlang-queue-module-in-elixir.txt", "jsonld": "https://wpnews.pro/news/the-erlang-queue-module-in-elixir.jsonld"}}