Thoughts On Elixir: Pros And Cons Of The Most Popular Tool For High-Load Dev
Why is Elixir/Phoenix achieving such a high rate of adoption in the software development industry? What are the best use cases of this language? Are there any drawbacks when using it? We talked to Sergiy Kukunin, a full-stack developer at Spotlight and an Elixir expert, to find answers to these and other questions.
Ivan: why is Elixir becoming so popular?
Sergey: Elixir features principles that are rarely met in other languages. Also, it is based on Erlang which is used for building telecommunication networks. As a result, we have a highly reliable tool for high-load projects that is proven over time.
This is essential, as in recent years hardware technological progress is slowing down. The performance of one core of the CPU has reached the physical limit of 5 GHz. The only thing manufacturers have come up with to increase the performance of the hardware is to use more and more cores.
The problem with this approach is that you need software that is capable of working with multiple cores in parallel. The classic programming languages are poorly suited for such tasks. When talking about multithreading in, say, C, we know that there is shared memory and several threads that may compete for resources. In such conditions, it is tough to create a reliable code for high-load projects because of random race conditions and segmentation faults.
Elixir is effectively solving this problem by introducing a new approach to multithreading.
What are the core advantages of this technology?
The first essential characteristic of the language coming from its functional nature is immutability. Once we've created and loaded the data structure (there are no objects in functional languages), we are unable to alter it, except by recreating it from scratch again.
On the one hand, this peculiarity lifts the entry threshold for using Elixir, as you can't pick one part of an application and try to record something there repeatedly, and end up with everything up and running. On the other hand, this makes the engineer plan an app's architecture more thoroughly.
The Elixir virtual machine is literally a mini-OS, where each process has its own memory space. No other process can get into it, borrowing its resources. All a process can do is send a message to another process, with no control how it will process the message. Also, as it is a functional language, there are no data structures that cannot be transmitted between processes, even over the network (unlike the approach used in the object-oriented languages).
It is harder to write Elixir code due to the multiple restrictions the runtime applies, and it takes longer than composing something using Java. However, the overall reliability of an application is very high.
Are there any alternatives to Elixir?
The most similar approach is the coroutines used in the Go language. Also, Python’s greenlets and fibers in Ruby provide a similar approach, but there is no runtime as robust and fast available to run them. The developer should decide how to run these coroutines, while Erlang handles that all itself.
Any tool has its drawbacks. What are they for Elixir?
Elixir today is a production-ready tool, but the overall language ecosystem is still a bit too young. In particular, this means you cannot just log in to Stack Overflow and find an answer to any of your possible questions, as you can do for on Ruby, for example. Yes, there are books, courses, and some blogs, but you will need to continually conduct your research and experiments to solve a lot of everyday tasks. This is the main drawback for me.
Also, it is harder to test Elixir apps as unit tests are less helpful, and you need to focus more on integration ones. However, you should remember that almost all Elixir functions are clean, which makes unit tests simpler: there is a single output for each input.
Could you describe the case when using Elixir provides maximum results?
If you do not have any real need for supporting high-loads with millions of requests processed by your app, then Elixir is not a tool you should bother using. However, Elixir is a perfect solution for a real high-load projects. You may not even write all your app using it, but using Elixir for the hottest parts of a system will always be a good idea.
In the age of a dynamic internet when users expect very high speeds and an immediate response by any website, especially content-related resources. If you, say, have an online media outlet with lots of concurrent connections from thousands of readers who want to receive news updates instantly, then moving to Elixir will make your life a whole lot easier.
Personal experience: on one project we had a Node.js web app that was capable of maintaining up to 20,000 connections at one time. After moving to Elixir, this number grew to 80,000 with no changes in the business logic or smth. This result was possible solely because of the better efficiency of the language we used.
However, developers should understand that there is no silver bullet. Elixir cannot give you more than the Erlang virtual machine was initially entitled to. It just translates new syntax so that the old-yet-good compiler created by Ericsson in the late 90’s is able to understand it. This is a perfect example of how time-proven technology combined with a new one can open up new possibilities for developers, but there are always some constraints.
What is your advice when starting to use Elixir? What technology should one study to get up and running faster?
Well, the syntax of Elixir has some things in common with Ruby. The languages are entirely different, but it is always good to see symbols and elements you are used to. The simplest thing is to use some of the new Elixir-compatible web-development frameworks. The most popular web framework for Elixir is Phoenix. You should definitely give it a try, especially if you are used to using Ruby on Rails. This will simplify development while still making the app faster and more reliable.