Why We Use Elixir, and Some of What We’ve Done With It

Since 2015 we’ve had a small but active team of Elixir engineers at FanDuel. FanDuel first adopted Elixir in order to build a prototype for a new product. This product is likely one of the biggest Elixir projects anywhere, and, although it’s difficult to gauge how many companies are using a given technology, there are indications that FanDuel was one of the language’s early adopters.

Elixir is a functional programming language with a syntax more reminiscent of Python or Ruby than C or Haskell. This may or may not intrigue you. However, what makes Elixir special is the fact that it runs on the Erlang VM (BEAM) and can call Erlang code directly, which gives the language a substantial headstart over other new languages. Erlang first appeared at Ericsson thirty years ago (evidence) as a proprietary language for telephone switching, and the attending maturity and feature-richness of the language are a boon to Elixir programmers. Add the concurrency and fault tolerance that are Erlang’s specialties (see the “Platform Features” section of the Elixir home page), and you have an excellent foundation indeed.

There’s ample reason to gush about what Erlang brings to Elixir (and we’ll do more of that soon), but Elixir has much to offer in its own right. What often appeals to newbies right away is the familiar syntax:

defmodule Math do
def sum(a, b) do
a + b

Compare this to Erlang’s unique syntax:

sum(A,B) ->
A + B.

There is a common misconception that Elixir is nothing more than “syntactic sugar” for Erlang. Elixir brings plenty more substantial features to the table, each of which would require its own blog post to properly explore:

  • A full-fledged web framework called Phoenix, which introduces (among other things) a transport-agnostic API for two-way communication between server and client, called ”Channels”
  • Metaprogramming with macros
  • The |> (pipe) operator, which chains function calls together. A minor but endlessly useful addition.
  • Mix: Elixir’s build and deployment tool
  • A standard library which does more than just wrap that of Erlang (see: the Enumerable protocol)
  • Ecto: a domain-specific language for database abstraction

FanDuel’s Open Source Elixir Projects

Both of FanDuel’s open source Elixir libraries take advantage of existing Erlang code, and serve to illustrate much of what makes us so excited about the language.

ExVmstats, which periodically reports BEAM statistics to StatsD, is a port of an existing Erlang library. It is a fairly simple tool that originally addressed a monitoring need in one of our Elixir products, but it is now in the top fifteen percent of packages by download count on Hex.

Our newest contribution to Elixir, jsonrpc2-elixir, enlists the help of several Erlang projects. This is a library for using JSON RPC 2.0 in Elixir, including a server and client for both HTTP(S) and TCP. (Users can also provide their own transports as well as serializers, if they prefer.) Erlang battle-tested answers for each of these needs:

  • Ranch and Shackle provided us with a high-performance TCP server and client, respectively.
  • Cowboy combined with the Elixir middleware Plug for a fast, lightweight HTTP(S) server.
  • Hackney gave us a simple HTTP(S) client.
  • Two serializers are available out of the box. By default jsonrpc2-elixir uses an Elixir library called Poison, but a Jiffy serializer is also included.

The ability to use these libraries certainly made building jsonrpc2-elixir much easier. But it also resulted in a fast and reliable tool for other Elixir developers — not just because these dependencies have seen many tweaks and revisions from contributors, but also because each of them exists as a supervised process in our application tree, and therefore takes advantage of Erlang’s built-in concurrency and fault tolerance. Anyone who uses the package can be reasonably sure that it won’t cause any unexpected performance issues or application-wide crashes.

Since there’s no need to fuss about transports or serializers, other than by adding a few lines to your app’s configuration, getting jsonrpc2-elixir up and running requires very little “boilerplate code”. Using excerpts from the examples here, let’s see how we would set up a simple TCP server and client for JSON RPC 2.0. In this example, we’ll have the client pass a name parameter to the server, and the server will respond with “Hello, #{name}!”.

Assuming that both Ranch and Shackle have been added as dependencies (see the README), let’s start by making a handler module for the server to use. (Notice that there are no raw JSON strings in any of the example code:jsonrpc2-elixir serializes and deserializes Elixir terms behind the scenes.)

defmodule Handler do
use JSONRPC2.Server.Handler
  def handle_request("hello", [name]) do
"Hello, #{name}!"

Then we’ll start the server, passing it the handler and a port:

# Start the server (this will usually go in your OTP application's start/2)
JSONRPC2.Servers.TCP.start_listener(Handler, 8000)

Now we’ll define the client, including a Client.start/2 function that starts a client pool:

defmodule Client do
alias JSONRPC2.Clients.TCP
  def start(host, port) do
# Start a client pool named __MODULE__, or the current module's name, connected to `host` at `port`.
TCP.start(host, port, __MODULE__)
  def hello(name) do
TCP.call(__MODULE__, "hello", [name])

Then we actually start the client pool:

# Start the client pool (this will also usually go in your OTP application's start/2)
Client.start("localhost", 8000)

With the server and client pool both started, we can test our JSON RPC 2.0 implementation:

# Make a call with the client to the server
IO.inspect Client.hello("Elixir")
#=> {:ok, "Hello, Elixir!"}

Voila! We’ve written very little code to set up a TCP server and client, and a real-world app may be implementing only one or the other. This, more or less, represents the upshot of both Elixir’s relationship to Erlang and of a tool likejsonprc2-elixir: productivity. And because Elixir boasts an enthusiastic and welcoming community, jsonrpc2-elixir is far from the only tool that helps to save developers time and keep frustration at bay.

We really like Elixir at FanDuel, and we’ve had great success with it. If you’re interested in writing Elixir at FanDuel, check out the job posting here, and apply!

Greg Narajka, Software Engineer

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.