Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 48 ยท Real-World Elixir

Performance and Common Pitfalls

Avoid slow Elixir code: list append costs, binaries, N+1 queries in Ecto, unbounded mailboxes, large messages, and how to measure with Benchee.

Elixir programs are usually fast enough without any tuning, because the BEAM was built for many small tasks running at once. When something is slow, the cause is rarely the language itself. It is usually a data structure used the wrong way, a database query repeated hundreds of times, or a process that is quietly drowning in messages.

This lesson lists the pitfalls that bite most often, explains the reason behind each, and shows how to measure before you change anything. Real Elixir blocks are read-only here, and the editors use JavaScript stand-ins to count operations, so the numbers you see are about the shape of the problem, not about real BEAM timings.

Measure first

Benchmark Compare versions with real numbers.
Profile Find where the time really goes.
Then fix Change one thing at a time.

Guessing at performance is how code gets worse.

1 / 2

Lists: do not append in a loop

A list in Elixir is a linked list. Adding to the front is one step, because the new cell just points at the old list. Adding to the end means walking the whole list and rebuilding it, which costs time proportional to its length. Do that inside a loop and the total work grows with the square of the size.

ELIXIR
# Slow: every ++ copies the accumulator, so the loop is O(n^2)
Enum.reduce(1..n, [], fn x, acc -> acc ++ [x * 2] end)

# Fast: prepend, then reverse once at the end
1..n
|> Enum.reduce([], fn x, acc -> [x * 2 | acc] end)
|> Enum.reverse()

# Even better: let Enum do it
Enum.map(1..n, &(&1 * 2))

Also remember that length/1 and Enum.at/2 walk the list. Calling Enum.at(list, i) inside a loop over i is the same quadratic trap in disguise. If you need random access, use a map, a tuple, or restructure the algorithm. The next editor counts the steps.

JS Counting the cost of append versus prepend

Press Run (or Ctrl + Enter) to see the console output here.

No output.

For n = 400 the append version does tens of thousands of steps for 400 elements, while prepending does exactly 400. That gap is the whole lesson.

Strings and binaries

Strings are binaries, and the BEAM treats large binaries (over 64 bytes) specially: they live in a shared heap and are passed around by reference with a counter. That makes sending a big binary between processes cheap. The pitfall is the reverse. If you keep a tiny slice of a huge binary, for example the first 10 bytes of a 100 MB file, the slice can keep the whole large binary alive in memory. When you store a small piece for a long time, copy it with :binary.copy/1 so the big one can be freed.

Building text is the other common cost. Repeatedly using <> in a loop creates new binaries, though the runtime optimizes appends in many cases. For output, IO lists are the idiomatic fast path: a nested list of strings and bytes that you hand to the I/O layer without ever concatenating.

ELIXIR
# Builds one big string first
Enum.map_join(rows, "\n", &format_row/1)

# IO list: nothing is joined; the writer walks the structure
lines = Enum.map(rows, fn row -> [format_row(row), ?\n] end)
IO.puts(lines)

Phoenix templates use this internally, which is a large part of why rendering is fast. Read more about the types in Strings and Binaries.

N+1 queries in Ecto

The most expensive mistake in web apps is the N+1 query: one query to load N records, then one more query for each to load something related. With 100 posts, that is 101 round trips to the database.

ELIXIR
posts = Repo.all(Post)

# Each call runs another query. 1 + N queries in total.
Enum.map(posts, fn post -> {post.title, Repo.all(Ecto.assoc(post, :comments))} end)

Ecto never loads associations lazily, so an unloaded association is a visible %Ecto.Association.NotLoaded{} value and you are forced to decide how to load it. Do it up front with preload:

ELIXIR
# 2 queries: one for posts, one for all their comments
posts = Post |> Repo.all() |> Repo.preload(:comments)

# or one query with a join
from(p in Post, join: c in assoc(p, :comments), preload: [comments: c]) |> Repo.all()

Pick separate preload queries for large has_many data (a join repeats the parent row for every child), and a join when you also filter on the association. Other database habits matter just as much: add indexes for the columns you filter and sort by, select only the fields you need, and paginate. See Ecto: Databases and Changesets for the query basics.

JS Counting queries: N+1 versus preload

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The results are identical; only the number of round trips differs, and each one costs network time in real life.

Mailboxes and message volume

Every process has a mailbox, and it is unbounded. If a process receives messages faster than it handles them, the mailbox grows, memory climbs, and eventually the node can fall over. There is no automatic back-pressure on send/2 or GenServer.cast/2.

Warning signs: Process.info(pid, :message_queue_len) keeps growing, and calls to a busy GenServer begin to time out. Remedies:

  • Prefer GenServer.call/3 for work where callers should wait: a call blocks the caller, which slows producers down naturally.
  • Limit concurrency with Task.async_stream/3 and its max_concurrency option (see Tasks and Parallel Work).
  • Move slow work out of the server's message loop, so it answers quickly.
  • Use a bounded queue or a library designed for back-pressure when you ingest from outside.
  • In a selective receive, messages that never match stay in the mailbox and are scanned again each time, so always handle unexpected messages (a catch-all clause in handle_info/2).

A single process is also a serial bottleneck. If every request goes through one GenServer, you have a queue in front of your whole app. For hot, read-heavy data, consider ETS instead.

Large messages are copied

Messages between processes are copied into the receiver's heap (large binaries aside, as above). Sending a 50 MB map to another process copies 50 MB, and the same goes for closures that capture big values, which is easy to miss:

ELIXIR
big = load_huge_structure()

# The anonymous function captures `big`, so spawning it copies `big` too
Task.async(fn -> process(big) end)

Send only what the other process needs, such as an id, and let it fetch the rest. Or share data through ETS or persistent_term, where reads do not copy a whole structure the same way for every consumer.

Other pitfalls worth knowing

  • Body recursion on huge inputs. A non-tail-recursive function over a million items builds a deep stack. It works on the BEAM (the stack grows on the heap), but memory grows too. See Recursion and Tail Calls.
  • Eager pipelines on big data. Each Enum step builds a full intermediate list. For big or unbounded sources, use Stream, as in Lazy Processing with Stream.
  • String.to_atom/1 on user input is a memory and safety issue; see Security Basics.
  • Logging inside tight loops is a large cost; log at boundaries.

Measure with Benchee

Before optimizing, run a benchmark. Benchee is the standard tool. Add it as a dev dependency, then compare functions side by side:

ELIXIR
Benchee.run(
  %{
    "append" => fn -> Enum.reduce(1..5_000, [], fn x, acc -> acc ++ [x] end) end,
    "prepend + reverse" => fn ->
      1..5_000 |> Enum.reduce([], fn x, acc -> [x | acc] end) |> Enum.reverse()
    end,
    "Enum.to_list" => fn -> Enum.to_list(1..5_000) end
  },
  time: 3,
  memory_time: 1
)

It prints iterations per second, average time, deviation and memory use for each case. Tips: benchmark with realistic input sizes, run in the prod environment or at least without debug tooling, and look at memory as well as time. For finding hot spots in a running system, use :observer, :eprof, :fprof or LiveDashboard rather than guessing.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is acc ++ [x] inside a reduce slow for large lists?

    ++ rebuilds the whole left-hand list. Prepending with [x | acc] and reversing once is linear.
  2. 2. How do you avoid the N+1 query problem in Ecto?

    Repo.preload/2 or a query with preload loads all related rows in a fixed number of queries.
  3. 3. What is the risk of an unbounded mailbox?

    Mailboxes have no size limit. A slow consumer accumulates messages until the node runs out of memory or calls time out.
  4. 4. What happens when you send a large map to another process?

    Ordinary terms are copied between process heaps. Large binaries are the exception, they are reference counted.
  5. 5. What should you do before optimizing?

    Measurements show where time and memory really go and whether a change helped.

Summary

  • Prepend to lists and reverse once; avoid ++ and Enum.at/2 in loops.
  • Large binaries are shared, but a small slice can pin a huge one; copy slices you keep. Use IO lists for output.
  • Preload associations to avoid N+1 queries, add indexes and paginate.
  • Mailboxes are unbounded: use calls, bounded concurrency and fast handlers.
  • Messages and closures are copied, so send small data or share through ETS.
  • Benchmark with Benchee and profile before changing code.

Next up: Security Basics.