Article
February 28, 2024 · 4 min read timeFor years, the golden standard in the JavaScript world for backend frameworks has been Express. There have been other options like Koa and Deno, but they haven’t received nearly the same popularity.
While Express comes with handy helpers for deploying to e.g. AWS Lambda-like serverless Express, the development side has stayed quiet for some time now. The last minor update for Express was made on April 25, 2022. Since then, two new updates have become available.
So, I was tempted to see if there are any newer alternatives out there with the same capabilities in AWS as Express. That’s how I became familiar with Fastify, which is advertised as the fastest JS backend framework in the business with built-in validation and serialization. Faster frameworks should theoretically lead to decreased costs in cloud environments, where customers pay for the resources they use.
I decided to test the hypothesis during my Nitor Core time that allows every Nitorean to use 10 % of their working hours for self-development.
Does Fastify walk the talk?
According to Fastify's benchmark tests, it is approximately four times faster than Express for simple 'hello world' responses. However, when I conducted tests with more complex POST requests involving database connections, the speed advantage reduced to about 2.5 times faster. It’s not a bad result either, and can be mainly attributed to Fastify's implementation of a JSON parsing library, fast-json-stringify, developed by their team.
I tested Fastify's built-in validation to see if it's adequate for an anti-corruption layer, which is used to validate requests and, potentially, responses between two bounded contexts or isolated systems. It turned out that the request validation capabilities are comparable to tools like Zod, and it achieves this using ajv.
It's important to note that Fastify uses the fast-json-stringify library for response serialization. This allows for defining mandatory and allowed fields, along with their types, by a schema. However, certain validations, like the “minLength” for strings in request validation, do not trigger errors in serialization even if the string length is shorter than the specified minLength.
Therefore, when using Fastify to implement an anti-corruption layer, while requests can be validated, any data enriched through a database connection must be validated prior to serialization. Alternatively, this data can be sent as a request to another endpoint for validation.
Using Fastify in AWS Lambda
The Fastify team has developed a wrapper called “aws-lambda-fastify" to help in running Fastify applications in AWS Lambda. This wrapper functions similarly to serverless Express. During testing, Fastify demonstrated better performance than Express, both in AWS ECS and Lambda environments. However, in the ECS context, this improved performance did not translate into cost savings for the test project, as it required the same instance size as Express.
The Fastify documentation suggests evaluating whether performance is a critical factor for a project, as Fastify may offer cost savings in certain scenarios. In AWS Lambda, a modest performance improvement was noted, primarily due to more efficient JSON parsing. Though the overall difference was minimal, it is suggested that with a sufficiently large load, Fastify could potentially offer more significant cost benefits compared to using Express and serverless Express.
The ongoing development offers pros and cons
One drawback identified during testing with Fastify is its ongoing development, particularly with the anticipated release of version 5 and associated deprecations. This evolving nature presents challenges in finding up-to-date tutorials and resources for troubleshooting – much of the available material quickly becomes outdated due to frequent version changes.
In comparison, Express, having been stable for a longer period, offers a variety of high-quality tutorials. Past updates to Fastify have often required refactoring of existing projects. It's likely that this trend will continue with the release of new versions in the near future.
Fastify also offers a broad range of plugins, functioning similarly to Express middleware but with greater flexibility. These plugins facilitate features like next.js-style routing using a folder structure, database client integration, and simple CORS configuration. The Fastify-cli tool is particularly useful for quickly setting up a Fastify project, and it comes with several useful plugins pre-installed. But, of course, Express still has a more extensive collection of middleware and extensions at the moment.
Conclusions
After testing Fastify, I can say it does seem like a good and potentially more efficient alternative to Express for both on-premises and AWS deployments. However, the performance gains may vary across different projects and should be verified case by case.
While new releases of Fastify might require project refactoring, this also indicates its ongoing development, in contrast to Express, which appears to be on hiatus. Fastify's built-in request validation and response serialization also contribute to faster project setup, especially when using Fastify-cli.
To sum it all up, Fastify is fast indeed, but potential cost-savings rely on the nature of the project at hand. There is no real difference between Fastify and Express in how to deploy to AWS, and the existing features seem good enough for most use cases, at least. That said, the active development will likely cater to any future needs. As Express isn’t really moving forward, Fastify seems like a sound option in the long run.