Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Accelerating Server-Side Development with Fastify

You're reading from  Accelerating Server-Side Development with Fastify

Product type Book
Published in Jun 2023
Publisher Packt
ISBN-13 9781800563582
Pages 406 pages
Edition 1st Edition
Languages
Authors (3):
Manuel Spigolon Manuel Spigolon
Profile icon Manuel Spigolon
Maksim Sinik Maksim Sinik
Profile icon Maksim Sinik
Matteo Collina Matteo Collina
Profile icon Matteo Collina
View More author details

Table of Contents (21) Chapters

Preface 1. Part 1:Fastify Basics
2. Chapter 1: What Is Fastify? 3. Chapter 2: The Plugin System and the Boot Process 4. Chapter 3: Working with Routes 5. Chapter 4: Exploring Hooks 6. Chapter 5: Exploring Validation and Serialization 7. Part 2:Build a Real-World Project
8. Chapter 6: Project Structure and Configuration Management 9. Chapter 7: Building a RESTful API 10. Chapter 8: Authentication, Authorization, and File Handling 11. Chapter 9: Application Testing 12. Chapter 10: Deployment and Process Monitoring for a Healthy Application 13. Chapter 11: Meaningful Application Logging 14. Part 3:Advanced Topics
15. Chapter 12: From a Monolith to Microservices 16. Chapter 13: Performance Assessment and Improvement 17. Chapter 14: Developing a GraphQL API 18. Chapter 15: Type-Safe Fastify 19. Index 20. Other Books You May Enjoy

Performance Assessment and Improvement

We all know that Fastify is fast – but is your code fast enough for Fastify? Learn how to measure your application’s performance to improve your business value and the quality of your code. By analyzing the metrics, you will avoid introducing speed regressions and identify bottlenecks or memory leaks that could crash your system. You will learn how to add an instrumentation library to a Fastify application to analyze how the server reacts to a high volume of traffic. We will get an overview to understand and act accordingly depending on your measurements to maintain your server’s performance and its healthy status.

This is the learning path we will take in this chapter:

  • Why measure performance?
  • How to measure an application’s performance
  • How to analyze the data
  • How to optimize the application

Technical requirements

As mentioned in earlier chapters, you will need the following:

All the snippets in this chapter are on GitHub at https://github.com/PacktPublishing/Accelerating-Server-Side-Development-with-Fastify/tree/main/Chapter%2013.

Why measure performance?

It sounds nice to go out for dinner and impress our guests by telling them that our APIs serve 10,000 requests per second, but nobody cares about numbers that do not bring any value.

An application’s performance impacts a company’s business in many ways, which are often underestimated because you can’t see a performance issue at first sight as you can with a bug. In fact, performance is responsible for the following:

  • A slow API, which may result in customers abandoning your website, as documented by this research: https://www.shopify.com/enterprise/site-performance-page-speed-ecommerce
  • A server in an idle state during high-load traffic, which is a waste of resources that impacts your infrastructure bill
  • Unoptimized API code, which may waste resources that affect your infrastructure bill

Therefore, to save money and open new business opportunities, it is crucial to measure the application’s performance, but...

How to measure an application’s performance?

By following the Optimization Cycle, the first step to improving something in every work field is that we need to set a baseline. The starting point will be different for each company’s department. For instance, the sales team will measure the sales or the revenue over time to set its improvement goals.

So, we need to define what is worth measuring by defining our application’s metrics boundaries. It’s clear that our first challenge is choosing what to measure. The most common metrics are the following:

  • HTTP response times and request rates
  • Error ratings
  • Process metrics such as CPU, memory, and Garbage Collector usage

Each bullet point is a KPI. Every KPI requires a different approach and a different analysis. Despite their differences, these measurements are strongly connected since inadequate resource management will impact the response latency, and a high error rating may increase...

How to analyze the data

Following the Optimization Cycle, we must analyze the measurement result to understand the following:

  • Can the application performance be improved?
  • Where should we focus first?
  • How can we improve it?
  • Where is the bottleneck?

For example, if we analyze Figure 13.3, the handler spends most of its time on the MongoDB find operation. This means that we should focus on that part, trying to check whether the following applies:

  • We have set the connection pool correctly
  • We have created a dedicated index

Based on whether this is the case or not, you can define some boundaries that can be further optimized in the next Optimization Cycle step.

Of course, when monitoring all the application’s routes, we will need many filters to select all the routes that require our attention, such as slow endpoints or APIs with a high error rate.

Note that, at times, the data cannot be as detailed as in this example. Let’...

How to optimize the application

To optimize the application, we must start from the output of the analysis phase of the Optimization Cycle workflow. The actions that we could take are very different:

  • No action: The data analysis suggests that optimization is not possible or is not worth the effort to improve performance
  • Code base optimization: The data tells us that there are bad algorithms or bad business logic implementation in our code base, and we can rewrite it to speed up the server
  • Architectural action: The data identifies a system bottleneck, such as a legacy database
  • Configuration setup: The analysis suggests that tweaking the configuration could improve the performance

The No action step is continually revised based on data that we will continue to measure and analyze over time.

For every action type, there is different knowledge to acquire. Working within a fantastic and heterogeneous team helps cover all these aspects, which are not always...

Summary

In this dense chapter, we touched on many concepts that will help us measure, analyze, and optimize an application’s performance. This Optimization Cycle is a practical workflow to follow step by step for this challenging task, which may lead to excellent business achievements, such as improving sales or overall customer satisfaction.

Now, you can evaluate a tracing module. By knowing how it works under the hood, you can integrate it into any application to start the measurement process. You also know which third-party software you will need to process and visualize the data, such as an APM. Moreover, you are now able to dig deeper into any performance issue, by analyzing it in detail, supported by some open source tools. Last but not least, once you have found a problem, you can plan an optimization to solve the issue and boost your application’s endpoints iteratively.

You have completed this journey toward building a performant and maintainable Fastify...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Accelerating Server-Side Development with Fastify
Published in: Jun 2023 Publisher: Packt ISBN-13: 9781800563582
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}