Fastify vs ExpressLast updated on September 21, 2022APIInfrastructure
Fastify vs ExpressExpress is one of the most popular web frameworks in the world. It is simple, minimalist, and easy to setup. Express is known to be thin and performant, however, what if there was a faster, less resource intensive web framework?What is Fastify?Fastify is a high performant web framework that is focused on providing low overhead and a powerful plugin architecture. Fastify is easy to use to create a new web app, or to switch to from another framework.Core Features:- Highly performant
- Extensible
- Schema based
- Logging
- Developer friendly
- TypeScript ready
Fastify PerformanceFastify as a library functions very similarly to express, except, it can handle five times the requests per second, according to the official Fastify benchmark.Fastify is able to handle 600k req/sec in this benchmark versus Express sitting at around 130k req/sec.
Generally, Fastify is a lot less popular than Express. Many people still choose express because of its wide variety & thoroughly tested suite of plugins and add-ons. With Fastify, a common complain is that there are not as many native Fastify plugins compared to express.However, the team behind Fastify also created fastify-express, which is a composability layer to Fastify which allows compatibility with any express plugin. However, using this plugin will show decreased performance, and should not be used as a long term solution. Instead it should be used to help transition over from Express to Fastify.Fastify At ModuleAt Module, we initially built our Web API using Express, but switched to Fastify early on. After switching, we saw: noticeably lower response latency with large responses, more consistent latency during times of traffic, and generally the ability to handle more requests per second before eating up all the resources provided to the container. The switch was relatively easy due to Fastify Hooks and Fastify Plugins.When integrating Fastify into our infrastructure, we built our own custom plugins. Our goal was to keep code minimalistic and efficient, aiming to maximize performance and keep latency low. In return, we are allowed to scale with less force and lower overall server costs.Some custom plugins we created to better integrate with our overall infrastructure are:- Custom Smart Caching with K8 Redis Clusters
- Custom Rate Limiting Depending on a variety of factors
- Request Logging and Direct Database Management
In the future, we will be discussing these custom plugins on their own and why we built them, how they work, and how you can integrate it into your app.