Hyper Fast API Using Elixir & Phoenix

Introduction

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:

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:

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:

Conclusion

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.