How to test the performance of your GraphQL applications?
Tags: webdev, graphql, security, cybersecurity
Published: 2022-12-22 15:30:00


**We released a new feature** in Escape that enables you to **monitor the performance of your GraphQL application.** Let's discover it!


**🔎 Performance is at the heart of GraphQL security. But what makes an API perform poorly?**


We found that 80% of all GraphQL endpoints scanned by Escape since its inception are vulnerable to Denial of Service (DoS) and Complexity attacks. That's a lot of APIs with performance problems!


![Escape CEO Tristan Kalos on GraphQL Security Best Practices during the Apidays Paris (December 2022)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwc382tahjwaqckpo2fu.jpg)

GraphQL being a Query Language with user-supplied queries, an attacker can craft a request requiring a lot of resources for your server to process. All these requests have in common that they use little-known features of GraphQL.

DoS attacks are made more accessible by a GraphQL feature named aliasing. With GraphQL aliases, an attacker can call a resolver many times in the same query or mutation, bypassing HTTP-based rate limiting. ➡️ [Read more about this vulnerability and its remediations.](https://escape.tech/blog/graphql-batch-attacks-cause-dos/)

Another common cause of performance issues is the ability of clients to nest GraphQL queries and even make them deeply recursive. That happens when your graph contains self-referencing objects. (e.g., a user has friends, who also have friends, who also have friends, etc.) Attackers can abuse this feature to cause CPU or memory exhaustion. ➡️ [Learn how it works and how you can protect your API here.](https://escape.tech/blog/cyclic-queries-and-depth-limit/)

Performance issues may also result from the infamous "N+1 problem" in GraphQL. For instance, imagine we want to fetch all users' applications' names. The endpoint will fetch all users first, then resolve subsequent applications sequentially to get their names. We, therefore, have 1 request for the users plus N requests for the applications, one for each user.

```graphql
query {
  users {
    id
    email
    applications {
        name
    }
  }
}
```
If the underlying storage of the endpoint is a relational database, logs will look like this:

```SQL
-- Fetch all users
SELECT * FROM users;
-- Fetch applications for all users, sequentially
SELECT name FROM applications WHERE userId = 1;
SELECT name FROM applications WHERE userId = 2;
SELECT name FROM applications WHERE userId = 3;
SELECT name FROM applications WHERE userId = 4;
```
Last but not least, performance problems may also result from debug queries. As a developer, if you have a huge query that allows you to fetch most of your database, be sure to have at least two types of environments (dev and production), with debug queries disabled in production. We wrote two articles about best practices for the production environment: [GraphQL Error Handling Best Practices for Security](https://escape.tech/blog/graphql-error-handling-a-security-pov/) and [When GraphQL Errors become a Security Issue](https://escape.tech/blog/graphql-verbose-error-suggestions/).

Now, let's see about how Escape finds those vulnerabilities.


**🛡️ What does Escape do?**


**During the scan**, Escape's scanner sends a custom header (X-Escape-Request-ID) with each request sent to your API endpoint. This lets us know when we sent it and when we received it. We call this **request tracing**. The scanner keeps track of all responses time and puts the concerning requests in a queue.

**After the scan,** we consume the queue synchronously to be sure the flagged requests outline performance problems in your app.

Let's take a look at the Escape platform for browsing performance issues found during a scan.


**⚙️ How to manage your performance issues?**


![Escape platform Performance view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14hzz4ddor06cuq6aq24.png)

Here is the performance issues view of a scan run with Escape's GraphQL Security Scanner, which enables you to detect and fix your performance problem. Escape lists poorly performing queries and associates a severity score depending on the response time. **A response time above 4 seconds is flagged as medium severity and above 10 seconds as high.**

You can read our [documentation about DOS](https://escape.tech/docs/security_tests.html#dos) and [Complexity](https://escape.tech/docs/security_tests.html#complexity) to learn more about security timeout.

![Performance issues - How to reproduce panel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jiqluc4i1d7tcw2ehw87.png)

You can get more details by clicking on a performance vulnerability. For example, in the screenshot above, our scanner found that the debug query took 5 seconds to process, flagging the issue as medium. As a developer, you can see how to reproduce the request by copying the query and trying it yourself.👇

![Transaction_received query request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azqatyk3epccxlc4y8vu.png)

Want to secure your GraphQL application now? Try Escape for free 🚀