In the beginning of 2015, I wrote a blog post about how my then-current programming language of choice (Ruby) was showing itself to not be as future-proof as I would have liked it to be.
A lot has changed since then, but a lot has remained the same.
First: I have started a few open-source Elixir projects:
- Exfile — a file upload handling, persistence, and processing library. Extracted from an image upload service I’m working on (also in Elixir).
- multistream-downloader — a quick tool to monitor and download HTTP-based live streams.
- runroller — a redirect “unroller” API, see the blog post about it.
In most cases, languages like Go and Rust can push more raw performance out of a simple “Hello World” benchmark test — exactly what I initially did to compare Ruby on Rails to Elixir / Phoenix. The more I used Elixir, the more I gained an appreciation for what I now regard critical language features (yes, most of these apply to Erlang and other functional languages too).
This is a big one. Manipulating a mutable data structure is essentially changing the data in memory directly. This, however, is susceptible to the classic race condition bug programmers encounter when writing multithreaded programs:
- Thread 1 reads “1” from “a”
- Thread 2 reads “1” from “a”
- T1 increments “1” to “2” and writes it to “a”
- T2 increments “1” to “2” and writes it to “a”
In this case, the intended value is “3” because the programmer incremented “a” two times, but the actual value in memory is “2” due to the race condition.
In systems with immutable variables, this class of bug doesn’t exist. It forces the programmer to be explicit about shared state.
A typical Erlang (Elixir) node can have millions of processes running on it. No, these are not your traditional OS processes — they are not threads, either. The Erlang VM uses its own scheduler and thread pool to execute code. The memory overhead of a single process is usually very light — on my machine, it’s 2.6kb (SMP and HiPE enabled).
Another big one.
Want to send a message to another process?
Want to handle a message from another process?
receive do :hello -> puts "I received :hello" end
This works on processes that are running in the current node — but it also works across nodes. The syntax is exactly the same. The “pid” variable in the example is able to refer to a process anywhere in the cluster of nodes. The other node doesn’t even have to be Erlang, it just needs to be able to speak the same language, the distribution protocol and the external term format.
You can’t talk about Erlang or Elixir without bringing up OTP. OTP stands for “Open Telecom Platform” (Erlang was initially developed by Ericsson for use in telecom systems). OTP is a framework with many battle-tested tools to help you build your application — for example,
gen_server– an abstraction of the client-server model
gen_fsm– a finite state machine
supervisor– a supervisor process that automates recovery from temporary failures
OTP is, for all intents and purposes, part of the Erlang standard library. Thus, it is automatically included in any Elixir application as well. The nuts and bolts of OTP are out of the scope of this blog post, but having such a rich toolbox is like a breath of fresh air coming from Ruby (I thought the same when switching full-time from PHP to Ruby).
I’ve learned a lot in this past year, and yet I feel like I’ve only scratched the surface. Thanks for reading!