Edit and preview content side-by-side with our new Live Preview

Performance isn't a compromise with GraphQL

This article aims to debunk the myth that GraphQL's flexibility comes at the cost of performance.
Joel Olawanle

Written by Joel Olawanle

Jul 29, 2024
GraphQL performance

In web development, performance is non-negotiable. Meta (formerly Facebook) designed GraphQL with the understanding that the speed and efficiency of your API can make or break your application's user experience.

GraphQL was created to offer a more flexible and powerful alternative to traditional REST APIs. However, a quick web search reveals many articles and comments arguing against GraphQL due to perceived performance issues.

This conflicting information can be quite confusing for developers. However, the truth is that performance isn’t a compromise with GraphQL. When implemented correctly, GraphQL delivers exceptional performance.

This article aims to debunk the myth that GraphQL's flexibility comes at the cost of performance. We’ll explore the various layers that impact GraphQL performance and provide actionable insights to ensure your GraphQL APIs are optimized to their full potential.

#GraphQL performance explained

When discussing GraphQL performance, it's crucial to understand the four fundamental layers that significantly impact it: the HTTP endpoint, GraphQL query, GraphQL resolver and data-loader, and tracing.

Let's examine each layer in detail…

1. HTTP endpoint

The HTTP endpoint is the initial touchpoint for any GraphQL request and gateway to all data transactions. Its performance is crucial as it can significantly impact your API's efficiency.

Unlike REST APIs, which have multiple endpoints for various data, GraphQL operates with a single endpoint, typically an HTTP POST endpoint, where all queries are sent.

This raises some concerns about performance, particularly around caching. In REST, you can easily configure web caches to match specific URL patterns, HTTP methods, or resources. However, with GraphQL, every query, despite being different, hits the same endpoint, making traditional caching more challenging.

Despite these challenges, GraphQL's flexibility allows for effective cache management through persistent queries and response caching strategies. These methods can ensure that frequently requested data is efficiently cached, improving performance.

2. GraphQL query

A GraphQL query is a request made by the client specifying the exact data needed. One of the primary advantages of GraphQL is its ability to avoid overfetching and underfetching by allowing precise queries tailored to the client's needs.

An efficient query structure is crucial for performance. It ensures that only the necessary data is fetched. This precision prevents unnecessary data transfer and processing, enhancing the API's responsiveness and efficiency.

Consider the following GraphQL query:

{
post(id: "1") {
id
title
author {
id
name
}
}
}

This query requests a specific post by its ID and includes only the post’s ID, title, and the author’s ID and name, avoiding extraneous data.

3. GraphQL resolver and data-loader

Resolvers are the backbone of a GraphQL API and are responsible for fetching the data specified in the query.

Writing efficient resolvers is crucial. They should be optimized to minimize the number of operations and avoid redundant computations.

For example, a resolver fetching posts in a blog platform might be optimized to fetch posts in bulk instead of making separate database calls for each post. This reduces the overall number of database hits, speeding up the response time.

const resolvers = {
Query: {
posts: async () => {
// Fetch all posts in one go
return await fetchPostsFromDB();
},
},
};

On the other hand, data loaders enhance performance by batching multiple requests into a single query and caching the results to avoid redundant data fetching.

Using data loaders is like having a well-organized warehouse. Instead of picking items individually, you batch similar items together, saving time and effort. This approach ensures that repeated requests for the same data are served from the cache rather than hitting the database multiple times.

Imagine we need to fetch authors for each post. Without a DataLoader, each post would trigger a separate database query for its author. With DataLoader, we batch these requests.

const DataLoader = require('dataloader');
// Create a DataLoader for authors
const authorLoader = new DataLoader(async (ids) => {
const authors = await db.getAuthorsByIds(ids);
return ids.map(id => authors.find(author => author.id === id));
});
const resolvers = {
Query: {
posts: async () => {
// Fetch all posts in one go
return await fetchPostsFromDB();
},
},
Post: {
author: (post) => {
// Use DataLoader to batch and cache author requests
return authorLoader.load(post.authorId);
},
},
};

In this scenario, the posts resolver fetches all posts in one go, and the author field resolver uses DataLoader to fetch all authors in a single, batched request, significantly improving performance.

4. Tracing

Tracing involves monitoring and analyzing the performance of an entire GraphQL request. This provides insights into where bottlenecks occur and how to address them. Monitoring the path and execution time of each request helps identify slowdowns and inefficiencies.

For instance, you can monitor and trace the performance of a real-time analytics platform by looking into its GraphQL queries to quickly pinpoint and resolve any performance issues that arise.

Analyzing the data collected through tracing can reveal patterns and trends, guiding developers to optimize their GraphQL implementation.

#GraphQL performance metrics

Having understood the four fundamental layers that can impact GraphQL performance. It is important to understand the key metrics for measuring the performance of your GraphQL API.

These metrics provide insights into the efficiency and responsiveness of your queries and resolvers.

  1. Response time: This measures the total time it takes for a query to be processed and a response to be sent back to the client. It encompasses all the request stages, from the initial query reception at the HTTP endpoint to the final data delivery after processing by resolvers and data-loaders. Lower response times indicate a more responsive and efficient API.

  2. Overhead: This refers to the extra processing time added by the GraphQL server to handle a query. This includes parsing the query, validating it, executing it, and finally formatting the response. Reducing overhead is essential to improving overall performance.

  3. Query execution time: Measures the duration taken to execute the GraphQL query, excluding any network delays. This is vital for understanding how efficiently the server processes a query.

  4. Resolver execution time: Measures how long it takes for each resolver to obtain and return the necessary data. Efficient resolvers are crucial for minimizing response times.

  5. Network latency: Measures the delay caused by data transmission between the client and the server. While some latency is inevitable, minimizing it is essential for maintaining a responsive API.

  6. Caching efficiency: Refers to how effectively the system caches and retrieves data to avoid redundant processing. Efficient caching can significantly reduce the load on the server and improve response times.

  7. Error rate: Tracks the frequency of errors encountered during the query execution. A high error rate can indicate issues with query design, resolver logic, or server configuration.

#How to ensure optimal GraphQL performance

Ensuring optimal performance in a GraphQL API involves a combination of best practices, careful planning, and leveraging the right tools.

A recent GraphQL survey by our team asked developers what measures they take to improve GraphQL API performance. About 67.8% said they rely on caching, 52.6% mentioned client-side query optimization, 30.3% used persistent queries, and 13.2% employed methods like batch loading, per-entity rate limits, and DataLoaders.

Let’s explore these strategies:

1. Efficient query design

The foundation of a performant GraphQL API starts with designing efficient queries. By structuring your queries to fetch only the necessary data, you can minimize the processing load and speed up response times.

For example, instead of fetching an entire user profile with all details, fetch only what is needed:

query {
user(id: "123") {
name
email
recentPosts(limit: 5) {
title
date
}
}
}

2. Use persisted queries

Persisted queries involve storing pre-defined queries on the server and using unique identifiers to execute them. This reduces the need to parse and validate queries at runtime, thus improving performance.

They are like having a fast pass at an amusement park—skipping the long lines and getting straight to the fun.

By eliminating the overhead of query parsing and validation for each request, persisted queries lead to faster response times and improved performance. This method is especially beneficial for high-traffic applications where reducing server load is crucial.

3. Optimize resolvers

Resolvers are responsible for fetching the data requested in queries. Writing efficient resolvers is crucial to minimizing execution time.

For example, here is a resolver optimized to fetch user data and their recent posts separately, avoiding redundant data fetching.

const resolvers = {
Query: {
user: async (_, { id }, { dataSources }) => {
return dataSources.userAPI.getUserById(id);
},
},
User: {
recentPosts: async (user, { limit }, { dataSources }) => {
return dataSources.postAPI.getRecentPostsByUserId(user.id, limit);
},
},
};

Efficient resolvers ensure that each piece of data is fetched in the most optimal way, reducing server load and improving response times.

4. Implement data loaders for batching and caching

Data loaders help reduce the number of database calls by batching multiple requests into a single query and caching the results.

Here is an example demonstrating how to use a data loader to batch and cache user data requests, reducing the number of database hits.

const DataLoader = require('dataloader');
const userLoader = new DataLoader(keys => batchGetUsers(keys));
const resolvers = {
Query: {
user: (_, { id }) => userLoader.load(id),
},
};
async function batchGetUsers(userIds) {
const users = await getUsersFromDatabase(userIds);
return userIds.map(id => users.find(user => user.id === id));
}

By batching requests, data loaders minimize the number of database queries, improving the efficiency of data retrieval and reducing server load.

5. Monitor and analyze performance with tracing

Implementing tracing helps you monitor the performance of GraphQL requests, identify bottlenecks, and optimize accordingly.

For example, by adding the ApolloServerPluginUsageReporting plugin, you can monitor and analyze request performance, gaining insights to improve efficiency.

#Debunking the myth: Why GraphQL performance is not inferior to REST

The debate between GraphQL and REST often centers on performance, with some arguing that GraphQL’s flexibility leads to inefficiencies and potential performance issues.

However, with proper implementation and optimization, GraphQL performs better than REST APIs. Let’s address some common misconceptions and demonstrate why GraphQL performance is not inferior to REST.

Myth 1: GraphQL queries are always more complex

While GraphQL allows for complex queries, they don’t have to be inefficient. The key is in how you design and optimize your queries.

By leveraging GraphQL’s ability to fetch only the necessary data, you can reduce the amount of data transferred compared to REST, where over-fetching is a common issue.

For example, in REST, you might need multiple endpoints to get all the data you need:

GET /user/123
GET /user/123/posts

In GraphQL, a single query can fetch all the necessary data:

query {
user(id: "123") {
name
posts {
title
content
}
}
}

This reduces the number of requests and can lead to faster overall data retrieval.

Myth 2: GraphQL introduces high server load

Proper use of batching and caching can mitigate server load. Data loaders can batch multiple requests into a single query, reducing the number of database hits and improving performance.

Here is an example of using a data loader to batch requests:

const DataLoader = require('dataloader');
const userLoader = new DataLoader(keys => batchGetUsers(keys));
const resolvers = {
Query: {
user: (_, { id }) => userLoader.load(id),
},
};
async function batchGetUsers(userIds) {
const users = await getUsersFromDatabase(userIds);
return userIds.map(id => users.find(user => user.id === id));
}

By batching database requests, you reduce the server load and improve performance.

Myth 3: GraphQL’s flexibility leads to performance issues

While GraphQL’s flexibility allows for more tailored queries, it also provides tools to manage and optimize them effectively.

Persisted queries and query complexity analysis can help prevent inefficient queries from impacting performance.

Myth 4: REST is better for caching

GraphQL can be just as effective for caching, especially with persisted queries and response caching strategies.

Tools like Apollo Server’s response cache plugin allow you to cache responses and improve performance.

Here is an example of implementing response caching in Apollo Server:

const { ApolloServer, gql } = require('apollo-server');
const responseCachePlugin = require('apollo-server-plugin-response-cache');
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [responseCachePlugin()],
cacheControl: {
defaultMaxAge: 5,
},
});

Caching frequently requested data can significantly reduce server load and improve response times.

Myth 5: GraphQL error handling is inefficient

In REST APIs, errors are often communicated through HTTP status codes such as 404 for "Not Found" or 500 for "Internal Server Error." These codes provide a high-level overview of what went wrong but can sometimes be too generic, requiring additional investigation to pinpoint the specific issue.

In contrast, GraphQL returns errors within the response body rather than relying solely on HTTP status codes. This approach enables developers to provide specific error messages, the exact location of the error in the query, and additional context, such as custom error codes and relevant metadata.

This detailed information makes diagnosing and resolving issues much easier. Here’s an example of a detailed GraphQL error response:

{
"data": null,
"errors": [
{
"message": "User not found",
"locations": [{ "line": 2, "column": 3 }],
"path": ["user"],
"extensions": {
"code": "USER_NOT_FOUND",
"timestamp": "2024-06-03T12:00:00Z",
"details": {
"userId": "123"
}
}
}
]
}

In this example, the error message is specific ("User not found"), the exact location of the error in the query is provided, and additional context such as a custom error code (USER_NOT_FOUND), timestamp, and user ID is included. This level of detail significantly improves the efficiency of error handling.

Additionally, GraphQL tools like Apollo Client enable the implementation of custom error handling, providing further control and flexibility. Here's an example of how to handle errors using Apollo Client:

import { ApolloError } from 'apollo-server';
const resolvers = {
Query: {
user: async (_, { id }, { dataSources }) => {
try {
const user = await dataSources.userAPI.getUserById(id);
if (!user) {
throw new ApolloError('User not found', 'USER_NOT_FOUND');
}
return user;
} catch (error) {
throw new ApolloError('Failed to fetch user', 'FETCH_USER_ERROR');
}
},
},
};

In this example, the ApolloError class creates custom error messages. When a user is not found, a specific error message and code (USER_NOT_FOUND) are returned.

If the user fetching process fails for any other reason, a different error message and code (FETCH_USER_ERROR) are returned. This provides clear feedback to the client, improving the overall user experience.

#Summary

GraphQL isn't just a buzzword; it's a powerful tool that, when used correctly, can outperform traditional REST APIs in many scenarios.

By understanding the critical layers impacting performance, utilizing key metrics, and implementing best practices, you can ensure your GraphQL API is robust and performant.

For more in-depth insights and statistics, check out our 2024 GraphQL Report. Let's embrace the future of APIs with GraphQL, where performance isn't a compromise but a given.

The GraphQL Report 2024

Statistics and best practices from prominent GraphQL users.

Check out the report

Blog Author

Joel Olawanle

Joel Olawanle

Joel Olawanle is a Frontend Engineer and Technical writer based in Nigeria who is interested in making the web accessible to everyone by always looking for ways to give back to the tech community. He has a love for community building and open source.

Share with others

Sign up for our newsletter!

Be the first to know about releases and industry news and insights.