Fastify – framework overview, implementation, professionals, and cons
Net frameworks make constructing Node.js internet purposes simpler. They take the guesswork out of dealing with requests and HTTP strategies. They supply dynamic HTML rendering, templates, and architectural patterns that make growing apps simpler. Over time, Categorical turned the Node.js internet framework of alternative. However is it all the time one of the best one? The creators of Fastify imagine that it’s not the case. I used to be capable of expertise Fastify first-hand throughout a current venture. Do I aspect with them or not?
Only a whereas in the past, my Node.js group and I labored on a venture of a reward system for gamers of varied free-to-play on-line video games. The consumer wished to revamp the entire app dramatically. For all intents and functions, we had been to develop a complete new model of the system.
One of many major venture necessities was growing the velocity of the system. The app had lots of customers who carried out lots of actions. The requests per second metric was of explicit significance.
Our pure framework of alternative for Node.js-based was Categorical. It’s simple to study, very properly documented, and extremely versatile. However since velocity was alleged to be such a giant issue, we determined to take a better have a look at different choices with a view to make a correct velocity comparability.
Quickly sufficient, we began contemplating a framework that bears a reputation that’s meant to make you immediately consider velocity – Fastify.
What’s Fastify?
Fastify is a Node.js library that was created by Matteo Collina and David Mark Clements in late 2016. The newest model out there’s 4.7.0. If you’re within the launch historical past, test the details about long-term help and corresponding launch notes.
Fastify was born out of a want to create a general-purpose internet framework that would supply an incredible developer expertise with out compromising on throughput and efficiency.
Fastify is downloaded greater than 700,000 instances every week. At this second, it’s utilized in about 25,000 tasks. Numerous corporations are already on board, together with giants equivalent to Microsoft. Listed here are a number of the companies that use Fastify.
Fastify efficiency – velocity take a look at
When the consumer expressed their curiosity in prioritizing software velocity, we determined to check out Fastify. They are saying it is among the quickest internet frameworks. Naturally, we couldn’t decide it up on popularity alone.
We tried to seek out as many analysis sources on the subject of Fastify as attainable. Then, we determined to run exams on our personal.
To that finish, we used the wg/wrk benchmarking software. We in contrast Fastify and Categorical in 10 impartial exams. We anticipated Fastify to be quicker, however we wanted detailed data to determine whether or not altering the Node.js framework is well worth the bother.
The comparability included a variety of components:
- requests per second,
- variety of requests served in 30 seconds,
- switch per second,
- common latency.
Right here is our Fastify take a look at information:



In each single take a look at, Fastify proved superior. On common, the framework carried out about 25 p.c higher than Categorical.
Our findings each stunned and impressed us. They influenced our resolution to modify to Fastify. Nonetheless, they had been hardly the one issue.
In an effort to clarify it absolutely, let’s take a better have a look at the framework.

Why use Fastify?
Based mostly on our observations, Fastify’s benefits will be grouped into 4 classes.
Efficiency
We already talked about velocity. However Fastify can be extremely scalable, making the framework appropriate for each small and enormous tasks. The JSON format routinely analyzes Fastifiy’s capabilities, guaranteeing quick routing of information packets throughout the community. That’s what makes the framework lightning-fast.
Maintainability
Fastify is a low overhead internet framework, which minimizes upkeep prices for the complete software. Along with that, it’s an online framework extremely targeted on safety that ensures a system of automated safety and information validation.
Flexibility
Fastify helps TypeScript, cooperates with AWS Lambda, and communicates with its API by way of a GraphQL adapter. In consequence, it’s nice for internet software improvement in addition to API interface improvement.
Ease of improvement
One in every of Fastify’s greatest professionals in serverless purposes is the benefit of improvement. In your native atmosphere, you’ll all the time run the Fastify software straight, with out the necessity for any extra instruments, whereas the identical code will likely be executed in your serverless platform of alternative with an extra snippet of code.

Do you assume that Fastify might be the know-how to your subsequent venture?
Why don’t you contact us to seek the advice of it?
Do you need to effectively deal with your server’s sources whereas serving the best variety of requests attainable? Do you need to do all that with out sacrificing safety validations and useful improvement?
Fastify was made for this situation.
The venture I discussed earlier than is an ideal instance of such a situation. The consumer anticipated the app for use by an infinite variety of folks. And since we wished the brand new model to be a particular improve of model one in relation to efficiency, altering Categorical to Fastify was a logical step.
When not to make use of Fastify?
Naturally, all the pieces has its professionals and cons – Fastify is not any exception. As of 2022, the framework just isn’t broadly used within the trade. It’s being more and more adopted, however its use continues to be modest when in comparison with Categorical or Nest.
Along with that, being a reasonably younger venture, the documentation just isn’t as expansive as you would possibly anticipate from extra mature frameworks. The neighborhood help is so-so. For those who plan on utilizing a library equivalent to passport.js, you will see that that it doesn’t work with Fastify. Make it possible for Fastify is appropriate with the entire different items of software program you need to use.
If you’re an inexperienced developer searching for a job, you would possibly discover that only a few employers are presently searching for devs that know Fastify particularly. Consequently, you could be higher served going for one thing extra mainstream.
Nonetheless, for those who already know Categorical, studying Fastify can provide you extra choices to make use of in your venture and supply with you a special perspective on internet improvement in Node.js. Its structure actually stands out and that alone makes it price trying out. However extra on that later.
Fastify sensible tutorial
For those who assume that Fastify is one thing for you, stick round as a result of we’re going to exhibit how one can implement it in order that it could possibly help your personal venture.
Implementation
We’re going to make a easy Fastify venture. The implementation of Fasitfy is kind of just like what you might need skilled with Categorical.js.
Let’s begin with the implementation of fundamental modules: router, app, and server.
To do that, we’re going to recreate three recordsdata:
- server.js – the primary script of the appliance,
- app.js – the script through which we create a Fastify Node server occasion,
- router.js – Fastify router occasion that helps us encapsulate routes and plugins.
We’re going to create a complete of 4 ts recordsdata within the course of: server.ts, app.ts, router.ts, and routing.ts.
For Server.ts, we merely use our createApp operate from app.ts. We begin by defining a port on which the server will likely be listening. The file additionally features a easy instance of the way to use Fastify’s logger.
The app.ts file is the center of our code. Right here, we create the createApp operate that initializes the Fastify occasion. Right here, we additionally register plugins by way of app.register (the instance proven registers cors, helmet, and swagger). The router can be registered right here.
The router.ts defines the way in which through which the consumer requests are dealt with by the appliance endpoints. The router gathers routings for all of the options. Within the instance beneath, we register usersRouting within the router. We cross the router to app.js. That’s additionally the place we register it.
There may be additionally routing.ts. It handles routing for customers. We use it to outline each endpoint associated to customers. The instance reveals how one can outline the get endpoint.
Fastify’s plugins
Some of the vital modifications Fastify brings is changing middleware with plugins. Fastify actually does have a strong plugin structure. What are these plugins?
A Fastify plugin could be a set of routes, a server decorator, or just about something. Let’s say we have to ship queries to the database from numerous software modules. Do we have to hook up with the database, export the connection, after which import it into any modules we have to use it in? This works however leads to spaghetti code.
That is the place the Fastify plugin system shines probably the most. It permits us to inject our dependencies right into a Fastify occasion after which use them wherever we’ve got entry to the occasion. It additionally helps to simply transfer from a monolithic construction to microservices since every service could be a standalone plugin.
The Fastify ecosystem of plugins is ever-growing. There’s in all probability already a plugin to your favourite database or template language and even performance.
Listed here are a few examples:
- cors -> Fastify-cors,
- helmet -> Fastify-helmet,
- express-jwt -> Fastify-jwt,
- multer -> Fastify-multer,
…. and plenty of extra!
Here’s a full checklist of all core Fastify plugins.
Getting probably the most out of Fastify
Fastify additionally has a spread of additional options that can help you do much more.
Validation
Fastify makes use of Ajv below the hood, which lets you outline validation guidelines with JSON schema; Be taught extra about how validation works in Fastify.
Logging
Fastify makes use of a quick and versatile logger: pino. The logger occasion is on the market on the Fastify server occasion (equivalent to Fastify.log.information(“…”)) and on all Request objects (equivalent to request.log.information(“…”));
Logger utilization is easy. You simply must allow it as a result of it’s disabled as default:
Then, you should utilize it like this:
Error dealing with
Fastify gives a setErrorHandler() methodology, which lets you explicitly specify a operate for error dealing with. For extra, you’ll be able to specify totally different Fastify error handlers utilizing plugins.
Fastify tries to catch as many uncaught errors as it could possibly with out hindering efficiency. This consists of:
- synchronous routes: app.get(‘/’, () => { throw new Error(‘kaboom’) })
- async routes: app.get(‘/’, async () => { throw new Error(‘kaboom’) })
Decorators
They permit us to customise core Fastify objects such because the server occasion itself and any request and reply objects used through the HTTP request lifecycle. The decorators API can be utilized to connect any kind of property to the core objects, e.g. capabilities, plain objects, or native varieties.
Fastify studying sources
Need to proceed your analysis into Fastify? There are lots of fascinating sources on the market that may enable you to out:
- Absolutely, you’ll want to begin with the official Fastify web site, which has the docs, benchmarks, and extra.
- The package deal supervisor for Node.js has a Fastify web page with tons of helpful data, together with variations, dependencies, guides, and whatnot.
- The go-to place for the newest contributions to the Fastify framework codebase.
Migrating from Categorical to Fastify
- A really helpful and sensible article for everybody who needs emigrate from Categorical to Fastify in an current venture. It’s a extremely really useful learn.
Categorical vs Fastify efficiency comparability
- This text has a really fascinating collection of efficiency information for Categorical and Fastify.

Fastify and internet improvement developments
Fastify is consistently evolving. The Fastify framework is written in vanilla JavaScript, and as such kind definitions are usually not as simple to take care of; nevertheless, since model 2 and past, maintainers and contributors have put in an incredible effort to enhance the categories.
Fastify kind system
The kind system was modified in Fastify model 3. The brand new kind system introduces generic constraining and defaulting, plus a brand new strategy to outline schema varieties equivalent to a request physique, question string, and extra!
Because the group works on enhancing framework and sort definition synergy, generally elements of the API won’t be typed or could also be typed incorrectly. That’s why it’s enormously appropriate with Typescript.
Fastify – conclusions & classes discovered
Contemplating simply how a lot builders love Categorical, one would possibly ask – do we actually want yet one more internet framework for Node.js?
My private opinion is a convincing: YES. Wholesome competitors all the time spurs progress, resulting in a unending drive in the direction of quicker, higher, and extra environment friendly options.
Fastify definitely has the makings of an incredible framework. I already take into account it to be an incredible alternative for each internet improvement and API improvement, for a variety of causes:
- It’s quick, economical, and helps TypeScript out-of-the-box. The latter is a giant bonus for giant business tasks.
- It really works nice with quite a lot of modern applied sciences widespread in modern-day internet improvement, equivalent to AWS Lambda, or GraphQL.
- It performs to its strengths, specializing in dealing with the sources of your server effectively and quick
Nonetheless, good outdated Categorical continues to be right here and is one thing all of us can depend on. On the finish of the day, there’s no good cause to completely restrict your self to 1 framework, and shun all of the newcomers. However, you shouldn’t out of the blue go right into a frenzy of refactoring each internet software to Fastify both.
Within the close to future, it’s secure to say that Fastify won’t overtake Categorical when it comes to recognition (Categorical with its 25 million downloads is big), however I feel that it’s going to definitely develop, and until some new “competitor” is discovered, it might at some point change into as common as Categorical. It definitely feels prefer it has all it takes.
I hope that by the point you begin your subsequent Node.js venture, you’ll have performed sufficient analysis into Fastify to make an knowledgeable alternative on whether or not it’s a good match for the event!
Node.js is a key know-how within the improvement of microservices
Be taught extra concerning the newest microservices developments from the State of Microservices report