Fastify – framework overview, implementation, pros, and cons (2024)

Web frameworks make building Node.js web applications easier. They take the guesswork out of handling requests and HTTP methods. They provide dynamic HTML rendering, templates, and architectural patterns that make developing apps easier. Over the years, Express became the Node.js web framework of choice. But is it always the best one? The creators of Fastify believe that it’s not the case. I was able to experience Fastify first-hand during a recent project. Do I side with them or not?

Just a while ago, my Node.js team and I worked on a project of a reward system for players of various free-to-play online games. The client wanted to revamp the whole app dramatically. For all intents and purposes, we were to develop a whole new version of the system.

One of the primary project requirements was increasing the speed of the system. The app had a lot of users who performed a lot of actions. The requests per second metric was of particular importance.

Our natural framework of choice for Node.js-based was Express. It’s easy to learn, very well documented, and highly flexible. But since speed was supposed to be such a big factor, we decided to take a closer look at other options in order to make a proper speed comparison.

Soon enough, we started considering a framework that bears a name that’s meant to make you instantly think of speed – Fastify.

What is Fastify?

Fastify is a Node.js library that was created by Matteo Collina and David Mark Clements in late 2016. The latest version out there is 4.7.0. If you are interested in the release history, check the information about long-term support and corresponding release notes.

According to the creators:

Fastify was born out of a desire to create a general-purpose web framework that would provide a great developer experience without compromising on throughput and performance.

Fastify is downloaded more than 700,000 times a week. At this moment, it is used in about 25,000 projects. A lot of companies are already on board, including giants such as Microsoft. Here are some of the businesses that use Fastify.

Fastify performance – speed test

When the client expressed their interest in prioritizing application speed, we decided to try out Fastify. They say it is one of the fastest web frameworks. Naturally, we couldn’t pick it up on reputation alone.

We tried to find as many research sources on the topic of Fastify as possible. Then, we decided to run tests on our own.

To that end, we used the wg/wrk benchmarking tool. We compared Fastify and Express in 10 independent tests. We expected Fastify to be faster, but we needed detailed information to decide whether changing the Node.js framework is worth the trouble.

The comparison included a number of factors:

  • requests per second,
  • number of requests served in 30 seconds,
  • transfer per second,
  • average latency.

Here is our Fastify test data:

Fastify – framework overview, implementation, pros, and cons (1)

Fastify – framework overview, implementation, pros, and cons (2)

Fastify – framework overview, implementation, pros, and cons (3)

In every single test, Fastify proved superior. On average, the framework performed about 25 percent better than Express.

Our findings both surprised and impressed us. They influenced our decision to switch to Fastify. However, they were hardly the only factor.

In order to explain it fully, let’s take a closer look at the framework.

Why use Fastify?

Based on our observations, Fastify’s advantages can be grouped into four categories.

Performance

We already talked about speed. But Fastify is also highly scalable, making the framework suitable for both small and large projects. The JSON format automatically analyzes Fastifiy’s functions, ensuring fast routing of data packets across the network. That’s what makes the framework lightning-fast.

Maintainability

Fastify is a low overhead web framework, which minimizes maintenance costs for the entire application. In addition to that, it’s a web framework highly focused on security that guarantees a system of automatic security and data validation.

Flexibility

Fastify supports TypeScript, cooperates with AWS Lambda, and communicates with its API via a GraphQL adapter. As a result, it is great for web application development as well as API interface development.

Ease of development

One of Fastify’s biggest pros in serverless applications is the ease of development. In your local environment, you will always run the Fastify application directly, without the need for any additional tools, while the same code will be executed in your serverless platform of choice with an additional snippet of code.

Fastify – framework overview, implementation, pros, and cons (5)

Do you think that Fastify could be the technology for your next project?

Why don’t you contact us to consult it?

Book free consultations

Fastify – framework overview, implementation, pros, and cons (6)

Do you want to efficiently handle your server’s resources while serving the highest number of requests possible? Do you want to do all that without sacrificing security validations and handy development?

Fastify was made for this scenario.

The project I mentioned before is a perfect example of such a scenario. The client expected the app to be used by an enormous number of people. And since we wanted the new version to be a definite upgrade of version one when it comes to performance, changing Express to Fastify was a logical step.

When not to use Fastify?

Naturally, everything has its pros and cons – Fastify is no exception. As of 2022, the framework is not widely used in the industry. It is being increasingly adopted, but its use is still modest when compared to Express or Nest.

In addition to that, being a fairly young project, the documentation is not as expansive as you might expect from more mature frameworks. The community support is so-so. If you plan on using a library such as passport.js, you will find that it doesn’t work with Fastify. Make sure that Fastify is compatible with all of the other pieces of software you want to use.

If you are an inexperienced developer looking for a job, you might find that very few employers are currently looking for devs that know Fastify specifically. Consequently, you might be better served going for something more mainstream.

However, if you already know Express, learning Fastify can give you additional options to use in your project and provide with you a different perspective on web development in Node.js. Its architecture really stands out and that alone makes it worth checking out. But more on that later.

Fastify – framework overview, implementation, pros, and cons (7)

Fastify practical tutorial

If you think that Fastify is something for you, stick around because we’re going to demonstrate how you can implement it so that it can support your own project.

Implementation

We’re going to make a simple Fastify project. The implementation of Fasitfy is quite similar to what you might have experienced with Express.js.

Let’s start with the implementation of basic modules: router, app, and server.

To do this, we’re going to recreate three files:

  1. server.js – the main script of the application,
  2. app.js – the script in which we create a Fastify Node server instance,
  3. router.js – Fastify router instance that helps us encapsulate routes and plugins.

We’re going to create a total of four ts files in the process: server.ts, app.ts, router.ts, and routing.ts.

For Server.ts, we simply use our createApp function from app.ts. We start by defining a port on which the server will be listening. The file also includes a simple example of how to use Fastify’s logger.

The app.ts file is the heart of our code. Here, we create the createApp function that initializes the Fastify instance. Here, we also register plugins via app.register (the example shown registers cors, helmet, and swagger). The router is also registered here.

The router.ts defines the way in which the client requests are handled by the application endpoints. The router gathers routings for all the features. In the example below, we register usersRouting in the router. We pass the router to app.js. That’s also where we register it.

There is also routing.ts. It handles routing for users. We use it to define every endpoint related to users. The example shows how you can define the get endpoint.

Fastify’s plugins

One of the most important changes Fastify brings is replacing middleware with plugins. Fastify really does have a powerful plugin architecture. What are those plugins?

A Fastify plugin can be a set of routes, a server decorator, or pretty much anything. Let’s say we need to send queries to the database from various application modules. Do we need to connect to the database, export the connection, and then import it into any modules we need to use it in? This works but results in spaghetti code.

This is where the Fastify plugin system shines the most. It allows us to inject our dependencies into a Fastify instance and then use them anywhere we have access to the instance. It also helps to easily move from a monolithic structure to microservices since each service can be a standalone plugin.

The Fastify ecosystem of plugins is ever-growing. There’s probably already a plugin for your favorite database or template language or even functionality.

Here are a couple of examples:

  • cors -> Fastify-cors,
  • helmet -> Fastify-helmet,
  • express-jwt -> Fastify-jwt,
  • multer -> Fastify-multer,

…. and many more!

Here is a full list of all core Fastify plugins.

Getting the most out of Fastify

Fastify also has a range of extra features that allow you to do even more.

Validation

Fastify uses Ajv under the hood, which allows you to define validation rules withJSON schema; Learn more about how validation works in Fastify.

Logging

Fastify uses a fast and flexible logger: pino. The logger instance is available on the Fastify server instance (such as Fastify.log.info(“…”)) and on all Request objects (such as request.log.info(“…”));
Logger usage is simple. You just need to enable it because it is disabled as default:

Then, you can use it like this:

Error handling

Fastify provides a setErrorHandler() method, which allows you to explicitly specify a function for error handling. For more, you can specify different Fastify error handlers using plugins.
Fastify tries to catch as many uncaught errors as it can without hindering performance. This includes:

  1. synchronous routes: app.get(‘/’, () => { throw new Error(‘kaboom’) })
  2. async routes: app.get(‘/’, async () => { throw new Error(‘kaboom’) })

Decorators

They allow us to customize core Fastify objects such as the server instance itself and any request and reply objects used during the HTTP request lifecycle. The decorators API can be used to attach any type of property to the core objects, e.g. functions, plain objects, or native types.

Fastify learning resources

Want to continue your research into Fastify? There are a lot of interesting resources out there that can help you out:

Official Fastify website

  • Surely, you need to start with the official Fastify website, which has the docs, benchmarks, and more.

Fastify npm

  • The package manager for Node.js has a Fastify page with tons of useful information, including versions, dependencies, guides, and whatnot.

Fastify’s Github

  • The go-to place for the latest contributions to the Fastify framework codebase.

Migrating from Express to Fastify

  • A very useful and practical article for everyone who wants to migrate from Express to Fastify in an existing project. It’s a highly recommended read.

Express vs Fastify performance comparison

  • This article has a very interesting selection of performance data for Express and Fastify.

Fastify – framework overview, implementation, pros, and cons (8)

Fastify and web development trends

Fastify is constantly evolving. The Fastify framework is written in vanilla JavaScript, and as such type definitions are not as easy to maintain; however, since version 2 and beyond, maintainers and contributors have put in a great effort to improve the types.

Fastify type system

The type system was changed in Fastify version 3. The new type system introduces generic constraining and defaulting, plus a new way to define schema types such as a request body, query string, and more!

As the team works on improving framework and type definition synergy, sometimes parts of the API will not be typed or may be typed incorrectly. That’s why it is greatly compatible with Typescript.

Fastify – conclusions & lessons learned

Considering just how much developers love Express, one might ask – do we really need yet another web framework for Node.js?

My personal opinion is a resounding: YES. Healthy competition always spurs growth, leading to a never-ending drive towards faster, better, and more efficient solutions.

Fastify certainly has the makings of a great framework. I already consider it to be a great choice for both web development and API development, for a number of reasons:

  • It’s fast, economical, and supports TypeScript out-of-the-box. The latter is a big bonus for large commercial projects.
  • It works great with a variety of innovative technologies common in modern-day web development, such as AWS Lambda, or GraphQL.
  • It plays to its strengths, specializing in handling the resources of your server efficiently and fast

However, good old Express is still here and is something we all can rely on. At the end of the day, there’s no good reason to permanently limit yourself to one framework, and shun all the newcomers. On the other hand, you shouldn’t suddenly go into a frenzy of refactoring every web application to Fastify either.

In the near future, it is safe to say that Fastify will not overtake Express in terms of popularity (Express with its 25 million downloads is enormous), but I think that it will certainly grow, and unless some new “competitor” is found, it may one day become as popular as Express. It certainly feels like it has all it takes.

I hope that by the time you start your next Node.js project, you’ll have done enough research into Fastify to make an informed choice on whether it is a good fit for the occasion!

Node.js is a key technology in the development of microservices

Learn more about the latest microservices trends from the State of Microservices report

I want to learn more

Fastify – framework overview, implementation, pros, and cons (9)

Mariusz Poręba

Node.js developer

Mariusz’s been working in IT for about 2 years, The Software House being his first job as a Node.js developer. He has a knack for learning new skills, which extends far beyond software. He loves playing chess and other board games. Tea > coffee.

I'm an experienced Node.js developer with a deep understanding of web frameworks, particularly Express and Fastify. My expertise extends beyond theoretical knowledge, as I've actively worked on projects that demanded high-performance web applications. One notable example involves the development of a reward system for players of free-to-play online games.

In this project, the client emphasized the need for increased system speed, given the large user base and high volume of actions performed by users. Traditionally, Express had been our go-to framework for Node.js, thanks to its ease of learning, extensive documentation, and flexibility. However, due to the project's focus on speed, we decided to explore alternatives, leading us to adopt Fastify.

Our decision to switch was not arbitrary; it was based on thorough performance testing. We used the wg/wrk benchmarking tool to conduct 10 independent tests comparing Fastify and Express. The results were compelling—Fastify consistently outperformed Express, demonstrating approximately 25% better performance on average. This empirical evidence influenced our choice to adopt Fastify for the project.

Fastify, created by Matteo Collina and David Mark Clements in 2016, has gained significant traction in the Node.js community. With over 700,000 weekly downloads and adoption by major companies, including Microsoft, Fastify has proven its reliability and speed.

Fastify's strengths lie in its performance, scalability, maintainability, flexibility, and ease of development. It supports TypeScript, integrates with AWS Lambda, and communicates with GraphQL adapters, making it suitable for both web application and API development.

To address potential concerns about Fastify, it's important to note that, as of 2022, it's not as widely adopted as Express. Documentation is still evolving, and community support may not be as extensive. Inexperienced developers might find limited job opportunities requiring Fastify expertise.

Despite its youth, Fastify's architecture stands out, especially in serverless applications, where it offers seamless development without additional tools. However, developers should carefully consider their project requirements, compatibility with existing software, and community support before choosing Fastify.

For those interested in exploring Fastify, the article provides a practical tutorial on its implementation, highlighting key files such as server.ts, app.ts, router.ts, and routing.ts. Additionally, the shift from middleware to plugins is emphasized, showcasing the power of Fastify's plugin architecture.

The article concludes with resources for further learning, including the official Fastify website, npm page, GitHub repository, and a helpful article on migrating from Express to Fastify. It acknowledges Fastify's continuous evolution, particularly improvements in the type system starting from version 3.

In my view, Fastify is a valuable addition to the Node.js ecosystem, offering a compelling alternative to Express. While Express remains a reliable choice with widespread popularity, the healthy competition introduced by Fastify contributes to the overall growth and innovation in web development frameworks for Node.js.

Fastify – framework overview, implementation, pros, and cons (2024)

FAQs

What are the pros and cons of fastify? ›

Fastify: - Pros: Fast, lightweight, built-in data validation and serialization, plugin architecture. - Cons: Steep learning curve, smaller community.

Why not use Fastify? ›

Cons of Fastify: Fastify is not as easy to learn and use as Express, making it less suitable for beginners. It has a steeper learning curve due to its focus on performance and scalability. Fastify does not provide built-in support for templating engines, making it less suitable for building dynamic web pages.

What is the Fastify framework? ›

Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.

Why do we use Fastify? ›

Fastify is a next-generation web framework for Node. js that prioritizes speed, efficiency, and developer experience. Built with a focus on performance, Fastify offers lightning-fast routing, built-in input validation, and serialization capabilities to enhance your development process.

How many requests can fastify handle? ›

A benchmark made by Fastify, shows that Express. js— a minimal and flexible Node. js web application framework with a robust set of features for web and mobile— can handle approx 15,000 requests per second and the basic HTTP module, 70K requests per second. This is very fast.

What is the alternative to fastify Express? ›

Top Alternatives of ExpressJS
  • SailsJS. Sails is a robust, flexible, high-performance MVC framework for NodeJS. ...
  • NestJs. NestJS is a full-stack JavaScript framework for building efficient and scalable server-side applications. ...
  • Fastify. ...
  • FeatherJS. ...
  • NuxtJS. ...
  • Koa. ...
  • EmberJS. ...
  • BackboneJS.
Jun 3, 2024

Should I use Nestjs or fastify? ›

Fastify includes a built-in router for defining routes and handling requests efficiently. @nestjs/core provides performance optimizations and is suitable for building scalable and efficient applications. Fastify is built for high performance and low overhead, making it one of the fastest web frameworks available.

Is Nest benchmark better than fastify? ›

Fastify provides a good alternative framework for Nest because it solves design issues in a similar manner to Express. However, fastify is much faster than Express, achieving almost two times better benchmarks results. A fair question is why does Nest use Express as the default HTTP provider?

How popular is fastify? ›

Is fastify popular? The npm package fastify receives a total of 1,648,219 weekly downloads. As such, fastify popularity was classified as a key ecosystem project. Visit the popularity section on Snyk Advisor to see the full health analysis.

How is fastify so fast? ›

Fastify provides full encapsulation for plug-ins, automatically parses JSON with relatively faster rendering, and provides quick routing. Among other benefits, Fastify also has a cleaner syntax for writing async code in controllers. Fastify is consistently faster than Express by 2–3 seconds.

Is fastify a Node module? ›

Fastify is a NodeJS framework used for backend web development. It has a powerful plugin, architecture, and minimal overhead. It is also known as one of the fastest Node. js web frameworks.

Is Fastify single-threaded? ›

Node. js, as its default behavior, operates on a single thread and does not make full use of available CPU cores, even when multiple cores are at its disposal.

What are the disadvantages of fastify? ›

Disadvantages of Fastify

Being a relatively newer web framework, Fastify may have a smaller community compared to more established frameworks. This can result in limited availability of community-developed plugins, extensions, and community-driven support resources.

Should I use Fastify? ›

If you value stability, a wealth of resources, and a vast community, Express is the way to go. However, if you're looking for a faster, more efficient alternative with built-in validation and serialization, Fastify is an excellent option.

How to build a fastify project? ›

How to Build Your First Route: Fastify Quickstart
  1. Step 1: Install the Package. Create an app folder, initialize a Node project and install a Fastify package by running the below code in your terminal: ...
  2. Step 2: Create a Server. Create a file named server.js and paste the code below into the file: ...
  3. Step 3: Testing the Route.
Feb 13, 2024

Why choose Fastify over Express? ›

If you value stability, a wealth of resources, and a vast community, Express is the way to go. However, if you're looking for a faster, more efficient alternative with built-in validation and serialization, Fastify is an excellent option.

Is fastify really fast? ›

Fastify provides full encapsulation for plug-ins, automatically parses JSON with relatively faster rendering, and provides quick routing. Among other benefits, Fastify also has a cleaner syntax for writing async code in controllers. Fastify is consistently faster than Express by 2–3 seconds.

What does fastify register do? ›

register creates a new Fastify context, which means that if you perform any changes on the Fastify instance, those changes will not be reflected in the context's ancestors. In other words, encapsulation!

Top Articles
Exploring the Pros and Cons of PoW and PoS: Which is the Best Consensus Mechanism? - WazirX Blog
US Stock Market — Live Quotes, News, Ideas — TradingView — India
Golden Abyss - Chapter 5 - Lunar_Angel
Skylar Vox Bra Size
Http://N14.Ultipro.com
Manhattan Prep Lsat Forum
Here are all the MTV VMA winners, even the awards they announced during the ads
Overnight Cleaner Jobs
Dr Doe's Chemistry Quiz Answer Key
Slapstick Sound Effect Crossword
Canelo Vs Ryder Directv
Sunday World Northern Ireland
Toonily The Carry
Aquatic Pets And Reptiles Photos
Aberration Surface Entrances
Urban Airship Expands its Mobile Platform to Transform Customer Communications
Everything you need to know about Costco Travel (and why I love it) - The Points Guy
Why Is 365 Market Troy Mi On My Bank Statement
Persona 4 Golden Taotie Fusion Calculator
Geometry Review Quiz 5 Answer Key
Skip The Games Fairbanks Alaska
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
Anotherdeadfairy
Ontdek Pearson support voor digitaal testen en scoren
Chime Ssi Payment 2023
Kentuky Fried Chicken Near Me
Klsports Complex Belmont Photos
Lbrands Login Aces
Striffler-Hamby Mortuary - Phenix City Obituaries
Mississippi Craigslist
Airg Com Chat
Does Royal Honey Work For Erectile Dysfunction - SCOBES-AR
Evil Dead Rise - Everything You Need To Know
Nacogdoches, Texas: Step Back in Time in Texas' Oldest Town
Deleted app while troubleshooting recent outage, can I get my devices back?
Texters Wish You Were Here
Craigslist Georgia Homes For Sale By Owner
7543460065
The TBM 930 Is Another Daher Masterpiece
R/Moissanite
Wilson Tattoo Shops
Wilson Tire And Auto Service Gambrills Photos
Nimbleaf Evolution
Headlining Hip Hopper Crossword Clue
Craigslist Sparta Nj
How to Find Mugshots: 11 Steps (with Pictures) - wikiHow
Laura Houston Wbap
Brutus Bites Back Answer Key
Craigslist Com Brooklyn
When Is The First Cold Front In Florida 2022
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 5887

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.