Hyper Fast API Using Elixir & Phoenix


Just recently we've begun running tests with a new language called Elixir and we are very excited about it for a few reasons. The language is built on top of the Erlang VM & Open Telecom Platform (OTP), which is a highly proven network language that is used extensively in telecommunications. It also forms the foundation of popular apps like Facebook Chat and WhatsApp.

So why are we excited about this language here at Kettle? We see a few high level benefits that it brings to the web:

Concurrency: Elixir is designed around what they call a shared-nothing actor model, which allows it to efficiently take advantage of every available CPU core.
Hot Code Updates: rather than immediately killing pending requests during code updates, Elixir will gracefully handle these processes before it updates the code.
Fault-Tolerant: Elixir has built-in supervisors that restart parts of your program by reverting to a state that was known to be working.

Introducing Phoenix

As you can see, Elixir is a great choice when you need an extremely reliable and fast network application. There is a fantastic framework designed on top of Elixir called Phoenix. It ships with many of the essential tools needed for building a web application:

  • Familiar MVC structure making it easy and pleasant to work with
  • Database drivers via Ecto
  • HTTP server via Cowboy
  • Module for filtering requests and responses via Plug
  • Various tools for package management (hex) and unit testing (ex_unit)

Building a Scoring API

Today we want to give Elixir a try and build a simple application with it. Our goal is to build an API that streams player scoring data, to a leaderboard, in real-time. In this example we are going to use MySQL, but if you're looking for something ΓΌber fast, we recommend using MongoDB, which also has a native database driver included in Phoenix.

For this example, we will be using Phoenix V1.0.3.

Install Phoenix

There is an awesome getting started guide on the Phoenix website and we recommend following that. Here is a quick rundown of what you need to do. Make sure you have Homebrew on Mac installed first. Then install the prerequisite software:

$ brew install elixir
$ brew install erlang
$ brew install node
$ mix local.hex

After that, you can begin to install the phoenix framework:

$ mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v1.0.3/phoenix_new-1.0.3.ez
$ mix phoenix.new kettle_phoenix --database mysql 
## Note the database flag which defines MySQL as our DB driver
$ cd kettle_phoenix
$ mix ecto.create
$ mix phoenix.server

You should now see the Phoenix welcome screen at http://localhost:4000.

Create API Resources

Traditionally you would have to go through the process of creating a model, view and controller for your API to render. However, in Elixir there is a simple command to automate this process. It's called Mix.Tasks.Phoenix.Gen.Json and it will automatically create the following resources for you:

  • model in web/models
  • view in web/views
  • controller in web/controllers
  • migration file for the repository
  • test files for generated model and controller

Let's go ahead and run this command to create the necessary resources to render our player names and their scores in a JSON file:

$ mix phoenix.gen.json Score scores player:string score:integer

Add Routes

The terminal output from this command will return a new resource for the API scope, which belongs in web/router.ex. If you modified the data structure in the command above you will have a different output, however for this project we'll update our file to look like this:

defmodule KettlePhoenix.Router do  
  use KettlePhoenix.Web, :router

  pipeline :api do
    plug :accepts, ["json"]

  scope "/api", KettlePhoenix do
    pipe_through :api

    resources "/scores", ScoreController, except: [:new, :edit]


Connect Database

Next, we need to update the database connection details in config/dev.exs.

Then you can run the following commands to run the pending migrations:

$ mix ecto.migrate

At this point we should verify the database has been updated. If everything looks good we can seed some data into the database.

Go ahead and start up the Elixir server:

$ mix phoenix.server

We should see the endpoint serving up all player data at: http://localhost:4000/api/scores. If you'd like to insert data you can do that via a simple cURL call:

$ curl -X POST -H "Content-Type: application/json" -d '{ "score": { "player": "Jack Bauer", "score": "100" } }' http://localhost:4000/api/scores


While this was a quick and simple example, it should highlight how developer friendly Elixir is. With a relatively small amount of code we are able to build a very robust, scalable and fault-tolerant API. You could certainly add additional routes, with filters to sort and parse this data further.

We hope you enjoyed this article. Please feel free to leave comments below. The entire project is available on Kettle's GitHub.