Mappa Via Marconi 20, Bussolengo (VR)
Email info@devinterface.com

Phoenix: the Elixir framework for high-performance applications

Phoenix logo and article title

Index

After having explored the Elixir language fundamentals in our previous articles "Introduction to Elixir Part 1" and "Introduction to Elixir Part 2," the time has come to deepen one of its strongest and most distinguished components: the Phoenix framework. 

If Elixir represents innovation in the world of functional programming, Phoenix amplifies its potential, making it an ideal tool for the development of high-performing web applications. 

Let's take a look at it. 

 

What is Phoenix

Programming languages are often defined by their ecosystem, including the available number of libraries and frameworks. This is also true for Elixir, where the Phoenix framework represents a key element in the contest of web frameworks. Phoenix is not only the main reference framework for Elixir, it also offers a wide range of functionalities that simplify the development of web apps, simultaneously allowing to create a more robust architecture. 

Phoenix is a web framework based on the MVC (Model-View-Controller) pattern. It operates on BEAM, the virtual machine that constitutes the OTP's heart. The framework's latest version - at the time of this writing - is 1.7.14, released in june 2024.

In other words, Phoenix is synonymous with productive, concurrent, beautiful, interactive, and reliable apps. In the next section, we'll analyse each of these qualities.

 

Phoenix advantages

Phoenix is one of the best-performing frameworks, thanks to its architecture based on Elixir and the BEAM VM (Erlang Virtual Machine). This combination offers a series of unique advantages:
 

Productivity

Phoenix makes developers productive, as it offers everything one expects from a web framework, that is:

  • a basic architecture for your application
  • a database access and management library to connect to the database
  • a routing layer to connect web requests to your code
  • a templating language and helpers for writing HTML
  • flexible and performant JSON encoding and decoding for external APIs
  • internationalisation strategies to take your application worldwide
  • all the breadth and power of Erlang and Elixir to grow

 

Functional programming: immutability

As we have seen in the articles dedicated to Elixir, one of the cardinal principles of functional programming is immutability. Imagine the following example:

map = %{key1: ‘value1’, key2: ‘value2’}
update_map(map)
map

In many programming languages, one cannot be sure that the map remains unchanged after calling the update_map function. This function could directly modify the existing map by altering its values. In Elixir, however, this is not possible. Maps, as well as other data structures, are immutable. Instead of modifying the original map, Elixir creates a new map with the desired changes.

This kind of approach forces the code to be organised into smaller, independent functions. Each function receives all necessary data as parameters and returns a new version of this data, without ever altering the original. This feature makes the code much easier to read and maintain, because there is no need to constantly track how and where variables are changed.

 

Concurrency

Concurrency has become a central theme in web application development. It concerns the ability of an application to handle multiple operations simultaneously, such as processing several web requests at the same time. For many languages and frameworks, handling concurrency can be complex and requires advanced mechanisms such as threads, locks, and mutexes, which can introduce synchronisation and performance issues. 

With Phoenix, thanks to Elixir and the BEAM virtual machine (inherited from Erlang), concurrency management is greatly simplified and highly efficient. BEAM is designed to support millions of lightweight processes that can run in parallel. All processes in Elixir are isolated, which means that a crash in one process does not affect the others, ensuring greater application stability and reliability.

A key aspect is that Phoenix allows this concurrency to be managed without the developer having to deal directly with the complexity of threading or synchronisation. Processes are handled automatically by BEAM, which takes care of balancing the load and distributing resources efficiently.

This concurrency capability makes Phoenix particularly suitable for applications that have to manage a large number of simultaneous connections, such as chat systems, streaming, or high-traffic e-commerce platforms. For example, a single instance of Phoenix can handle thousands of active WebSocket connections, allowing real-time updates without compromising performance.

 

Clean code

A major strength of Phoenix is its ability to promote the writing of clean, maintainable code by adopting the principles of functional programming. Elixir, on which Phoenix is based, supports data immutability and encourages the use of pure functions, which produce no side effects and always return the same output for a given input. This approach makes the code more predictable, easier to read, and safer, reducing the possibility of errors and making debugging more straightforward.

Besides its functional nature, Elixir introduces a number of tools that facilitate the writing of elegant code without compromising clarity. A key example is the use of macros, one of Elixir's hallmarks. Macros allow the language to be extended, adding functionality without compromising readability. Although macros may seem like an advanced tool, in Phoenix they are used to create pipelines and patterns that simplify the code structure, avoiding complexities such as chain inheritance found in other frameworks. This allows developers to concentrate on organising the logical flow of the application.

Furthermore, Phoenix allows code to be organised in pipelines, a powerful mechanism for applying a series of transformations or filters to HTTP requests. This allows groups of routes to be managed in an elegant and uniform manner without having to introduce complicated inheritance patterns or deep hierarchical structures. In practice, each group of routes can have a dedicated pipeline, thus keeping the code modular and easy to follow.

 

Interactive

One of the main advantages of Phoenix is its ability to handle interactivity in a highly efficient manner. With tools such as Phoenix LiveView and Phoenix Channels, developers can create interactive web applications without having to resort to complex technologies such as manually implemented JavaScript or WebSocket.

1) Phoenix LiveView allows responsive user interfaces to be built entirely on the server. This means that whenever the user interacts with the interface (e.g., clicking a button or editing a field), LiveView updates the page content in real time without having to reload the entire page or go through complex client-side scripts. This greatly reduces development time and facilitates maintenance, as there is no need to write custom front-end code for each interactive function.

2) Phoenix Channels, on the other hand, offer a native solution for implementing real-time functionalities such as chat or notifications. Channels use WebSocket to maintain open connections between client and server, allowing data to be sent and received instantaneously. This makes Phoenix particularly suitable for applications that require continuous updates, such as real-time dashboards or multiplayer gaming platforms.

 

Reliable

You can have the most powerful and responsive application you want, but if it is not reliable, it loses value. In Elixir, reliability is a cornerstone, inherited from its Erlang base. The secret lies in the way processes are structured and communicate with each other, allowing optimal management and supervision. Fully monitored concurrent processes can be started, and if one of them fails, Elixir is able to automatically restart it to the previous known state, along with any related processes that have been compromised.

The robust error management is ensured by the supervisor structure, which can monitor not only processes, but also other supervisors, creating a true supervision tree. Thus, the entire application is protected against isolated failures without having to write complex code. The good thing is that Phoenix, by default, already sets up much of the supervisor structure, facilitating the development of reliable applications without the need for manual configuration.

 

 

LiveView: real-time development

LiveView is an innovative technology born within the Elixir ecosystem for use with Phoenix. Its popularity and ability to simplify the development of interactive web applications have inspired emulation in other frameworks, such as Laravel's Livewire in the PHP community and Hotwire or StimulusReflex in the world of Ruby on Rails. The main feature that makes LiveView so attractive is the possibility of avoiding the massive use of JavaScript to handle client-side interactivity, thus reducing duplication of effort and facilitating development.

With LiveView, highly interactive user interfaces and real-time updates can be built directly from the server, using only Elixir and Phoenix. Each user interaction is sent to the server, which processes the request and sends back only the parts of the HTML that need to be updated. This process uses WebSocket to maintain an open connection between the client and server, ensuring immediate updates and smooth user experiences without the need to update the entire page or manage complex client-side scripts.

LiveView's server-side approach also offers important advantages in terms of maintainability and security. Since the application logic resides primarily on the server, it is possible to focus on a single codebase without having to synchronise logic between the front-end and back-end. Furthermore, the reduction of JavaScript minimises potential points of attack on the client side, thereby increasing the security of the application.

In contexts such as dashboards, content management systems, or e-commerce applications, LiveView makes it possible to manage dynamic real-time updates, such as automatic cart updates, notifications, and real-time chat, without compromising server or user interface performance.

 

Conclusion

As we have seen, Phoenix is an extremely powerful and versatile framework. Phoenix stands out for its ability to handle high-performance and scalable web applications, thanks to the solid base offered by Elixir and the BEAM virtual machine. Advanced features such as native concurrency, data immutability and a modular architecture allow developers to write clean and maintainable code, without sacrificing performance.

A major advantage of Phoenix is its simplicity in handling real-time interactivity through tools such as Phoenix LiveView. It eliminates the need to write and manage large amounts of JavaScript, allowing interactive web applications to be created directly from the server side. By dynamically and responsively updating interfaces, LiveView provides a smooth user experience, perfect for applications that require continuous updates, such as dashboards, chat systems, or e-commerce.

Both Elixir and Phoenix are part of our technology stack for developing high-performance and scalable web applications, harnessing the power of functional programming and the efficiency of native concurrency. Contact us if you wish to exploit the full potential of Phoenix and develop scalable, high-performance applications in Elixir.