What WhatsApp and Discord Know that You Don't: The Power of Elixir

What WhatsApp and Discord Know that You Don't: The Power of Elixir

Worker Pools of Thousands on your Fingertips.

Are you a fan of Java or Python? As a CS student, I was too. But then I recently decided to learn Elixir, a functional programming language that's been used in large-scale production apps like WhatsApp and Discord. In this blog post, I'll be sharing my journey of learning Elixir and comparing its functional programming approach with the traditional object-oriented programming that you know and love. So, let's dive in!

Ten Thousand Threads

One of the standout features of Elixir is its ability to handle large-scale systems with ease. Fact is, spawning threads in Elixir is so lightweight that it has an overhead of only a few hundred bytes, which is crazy lower than other popular languages like Java or C++. This means that it's common to have tens of thousands of threads running concurrently in Elixir without any significant performance impact. This makes Elixir an ideal choice for building highly concurrent production systems that need to scale quickly and efficiently. Fun fact: Elixir was stress tested with 2M concurrent threads on one machine, and it handled all of them with ease. Let's see Java do that.

A different approach to writing code:

Before I dive into my experience of learning Elixir, let's take a quick look at the core differences between object-oriented and functional programming.

Object-oriented programming is based on the concept of objects that interact with each other to perform tasks. Objects have data and behavior, and the interaction between objects is controlled by methods. This approach is useful for creating real-world simulations and solving complex problems.

Functional programming, on the other hand, focuses on the evaluation of functions and avoids changing state and mutable data. It is better suited for more mathematical and logic-based problems, and programs written in functional languages tend to be more concise and easier to reason about. Functional programming has the ability to handle complex systems with ease and it often leads to more maintainable and testable code.

"Functional programming is like describing your problem to a mathematician. Object-oriented programming is like giving directions to a robot." - Evan Czaplicki

My Experience of Learning Elixir

Elixir is a functional programming language based on the Erlang virtual machine, and it was designed to be scalable, fault-tolerant, and easy to use. As a student developer with experience in object-oriented programming, I found the syntax and the approach to programming quite different from what I was used to.

One of the first things I noticed was the use of pattern matching instead assignment. In Elixir, you don't assign values to variables as you do in other programming languages. Instead, you use pattern matching to bind values to variables. Here's an example

# Assigning a value to a variable
num = 10

# Using pattern matching to bind a value to a variable
[head | tail] = [1, 2, 3, 4, 5]

Same thing in C++:

vector<int> original = {1, 2, 3, 4, 5};
int head = original[0];
vector<int> tail = new vector<int>(original.begin()+1, original.end());

Yes, C++ looks messy. Welcome to functional programming.

Another thing that was different was the use of the pipe operator which makes it easier to chain multiple functions together. Here's an example:

# Without the pipe operator
String.upcase(String.reverse("hello world"))

# With the pipe operator
"hello world" |> String.reverse() |> String.upcase()

Elixir also has a built-in tool called iex which stands for Interactive Elixir. It's a command-line tool that allows you to interact with your Elixir code in real-time. This makes it easy to test your code and explore the language features without having to write a full program.

A feature I found very cool in Elixir was the ability to define functions with multiple clauses. This allows you to define different behaviors for the same function based on input parameters. Here's an example:

defmodule Math do
  def add(a, b), do: a + b
  def add(a, b, c), do: a + b + c
end

Final Thoughts

Learning Elixir was a great experience, and it has taught me a whole new approach to programming. While object-oriented programming may be better suited for some problems, functional programming has its merits as well. Programs written in Elixir tend to be more concise and easier to reason about, and the functional approach to programming can lead to more elegant solutions to complex problems.

If you're a student developer like me, don't be afraid to step out of your comfort zone and try learning a new programming language. You'll be able to broaden your skill set and open up new opportunities for yourself.