Choosing a PHP Framework for Microservice Architecture

Written by sercit | Published 2023/08/08
Tech Story Tags: php | laravel | software-architecture | microservices | symfony | microservice-architecture | monoliths-to-microservices | phalcon

TLDRThe choice of framework depends on the requirements for each service. For microservices, the requirements are different from those for a monolith. A framework comparison table can be found in the article. At the end of the article you can find out which php frameworks are the best for microservices todayvia the TL;DR App

Due to the popularity of microservice architecture, a large number of teams are faced with the need to choose a framework. Because most projects on the web are written in PHP, teams often decide to break up an existing monolithic service in the same programming language to save time. In this article, I propose to go through all the modern frameworks in PHP and tell you how suitable one or the other is for microservices.

Requirements

First of all, we need to decide on the requirements we have for our future services. Each team has different priorities for choosing a framework. If we look at the requirements for a classic service, these would be:

  • Framework features
  • Framework support and frequency of updates
  • Security
  • Performance
  • Community

When looking at the requirements through the lens of microservices, the requirements shift from framework capabilities and update frequency to speed and the ability to scale the service quickly and conveniently.

Since the required framework capabilities differ from service to service, let's look at the list of requirements needed in any microservice:

  • Speed of response
  • Speed and ease of deployment
  • Scalability
  • Extensibility
  • Community

The microservice architecture itself involves splitting the responsibilities of a service into smaller ones, which allows the requirements for each service to be divided separately.

Due to the fact that microservices assume some small set of responsibilities in each service, the application does not need to load all the features of the framework, but only the necessary parts, which leads to the acceleration of the service. Thus, the main requirement for classical, monolithic applications is not relevant for microservices.

Frequency of updates and especially security patches are important for services that work with clients, for example, for a microservice that proxies requests from clients and directs them to the right services. At the same time, with Gateway implementation, all other services already consider the request within the architecture as safe and can reduce the response time by doing without additional checks. The specifics of the architecture implementation may vary depending on the team and project.

In my opinion, performance is the most important point when choosing a framework for microservices. Since several services are often required to work together to generate responses, each service should respond as fast as possible so that the total response time to the user does not differ much from the response time of a monolithic application. Also, performance problems can cause difficulties in scaling due to unnecessary resources consumed.

The advantage of a microservice architecture is the ability to scale only the necessary parts of the application, which allows for great savings on resources and reduced response time. In order for this advantage not to turn into a disadvantage, we, when choosing a framework, should also think about the scalability and speed of service deployment.

It is worth noting the extensibility of the framework as one of the requirements when choosing a framework, because often some frameworks, especially microframeworks, provide a very limited set of features, and the tasks for such services can be very different and require an additional set of functionality, for example, work with queues or protocols.

As for the community - it is always an important parameter when choosing a framework, regardless of whether it is a monolith or a microservice, but it should not be the main and only criteria of choice.

Let's take a look at the solutions we have to choose from.

Solutions

According to JetBrains research, frameworks by popularity are arranged in this order:

I decided to research popular frameworks since I said that community plays an important role when choosing a framework. If we exclude CMSs that are incorrectly compared to frameworks, we get a list of 6 frameworks

  • Laravel
  • Symfony
  • CodeIgniter
  • Yii
  • Slim
  • CakePHP

I also think that we should add one more framework, which although not yet popular has very good speed and is increasing its popularity - Phalcon.

Laravel

The author of the most popular PHP framework is Taylor Otwell. For its more than 10-year history, it has turned into a full-fledged ecosystem with the framework at its head. Among the pluses of Laravel, it is worth mentioning the beginner-friendly documentation and project structure, ecosystem, where you can find everything, including the work with application deployment. However, Laravel has some disadvantages, such as not having the fastest request processing and difficult upgrade to a new version. Within the Laravel ecosystem, there is also a special micro-framework called Lumen.

Lumen

Is a scaled-down version of Laravel, which turns off most parts of the framework core that are not necessary and only add some convenience, such as built-in authorization or event model. In recent years, Lumen has not added new functionality, but only updated the version following its big brother.

Symfony

Developed in 2005 by SensioLabs, this framework has a good code base and follows SOLID principles in many ways. Symfony code is used in many other libraries and frameworks, such as Laravel. Given its relatively high entry threshold, Symfony developers, on average, tend to possess more advanced skills compared to their Laravel counterparts. This heightened skill level often facilitates quicker resolution of queries. However, it's important to acknowledge certain drawbacks. Symfony doesn't exhibit the swiftest response times, and its documentation, while comprehensive in elucidating intricate details, can prove intricate for newcomers to the framework, leading to potential confusion during initial interactions.

CodeIgniter

Crafted for speed and efficiency, the framework, introduced in 2006 by Phil Sturgeon, boasts excellent documentation and an active community. It offers a streamlined collection of built-in features and a malleable architecture, empowering project tailoring to individual requirements. However, a drawback lies in the relatively limited number of third-party packages that seamlessly integrate with CodeIgniter out of the box.

Yii

Qiang Xue introduced the inaugural version of Yii in 2008. Renowned for its exceptional extensibility, Yii stands out with numerous optimizations that position it as a top-tier, swift framework within the market. Notably, Yii offers an extensive array of built-in functionalities, encompassing code autogeneration and robust ORM capabilities. Nevertheless, it's worth noting that the Yii documentation occasionally falls short of fully elucidating all features. This shortfall is mitigated by an engaged community that provides assistance. Additionally, Yii presents a somewhat constrained assortment of libraries and extensions, alongside an initial lack of comprehensive out-of-the-box support for REST APIs.

Slim

Released by Josh Lockhart in 2010, this framework is considered to be one of the fastest in PHP. Because of its simple structure, it is well-suited to simple tasks, but the smaller number of components inside often forces developers to implement the functionality themselves. The implementation itself in the project is not difficult, but the small community does not give the opportunity to reuse the ready-made solution.

CakePHP

Another framework launched in 2005. The author is Larry Masters. The framework provides a good structure for the application, but difficulties occur in getting the team to learn the rigid design of the application. It is an extensible framework. On the downside, it does a poor job of updating the PHP version and requires more memory.

Phalcon

The youngest and fastest framework among the ones described in this article. Its first version was released in 2016 under the authorship of Halcyon Labs and has not yet been properly distributed. The main distinguishing feature is that it is written as a C extension for PHP. The priority of the developers was less server load and speed of response. Due to the specifics of the implementation - Phalcon is hard to set up and also requires additional adaptation for those team members who do not know C.

Comparison

Due to the fact that each framework has its own pros and cons, sometimes it can be difficult to choose a particular option. In order to avoid problems in choosing a framework just because of the qualifications available on the team, it is necessary to better understand the differences between frameworks. The table can help with this:

Requirements

Laravel/Lumen

Symfony

CodeIgniter

Yii

Slim

CakePHP

Phalcon

Speed

Medium/Fast

Medium

Fast

Fast

Fast

Medium

Very Fast

Deployment

Medium/Medium

Medium

Fast

Medium

Fast

Medium

Slow

Scalability

Bad/Good

Normal

Bad

Bad

Good

Normal

Normal

Extensibility

Good/Good

Good

Bad

Good

Normal

Good

Normal

Community

Big

Big

Small

Big

Normal

Small

Small

Features

A lot

A lot

Not a lot

A lot

A few

Not a lot

Not a lot

Updates

Regular/No updates

Regular

Regular

Non-regular

Regular

Non-regular

Regular

Documentation

Good/Good

Normal

Good

Bad

Good

Normal

Good

Conclusions

As you can see from the table, despite the popularity of some frameworks, it is very difficult to choose one option. When choosing a framework, it is important to realize that the choice depends on the specific requirements for each service and the choice cannot depend only on the speed of operation or the experience of the team. An important factor for microframeworks should be extensibility. Microservice architecture does not restrict the choice of a separate framework for each microservice, but it is worth highlighting that this practice can lead to potential errors due to incompatible requests and responses from scratch.

It is up to each team to choose which framework to write on. In my opinion, the best solution will be one framework from Phalcon/Lumen/Slim. Phalcon will give more speed but will make you struggle with infrastructural problems. Lumen, despite the convenient syntax that came from Laravel, is still as fast as Slim, but deploying Lumen is not as convenient and easy. Slim, on the other hand, provides quite a bit of functionality, but if you need a simple service that will do things quickly, Slim is a good option.

All other frameworks also have their advantages, but they are more suitable for full-fledged large services, for example, for monoliths.

The lead image for this article was generated by HackerNoon's AI Image Generator via the prompt "Cloud computing"


Written by sercit | Senior Software Engineer
Published by HackerNoon on 2023/08/08