See all articles

Phoenix is not Rails - common misconceptions and practical examples

Web application development is a field that offers many options, starting with frameworks like Rails and Phoenix. It’s time to take a closer look at the latter, as it empowers Elixir developers to build a high variety of web apps. And there are some misconceptions about it we’d like to clear up.

Before we discuss where Phoenix shines and which businesses can benefit from using it, let’s establish what Phoenix is. It’s a web framework for Elixir, which is a modern functional language that emphasizes efficiency and fault-tolerance. Phoenix is one of the most popular Elixir frameworks out there - it could be equated to Ruby on Rails in the Ruby world.

Phoenix lets developers work without thinking too hard about certain things early in the project by giving them a number of useful features out of the box. The team isn’t forced to go down any particular path (an example of framework railroading: authorisation and authentication in Django). They get tools and parts to build an app with, and can use them as they like. With Phoenix, they don’t need to remove anything along the way, because they are never forced to add components in the first place. Phoenix is a complete framework, suitable for building an app of any size.

Phoenix is much less opinionated than its competition. It has sane defaults, but does not try to force it’s own way of doing things on every corner (like Rails sometimes does - which can be a good thing when development needs to move fast, but is also somewhat limiting). For a counterexample with Phoenix, let’s take a look at the Ecto library, which Phoenix uses by default as a database abstraction layer. Ecto can be easily removed or exchanged for a similar tool. It bundles another library, which can be used separately from Phoenix. It’s not an integral part of the framework and can be used freely. Coupling between Ecto and Phoenix is very loose.

The unique advantages of Phoenix

The ease of building an app’s frontend with it

Once the core business logic for your app is ready, it’s not always a simple matter to add a web interface to it. At least not without complications. But “Phoenix is not your app”, meaning that you can add Phoenix, a web faced library, as your frontend very late in the development process.

To be clear: you can (and should) write your application independently from outer world interfaces. You can easily add Phoenix to an existing application whenever you feel the time is right, because it is just a library. In contrast, Rails has a strong preference for presenting your application like it’s something added to the Rails code structure. In some cases, that might mean giving up too much control. Phoenix can do the same thing if that’s what you want, but unlike Rails, adding Phoenix as a web interface is always simple, no matter how complex your application is.

Flexibility

Phoenix doesn’t force the development team to write their app in any particular way and empowers them to build it out of the parts they like.

Elixir’s advantages - concurrency and efficiency

Because Phoenix is written in Elixir, it gets all of the language’s inherent benefits. Elixir boasts a high concurrency, which means it can handle many HTTP connections without relaying on a connection pool. If we add a preemptive scheduler to that, guaranteeing that each connection will be given same amount of CPU time, we’ll have a solution that won’t allow for long requests to choke the server. The result? No dropped connections. The only limit is the hard limit of the infrastructure’s resources, like the amount of RAM (and these can easily be adjusted as necessary).

Scalability

The efficiency explained above makes Phoenix projects extremely scalable. Scalability issues appear much later in Phoenix apps than in apps using other frameworks - and once we reach that ceiling, we can easily transfer the app to a more powerful machine, because Phoenix can efficiently use all of the resources it’s given.

Fault tolerance

Systems built with Phoenix and Elixir recover from any mistakes very easily. We can write a high-uptime app, even reaching the 100% availability goal, without adding any external libraries or tools. It’s just how Elixir is built. It allows for hot-upgrades, which means you can fix or update an app in real-time without breaking any connections, and without the need to restart the application. Some businesses have no use for this, but apps like Discord are based on this, and they can have tens of millions of connections with a very high uptime.

Separation between business logic and users

A comfortable separation between business logic and web interaction (the users) helps maintain high code quality and can speed up problem solving.

Common misconceptions about Phoenix

Phoenix is slow

If you look at micro benchmarks, you may come to this conclusion. Remember, however, that micro benchmarks don’t actually check anything practical (like the response time of an empty web page). But if you use real world scenarios, the tools that looked better in the benchmarks turn out to be slow, while Phoenix doesn’t - without any extra work required.

Phoenix and Elixir are similar to Rails and Ruby

Some people who look at Phoenix and Elixir code think it’s similar to Ruby. They’re not entirely wrong: the syntax is similar. The semantics, however, are not, and they are what determines how the system works. Developers sometimes end up surprised by how such superficially similar languages require completely different models of thinking.

Elixir is a new and untested language

Elixir definitely is a modern language, but it has a long history. Elixir compiles to Erlang byte code, and is run on the Erlang virtual machine: BEAM. In fact, Elixir introduces a new syntax and macros, but the semantics and underlying systems have been in use for more than last 20 years on production environments all over the world - Elixir builds on what was tested and proven to work in Erlang. This means that Elixir should not be perceived as a new language. It’s a bundle of tried and true concepts and tools in a new packaging.

How businesses can leverage Phoenix

Perhaps some real world examples of where Phoenix shines can give you a better idea of the type of projects it’s best used for. You might have heard of Discord, an online communication platform targeted mostly at the video game community. It allows users to form groups and channels, talk using text, voice and video, and it might just be the most popular tool for in-game voice communication. At one point, Discord counted 5M concurrent users and 1M events per second on their platform. They only started to have trouble at 30K concurrent users per server. That’s a pretty big achievement if you ask us - and it was possible thanks to Elixir in Discord’s stack.

Example number two is Moz, a SEO company offering tool built by industry experts. Because of how Elixir works and how it makes it easy to create and manage threads for the Erlang virtual machine, Moz were able to limit the number of their machines and have very simple yet robust persistence layer. Before that, they had problems with their databases and concurrency due to sheer size of data. They switched to in-memory datasets and data archived in compressed csv’s on AWS’s S3. The benefits of this approach include more speed, less storage space, and lowered response times for users without compromising the safety of data. As a result, Moz don’t need to constantly connect to the database for updates and can enjoy shortened reaction times, which translate directly into better UX.

Conclusions

Overall, we believe Phoenix is a fantastic choice for many companies developing web apps. SaaS businesses can take advantage of Elixir’s concurrency, giving them the ability to fix one client’s issue without disrupting others. Companies that need to run many connections at once or want to scale rapidly in the future are also likely to enjoy using Phoenix. If you’re wondering whether the framework might suit your business, talk to us - we’re always happy to help clients make the right choice for their business.

If your web app needs high concurrency, fault tolerance, speed and scalability, Phoenix may be your best option.

Talk to industry experts and experienced Elixir developers about leveraging this technology to boost your business.

Similar articles