Playing with Phoenix Framework – Rails’ younger brother

phoenix_blogpost

Have you heard about Phoenix? It’s a new web framework written in Elixir language. And Elixir itself is a Ruby inspired language that runs on Erlang Virtual Machine. Yes, a lot of ties and references, but we are all one big family here, aren’t we?

Let’s see how closely related are Phoenix and Rails. We’ll do it all from scratch on OS X, with some help from Homebrew package manager.

The Environment

We’ll start with installing both languages that the frameworks rely on. Just like Ruby, Elixir already has several version managers to choose from, like exenv and kiex. But we won’t use them for the sake of simplicity.

Ruby installation:

Elixir installation:

Next thing we need is a package manager. Just like Ruby has its Gems, Elixir’s package manager is called Hex. Equivalent for Ruby’s Rake is Elixir’s Mix. Hex + Mix combined give us Ruby’s Bundler dependencies management functionality.

So we use Mix task to install Hex package manager:

The Foundation

We don’t do it often, so let’s recall how to install the Rails framework:

Phoenix framework installation is no harder:

We’re ready to create a new application. In Rails it’s nothing more than running everyone’s most beloved command:

The command created new app with its directory structure, and then Bundler downloaded and installed all dependencies.

Let’s see how Phoenix deals with the task:

Wow! New application created, dependencies downloaded. Beautiful.

To test our sample application we have to start a web server.

A Rails server:

And Phoenix server:

GET /hello_phoenix

Both frameworks are MVC based, so our main building blocks are models, views and controllers. Just like Rails, Phoenix has generators for those:

vs.

…but this is the easy way. I want to compare how to add the base features by hand, just to figure out the differences.

We’ll create sample page displaying a welcome message.

First things first, we’ll add a route for it. With Rails, we create new route by editing config/routes.rb file:

In Phoenix we have web/router.ex:

Now, controllers time!

Within Rails app create app/controllers/hello_controller.rb file:

Within Phoenix create web/controllers/hello_controller.ex file:

For the view we’ll use Rails’ erb and Elixir’s equivalent: eex.

For Rails we only need to create one file: app/views/hello/index.html.erb

Phoenix requires us to create a view module beside the template. It’s (more or less) like a view helper in Rails. It will sit in web/views/hello_view.ex:

The accompanying template file will be web/templates/hello/index.html.eex:

Run servers once again. Go to the browser, type http://localhost:3000/hello for Rails and http://localhost:4000/hello for Phoenix. It just works! Do you have this feeling again? 🙂

Rising from the ashes

Who doesn’t like benchmarks and numbers? So here they are. Rails app was benchmarked with Passenger in production environment. Phoenix app was running with built-in cowboy server. The machine was Macbook Pro (2014, 2.6GHz i5, 8GB RAM).

Rails:

Phoenix:

Note that it is just a simple benchmark with empty controller. You have been warned 🙂

Oh, there is one important thing. Phoenix is Elixir, and Elixir is not Ruby. It looks a lot like Ruby but there are differences.

What else? In both frameworks’ controllers we have a params hash to manage url params. Error pages are very similar. There are more equalities. Asking for models? Rails has ActiveRecord, Phoenix has ecto. Both support various database drivers, tasks for managing the database and its schema through migrations. Both frameworks ship with built-in testing frameworks.

If you know Rails, you already are familiar with Phoenix. It is still very young, so you have the chance to be an early adopter, and I highly encourage you to play with it while it’s hot!

UPDATES

  • Phoenix benchmark updated. Now server is running in production mode
  • Use wrk instead of ab

Michał Kalbarczyk

Ruby on Rails developer at EL Passion. A software engineer since I got my first Atari computer. Get in touch with me.

  • stevegrossi

    I knew Phoenix was inspired by Rails, but your side-by-side comparison shows just how similar they are. I hope that inspires some Rails developers to give Phoenix and the Elixir language a try—I’ve certainly enjoyed using it. Great post!

  • Peter

    “ab” literally is incapable of stressing Phoenix enough, so your measurement is still distorted even after turning on Production mode. Try adding “wrk” results for both!

    • fazibear

      You’re right. Use wrk instead of ab. Thanks for feedback.

  • http://www.stevedomin.com Steve Domin

    Is Phoenix running in production environment as well? If not protocols consolidation should give you a nice perf boost

  • maintheme

    Installing phoenix does not work for me if you have a clean installation.

    mix archive install phoenix
    * hex.ez
    Archives installed at: /Users/username/.mix/archives

    But it does not install phoenix.

    mix phoenix.new Desktop/phoenixapp
    ** (Mix) The task phoenix.new could not be found