Phoenix framework - the world's most loved web framework based on Elixir
Almost every programming language has at least one web framework in its ecosystem. In such a crowded space, the Phoenix web framework consistently ranks as the most admired in the world. My ambition with this article is to explain why it became the top pick in web development. Interested?
Modern web frameworks - what are the expectations?
Web development evolved a lot since the beginning of the internet. At first, it was all about PHP since it was the easiest programming language to learn (due to rich learning materials) and build something quickly. Then, as soon as the backend (the logic) and frontend (the interface) became more advanced, more and more patterns emerged. These patterns were turned into libraries and concepts that eventually became web frameworks.
Web frameworks, at first, were all about productivity. It's very simple to describe - the more productive you are, the faster you reach the market with fewer costs. To give you an idea of this feature's importance, the Ruby on Rails web framework, even though based on a not-so-fast and not really scalable Ruby programming language, at some point became the top choice for startups willing to build PoC (Proof of Concept) or MVP (Minimum Viable Product). Why? Because it was and still is productive.
To succeed in the long term, a web framework nowadays has to be more than just productive. Web apps became huge and complex while their user's demand consistently grew. Here is what is to be expected from a modern web framework.
As a developer or decision-maker, you expect:
- Productivity. Web framework has to be more than just productive, but it's still, I believe, the most important characteristic that decision-makers and programmers are looking for. Developing web apps is more fun and faster if you work with a productive tech stack.
- Scalability. Apart from hobby projects and internal tools, every new project starts with the goal of hitting the scale at some point. If the programming language and its framework are not easy to scale at the maintainable cost level, it may result in project failure.
- Maintainability. Thinking long term, if a web application is supposed to be developed for years, it has to be maintainable. Therefore, web frameworks should be designed to follow best practices that lead to fewer architectural design mistakes.
- Rich ecosystem. If you can stick to an ecosystem built around one technology, building a strong, cohesive team around this tech stack is much easier.
As a user, you expect:
- Real-time experience. One thing will always be true - time is precious, and as humans, we expect not to waste time whenever possible. Modern web frameworks should be able to deliver a reliable real-time experience for users.
- Reliability and availability. Users, customers, or simply visitors to your web application expect your app to respond. Bugs will always happen, but the unavailability of your server is unacceptable. Modern web frameworks should be fault-tolerant and support high availability.
Let's keep those features in mind as we move on to the history of the Phoenix framework.
Phoenix framework - a modern web development based on the Elixir programming language
Chris McCord created the Phoenix framework in 2014 when he struggled to effectively develop scalable and reliable real-time applications in Ruby on Rails (source). As mentioned before, Ruby on Rails is a very productive web framework, but it was not well suited for this type of app due to how the Ruby language works.
He searched for a programming language that would be scalable and reliable by design, and that's how he learned about Erlang - a battle-proven functional programming language that powered this kind of application for decades.
Erlang was not productive, though, and working with Ruby teaches you one thing - you don't want to lose the pleasure of productive coding. Fortunately, at that time, José Valim built Elixir - a highly productive programming language based on Erlang VM (Erlang Virtual Machine).
My goal with this article is to describe the Phoenix framework and concentrate less on Elixir itself. Therefore, if you'd like to learn more about what Elixir offers, I encourage you to read why this functional programming language is so admired in our "Elixir programming language guide" article.
Since playing around with Elixir was a very enjoyable experience for Chris McCord, he decided to build the Phoenix framework based on the Elixir programming language. If you're interested in seeing one of his first conference talks, where he introduces one of the first iterations of the Phoenix framework, you may want to check out this talk from 2014.
Phoenix framework core features
It's not about just one feature that makes the Phoenix framework so great, but rather the sum of all. There are quite a few of them, so I strongly encourage you to read about them all.
Phoenix framework is productive
Chris McCord borrowed some good concepts from Ruby on Rails. Elixir programming language is productive by itself, therefore, mixing those two resulted in a framework that is productive to work with. But what makes the web framework productive after all?
It's easy to start with Phoenix project
A core premise of Phoenix is "peace of mind, from prototype to production", and there are a couple of reasons why:
- Well-written and well-structured documentation that covers all building blocks of this framework.
- Generators that allow you to generate the main components of web applications.
Good tooling, including:
- debug tools,
- integrated, built-in testing framework,
- monitoring tools,
- package manager,
- code completion in IDE.
- Elixir programming language simplicity that stands out with its readable and straightforward syntax.
- Incredible community support. Elixir & Phoenix community is well known for its friendly environment, where you can quickly solve your problem (easy or hard).
As a company, we introduced Elixir and Phoenix to developers who have never heard of this technology. About 80% of them were able to learn Elixir and Phoenix and build their first CRUD application with tests in just two or three weeks. It's a very short period of time if you consider that it includes learning a new programming language, a new web framework, and building the application.
Elixir & Phoenix have rich open-source libraries directory
Since 2012 (the year Elixir was introduced), the open-source library directory has grown significantly, and at the moment of writing this article, I can safely say that it's very mature.
Almost any library you may need already exists for Elixir. There is a curated list of libraries grouped by categories available on Github where you can verify this claim.
There is one exception that's worth mentioning - third-party API wrappers. In this case, you may encounter some missing official libraries; however, from my perspective, these are very fast to develop on your own.
Build fast and scale fast with Phoenix
Productivity is not only about building MVP (Minimum Viable Product) in a matter of weeks. It's also about being able to scale as soon as needed without compromising the development speed. Scalability and ability to build distributed systems are the Elixir programming language's core promises, and Phoenix also inherits this characteristic.
Web apps built in Phoenix framework scale very well
There is no question that scalability in Phoenix apps comes down to the Erlang VM (Virtual Machine) that Elixir is based on. Here are the reasons why Phoenix apps built on top of Elixir scales so well:
- In a typical setup, a Phoenix application will automatically utilize multiple cores without the developers needing a specific configuration. Since each HTTP request/response cycle is a new Elixir process, it means that the multiple processes will be spawned and distributed across multiple cores, making the hardware utilization efficient.
- Elixir programming language makes it easy to build distributed systems. Each node, regardless of its localization, can communicate with another one. This means that you can distribute server infrastructure wherever you want at ease.
To scale your web application, you can leverage horizontal and vertical scaling. Elixir's lightweight processes and built-in support for message passing make it ideal for horizontal scaling. Since Elixir programming language utilizes machine cores effectively, it also allows you to postpone the need to perform vertical scaling, corresponding to server upgrades with more computing power.
Phoenix framework architecture promotes good practices
Phoenix follows an MVC (Model-View-Controller), which is a well-known pattern used with success in other frameworks. The only difference is how it approaches the business logic layer.
Although Phoenix allows you to organize the code as you wish, the suggested solution to model data manipulation is through Contexts, a DDD (Domain Driven Design) inspired way of structuring your application business logic. If followed properly, this approach leads to the long-term maintainability of the project.
No web framework or programming language can make your project maintainable unless you follow good patterns that simply prove to work. Fortunately, Phoenix supports these patterns well, and fortunately, at Curiosum, we discovered those and shared our findings in:
- Context maintainability & guidelines in Elixir & Phoenix article.
- How to compose and refactor Ecto queries with Queries Modules article.
Phoenix framework has a world-class ecosystem
Elixir and Phoenix ecosystem grows at a very high pace. I wrote an article about the Elixir programming language where I shared why its ecosystem should be perceived as a benefit of using this functional programming language. Since most of the information shared in that article would be a copy here, let me just point out the most important projects:
- Numerical Elixir. Elixir approach to AI and ML, offering various numerical computing libraries. You can check our tutorial on this topic in "How to create neural network in Elixir using Nx and Axon" article.
- Nerves. This framework allows you to develop IoT software with Elixir. We introduced Nerves in the "How to program an IoT device in Elixir using Nerves?" article.
For web development, an especially interesting project in the Elixir and Phoenix ecosystem is Phoenix LiveView. This framework is a consolidation of all benefits of Erlang VM in a modern approach to building responsive, real-time applications.
Real-time user experience through Phoenix Channels
A key feature and often selling point of the Phoenix framework is its real-time communication capability via Channels. Phoenix Channel provides an efficient, persistent WebSockets connection between clients and the server, ideal for dynamic user experiences.
- Interactivity: Enables features like live chats and real-time notifications.
- Efficiency: Reduces server load by eliminating constant polling.
- Scalability: Leverages Erlang VM's capability to handle numerous connections simultaneously.
What could be the use cases of channels? Here are a few:
- Live message boards, chat systems.
- Real-time updates in sports or events.
- Collaborative tools similar to Google Docs.
Setting up Channels is straightforward, with Phoenix handling complexities like connection retries and presence tracking. This makes it an excellent choice for applications demanding real-time interactivity and performance. You can also mix it with Phoenix LiveView (which is actually based on the same mechanism).
Phoenix Framework reliability and availability thanks to Elixir
Since Phoenix is based on the Elixir programming language and, most importantly, on Erlang VM, it's reliable and offers high availability.
Of course, if you produce a faulty implementation, the error still occurs, and Phoenix won't correct those errors on its own. However, in this case, thanks to the processes management on Erlang Virtual Machine, if there is an error or a given process dies due to unexpected circumstances, it's automatically restored in a typical application supervision tree setup. There are known scenarios of the Erlang system running for years without interruption, and Phoenix, of course, inherits that characteristic.
Functional programming language as the architecture core
Lastly, programming Phoenix-based web applications is a pleasure because of its function-based design.
The whole request-response cycle is wrapped in
conn- a special structure that is a part of the
Phoenix.Conn module. Everything that happens during this cycle (authorization, authentication, or simply rendering) will modify the
conn structure that, in the end, is formatted and returned as a response to the client. It makes you think of web applications as a set of layers, where each layer has an effect on the final result. It turns out that it's a very effective and productive way of thinking about web applications.
Elixir functional paradigm also leads to fewer black box patterns, where implementation is hidden from the developer. Although the black box pattern may be more productive in the short term, it eventually leads to a lot of time spent debugging, understanding, or rewriting the code in the long term. Phoenix framework balances it well, favoring an approach where the developer is aware of what's happening in the code.
Shining in the web development space is not about just productivity anymore. The requirements are growing, and only the web frameworks that offer "all in one" package can distinguish themselves from the other.
Phoenix framework has it all, which is why it's ranking on top of the most loved web frameworks in the world. I strongly encourage you to start a new phoenix project, and see for yourself.
If you're still considering this or wondering if Elixir and Phoenix is a good match for your next big project or company, we offer consultation and a team of Elixir and Phoenix experts who can help you build amazing web apps. Let's keep in touch!