Skip to content

Why the Elixir language has great potential

Update: There is a nice discussion on Hacker News about this blog post.

As a developer I think it is important to stay up to date with new developments. But mostly I love learning new things ;-)
This means to be on the lookout for current trends in the tech industry, guessing their potential and trying them out. Sometimes I’m right. For example: A few months ago, adopting GraphQL turned out to be a good guess.

Elixir

One development that kept me busy next to work for the last few weeks was the Elixir language. My background is for the biggest parts in PHP and Javascript. Learning a programming language that is dynamic, functional, immutable, and supports concurrency and pattern matching can be mind bending. But apart from being a modern language, Elixir intrigues me because it…

  1. Compiles to Erlang

    Running on the battle tested Erlang VM is a great choice because its built-in support for concurrency, distribution and fault tolerance. Before looking into Elixir I wasn’t aware how versatile the Erlang VM is and how much it can simplify a tech stack.


    [Source](https://www.manning.com/books/elixir-in-action)
  2. Quality of libraries

    For all my needs so far, I found some library. The quality and maturity of libraries surprised me. For example: the web framework Phoenix, the database abstraction Ecto and the GraphQL library Absinthe. Many more libraries can be found on the package manager hex.

  3. Path for scalability

    Premature optimization is costly. Rewrites caused by quick and dirty work are as well. Thanks to message passing and processes of Erlang there is actually a very nice path for scaling from small to large while reducing waste:

    > 1. Design your code around processes
    > 2. Separate these processes into OTP apps
    > 3. When an OTP app gets too much load, move it to its own node on better hardware
    Source: [Learn Phoenix episode “Scaling Phoenix”](https://www.learnphoenix.tv/episodes/scaling).

4) Reliability and resilience

Contrary to processes in other programming languages, a process that fails in Erlang doesn’t kill the whole application. If a process dies it can be restarted by a supervisor. Organising an application in supervision trees enables building very reliable systems, containing failures within each component. Erlang is known for it’s Let It Crash philosophy.

5) Performance

So far I’m very happy with the speed of Elixir/Erlang. Reading about sub-millisecond response times and scaling to 2 million active websocket connections on one server lets me not worry much about this topic just yet.

Especially when compared to PHP, Elixir comes with great performance. Being able to handle so many websocket connections enables a whole different user experience for web and mobile apps. I’m looking forward to using Elixir with GraphQL subscriptions.

6) Industry adoption

Although Elixir is quite young, I was glad to see usages by Pinterest, Bleacher Report, Brightcove, and more and more).

The Elixir Language

Why do functional programmers homeschool their kids?

Because they hate classes

The creator of Elixir is the well-known Rubyist José Valim.
The language is influenced by Ruby which makes it subjectively nice to read. I don’t want to go into the language in detail, as other people did this already much better than I could do.

What I wanted to highlight is the usefulness of the pipe operator |>. It works just like the unix pipeline and shuffles the return value of function A into the argument of function B. Instead of writing b(a()) you can write a() |> b().

There are many more things I like, for example pattern matching, the built in Stream module, extensibility via macros and protocols, documentation in markdown format, shared nothing concurrent programming (actor model) and much more.

Tips for learning Elixir

Coming from object oriented programming languages it wasn’t easy to unlearn patterns I practiced for years. But well worth it. How to get started?

Screencast series

I’m a big fan of screencasts. I like the combination between seeing code and hearing explanation at the same time. Luckily I found two nice screencast series about Elixir and Phoenix:

I also saw that there is DailyDrip Elixir which I’d like to watch in the future.

Exercises

Just watching screencast series won’t teach you a language. Practice is important. One way to do this, are the exercises on exercise.io. These exercises are small, fun and teach a lot about the language. Reading solutions of other people after submitting your own is a great way to learn about alternative and maybe better code.

Books

Although I haven’t gotten the chance to read these books myself, I heard good things about them:

Elixirs future?

Who knows. From what I see now, Elixir, although young, has great potential to become mainstream. It enables creating fast, reliable and concurrent modern applications with high productivity. I plan to use Elixir in 2017 at least for some side-projects.

© 2022 by Adrian Philipp