GraphQL Performance and Monitoring

Rate this content
Bookmark

GraphQL abstracts the downstream API calls from the frontend and all that frontend has to do is request fields in a query that are required for the rendering of the component. The frontend is not aware that a corresponding field might result in an API call or heavy computations on the backend side. This abstraction hits the performance when the GraphQL schema is not structured properly. Let's take an example to understand more:

Here's the query to get available rooms for a hotel:

hotel (id: $hotelId) { id rooms { id type name } }

The frontend doesn't know that the rooms field inside the query hotel will fire another API call even type field would be fetched from another API endpoint. These nested API calls worsen the performance if there are more rooms. We can effectively solve this structuring the schema well and using data loaders.

Ankita Masand
Ankita Masand
32 min
09 Dec, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk explores the performance implications of using GraphQL in a hotel property management system. It discusses optimizing GraphQL API calls, schema design, and caching using Data Loader. The use of automatic persisted queries and caching can improve performance. The impact of response size and pagination in GraphQL queries is also addressed. Additionally, the Talk emphasizes the importance of monitoring and testing queries, as well as improving perceived performance through visual feedback.

Available in Español: Rendimiento y Monitoreo de GraphQL

1. Introduction to GraphQL Performance and Monitoring

Short description:

I'll talk about GraphQL performance and monitoring. We'll understand the implications of using GraphQL by using a simple use case of a hotel property management system. Let's look at the HTTP calls that you would have to make from the front end client to render this user interface and show the list of bookings in this hotel.

Hi, everyone. My name is Ankita Masand. I'm an Associate Architect at Treebo. I work on Treebo's SaaS product called Hotel Superhero. Hotel Superhero is a cloud-based hotel property management system used by various hotel chains across the world. It provides a pool of features for creating, managing bookings, generating invoices, various reports, configuring prices of rooms in a hotel. We have been extensively using GraphQL for this application.

Today, I'll talk about GraphQL performance and monitoring. As a list of topics that we look at in this talk, performance implications of using GraphQL in the stack, designing GraphQL schemas, batching and caching at a request level using data loaders, lazily loading some fields and streaming responses in a GraphQL query using the new differ and stream directives, caching in GraphQL, and finally, monitoring GraphQL queries using New Relic. Let's get going.

We'll understand the implications of using GraphQL by using a simple use case of a hotel property management system. What you see on the screen is a representation of the Big Bank Hotel. It has three room types, room type 1, 2 and 3. 1A, 1B, 1C are the rooms for room type 1 and booking like the blank boxes indicate bookings for particular rooms. Booking 1 and booking 2 are for room number 1A. The bigger box indicates that booking extends for more number of days.

What would it take to render this UI using a front end client? So this is a view that a hotelier sees on a screen to understand and manage bookings for his hotel. Let's look at the HTTP calls that you would have to make from the front end client to render this user interface and show the list of bookings in this hotel. We'll first make parallel calls to fetch hotel data like name, location. We'll then make a call for fetching room types, rooms, bookings. And once we get a list of bookings from the downstream services, we'll make calls to fetch bills. So every booking is associated with a bill and there are also some attachments in a booking that we have to show on that user interface. So for every booking we'll fetch is corresponding bill and attachments. Every booking is done for a particular user. We'll then call the user's API to fetch more details about that user and also his preferences. This looks okay. And there are not too many calls. So this is something that we are used to. These are the API calls only when there are three bookings in a hotel. If you have seen carefully, we were fetching bill API thrice, which means there are three bookings in a hotel.

2. Performance Implications of Using GraphQL

Short description:

A hotel cannot afford a property management system for managing three bookings. We were at the early stage of building this application and it looked okay to experiment with GraphQL and see how it turns out. The GraphQL would give the response in one API call, there are no multiple round trips to the server. It is declarative in nature and the client specifies the data that it needs and hence a considerable reduction in the response size. Let's look at the GraphQL query that we are using for collectively fetching the response of all the API calls. This looks cool because we don't have to write code on the frontend side to manage when to fire particular APIs. So what are the performance implications of using GraphQL? After all the hard work we felt that the performance of the application is not improved much for bigger bookings.

A hotel cannot afford a property management system for managing three bookings. What would happen if there are hundreds of bookings in a hotel? So we'll have to make 100 API calls to fetch bills, attachments. There would be too many round trips from front end client to server. How do we solve these issues?

We were at the early stage of building this application and it looked okay to experiment with GraphQL and see how it turns out. The GraphQL would give the response in one API call, there are no multiple round trips to the server. It is declarative in nature and the client specifies the data that it needs and hence a considerable reduction in the response size. There is no repeated boilerplate code for different front end applications. And the strongest of all, it has strong type system. The pitch went really well and the front end team got a chance to experiment with GraphQL and add it to the stack.

Let's look at the GraphQL query that we are using for collectively fetching the response of all the API calls. If you see, there's a query Hotel by ID. Inside HotelbyID, we are fetching room types. For every room type we are fetching rooms. We are also fetching bookings in the same query. And inside bookings, we are fetching bill. Bill for every booking, attachments, customers. And for every customer we are fetching his preferences. Let's see how each of these queries would be resolved on the GraphQL server. For resolving HotelbyID query, we are making a call to the downstream service to fetch details of a Hotel. For resolving room types, we are making a call to fetch room types from a downstream service. And for every room type, we are fetching rooms. This is the call that we make to fetch bookings. And for every booking, we'll make call to fetch its corresponding bill, attachments, customers, and for every customer we'll make a call to fetch his preferences.

This looks cool because we don't have to write code on the frontend side to manage when to fire particular APIs. Like Bill is dependent on booking. Once we get response of bookings, then only we can fire Bill APIs. We also don't have to run maps on the frontend to map room types to rooms, rooms to bookings, bookings to Bill. GraphQL does all of that. So what are the performance implications of using GraphQL? After all the hard work we felt that the performance of the application is not improved much for bigger bookings. Bookings that extend for more number of days that have more customers.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Too much JavaScript is getting you down? New frameworks promising no JavaScript look interesting, but you have an existing React application to maintain. What if Qwik React is your answer for faster applications startup and better user experience? Qwik React allows you to easily turn your React application into a collection of islands, which can be SSRed and delayed hydrated, and in some instances, hydration skipped altogether. And all of this in an incremental way without a rewrite.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
React 18! Concurrent features! You might’ve already tried the new APIs like useTransition, or you might’ve just heard of them. But do you know how React 18 achieves the performance wins it brings with itself? In this talk, let’s peek under the hood of React 18’s performance features: - How React 18 lowers the time your page stays frozen (aka TBT) - What exactly happens in the main thread when you run useTransition() - What’s the catch with the improvements (there’s no free cake!), and why Vue.js and Preact straight refused to ship anything similar
From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Our understanding of performance & user-experience has heavily evolved over the years. Web Developer Tooling needs to similarly evolve to make sure it is user-centric, actionable and contextual where modern experiences are concerned. In this talk, Addy will walk you through Chrome and others have been thinking about this problem and what updates they've been making to performance tools to lower the friction for building great experiences on the web.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.