Skip to content

Commit 6baed0e

Browse files
mandiwisebenjie
andauthored
Update caching docs (#1818)
* Expand caching page to address performance in general. * Add redirect for old /learn/caching/ route. * Disambiguate categories of persisted queries. * Provide additional comment on N+1 solutions. * Split content onto separate caching and performance pages. * Apply suggestions from code review Co-authored-by: Benjie <[email protected]> * Fix HTTP spec URL. * Modify N+1 example and explanation. --------- Co-authored-by: Benjie <[email protected]>
1 parent c6dc31e commit 6baed0e

File tree

3 files changed

+110
-12
lines changed

3 files changed

+110
-12
lines changed

src/pages/learn/_meta.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export default {
2424
"schema-design": "Schema Design",
2525
"global-object-identification": "",
2626
caching: "",
27+
performance: "",
2728
security: "",
2829
}

src/pages/learn/caching.mdx

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# Caching
22

3-
> Providing Object Identifiers allows clients to build rich caches
3+
<p className="learn-subtitle">Provide Object Identifiers so clients can build rich caches</p>
44

5-
In an endpoint-based API, clients can use HTTP caching to easily avoid refetching resources, and for identifying when two resources are the same. The URL in these APIs is a **globally unique identifier** that the client can leverage to build a cache. In GraphQL, though, there's no URL-like primitive that provides this globally unique identifier for a given object. It's hence a best practice for the API to expose such an identifier for clients to use.
5+
In an endpoint-based API, clients can use [HTTP caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) to avoid refetching resources and to identify when two resources are the same. The URL in these APIs is a _globally unique identifier_ that the client can leverage to build a cache.
66

7-
## Globally Unique IDs
7+
In GraphQL, there's no URL-like primitive that provides this globally unique identifier for a given object. Hence, it's a best practice for the API to expose such an identifier for clients to use as a prerequisite for certain types of caching.
8+
9+
## Globally unique IDs
10+
11+
### Standardize how objects are identified in a schema
812

913
One possible pattern for this is reserving a field, like `id`, to be a globally unique identifier. The example schema used throughout these docs uses this approach:
1014

1115
```graphql
1216
# { "graphiql": true }
13-
{
17+
query {
1418
starship(id: "3003") {
1519
id
1620
name
@@ -26,23 +30,29 @@ One possible pattern for this is reserving a field, like `id`, to be a globally
2630
}
2731
```
2832

29-
This is a powerful tool to hand to client developers. In the same way that the URLs of a resource-based API provided a globally unique key, the `id` field in this system provides a globally unique key.
33+
This is a powerful tool for client developers. In the same way that the URLs of a resource-based API provide a globally unique key, the `id` field in this system provides a globally unique key.
3034

31-
If the backend uses something like UUIDs for identifiers, then exposing this globally unique ID may be very straightforward! If the backend doesn't have a globally unique ID for every object already, the GraphQL layer might have to construct this. Oftentimes, that's as simple as appending the name of the type to the ID and using that as the identifier; the server might then make that ID opaque by base64-encoding it.
35+
If the backend uses something like UUIDs for identifiers, then exposing this globally unique ID may be very straightforward! If the backend doesn't have a globally unique ID for every object already, the GraphQL layer might have to construct one. Oftentimes, that's as simple as appending the name of the type to the ID and using that as the identifier. The server might then make that ID opaque by base64-encoding it.
3236

33-
Optionally, this ID can then be used to work with the [Global Object Identification](/learn/global-object-identification)'s `node` pattern.
37+
Optionally, this ID can then be used to work with the `node` pattern when using [global object identification](/learn/global-object-identification).
3438

35-
## Compatibility with existing APIs
39+
### Compatibility with existing APIs
3640

3741
One concern with using the `id` field for this purpose is how a client using the GraphQL API would work with existing APIs. For example, if our existing API accepted a type-specific ID, but our GraphQL API uses globally unique IDs, then using both at once can be tricky.
3842

3943
In these cases, the GraphQL API can expose the previous API's IDs in a separate field. This gives us the best of both worlds:
4044

41-
- GraphQL clients can continue to rely on a consistent mechanism for getting a globally unique ID.
45+
- GraphQL clients can continue to rely on a consistent mechanism to get a globally unique ID.
4246
- Clients that need to work with our previous API can also fetch `previousApiId` from the object, and use that.
4347

44-
## Alternatives
48+
### Alternatives
49+
50+
While globally unique IDs have proven to be a powerful pattern in the past, they are not the only pattern that can be used, nor are they right for every situation. The critical functionality that the client needs is the ability to derive a globally unique identifier for their caching. While having the server derive that ID simplifies the client, the client can also derive the identifier. This could be as simple as combining the type of the object (queried with `__typename`) with some type-unique identifier.
51+
52+
Additionally, if replacing an existing API with a GraphQL API, then it may be confusing if all of the fields in GraphQL are the same **except** `id`, which changed to be globally unique. This would be another reason why one might choose not to use `id` as the globally unique field.
53+
54+
## Recap
4555

46-
While globally unique IDs have proven to be a powerful pattern in the past, they are not the only pattern that can be used, nor are they right for every situation. The really critical functionality that the client needs is the ability to derive a globally unique identifier for their caching. While having the server derive that ID simplifies the client, the client can also derive the identifier. Oftentimes, this would be as simple as combining the type of the object (queried with `__typename`) with some type-unique identifier.
56+
To recap these recommendations for using caching with GraphQL APIs:
4757

48-
Additionally, if replacing an existing API with a GraphQL API, it may be confusing if all of the fields in GraphQL are the same **except** `id`, which changed to be globally unique. This would be another reason why one might choose not to use `id` as the globally unique field.
58+
- Defining a globally unique ID field for an Object type can facilitate various types of caching

src/pages/learn/performance.mdx

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Performance
2+
3+
<p className="learn-subtitle">Optimize the execution and delivery of GraphQL responses</p>
4+
5+
At first glance, GraphQL requests may seem challenging to cache given that the API is served through a single endpoint and you may not know in advance what fields a client will include in an operation.
6+
7+
In practice, however, GraphQL is as cacheable as any API that enables parameterized requests, such as a REST API that allows clients to specify different query parameters for a particular endpoint. There are many ways to optimize GraphQL requests on the client and server sides, as well as in the transport layer, and different GraphQL server and client libraries will often have common caching features built directly into them.
8+
9+
On this page, we'll explore several different tactics that can be leveraged in GraphQL clients and servers to optimize how data is fetched from the API.
10+
11+
## Client-side caching
12+
13+
There is a range of client-side caching strategies that GraphQL clients may implement to help not only with performance but also to ensure that you render a consistent and responsive interface to your users. [Read more about client-side caching](/learn/caching).
14+
15+
## `GET` requests for queries
16+
17+
GraphQL implementations that adhere to the [GraphQL over HTTP specification](https://graphql.github.io/graphql-over-http/draft/) will support the `POST` HTTP method by default, but may also support `GET` requests for query operations.
18+
19+
Using `GET` can improve query performance because requests made with this HTTP method are typically considered cacheable by default and can help facilitate HTTP caching or the use of a content delivery network (CDN) when caching-related headers are provided in the server response.
20+
21+
However, because browsers and CDNs impose size limits on URLs, it may not be possible to send a large document for complex operations in the query string of the URL. Using _persisted queries_, either in the form of _trusted documents_ or _automatic persisted queries_, will allow the client to send a hash of the query instead, and the server can look up the full version of the document by looking up the hash in a server-side store before validating and executing the operation.
22+
23+
Sending hashed queries instead of their plaintext versions has the additional benefit of reducing the amount of data sent by the client in the network request.
24+
25+
## The N+1 Problem
26+
27+
GraphQL is designed in a way that allows you to write clean code on the server, where every field on every type has a focused single-purpose function for resolving that value. However, without additional consideration, a naive GraphQL service could be very "chatty" or repeatedly load data from your databases.
28+
29+
Consider the following query—to fetch a hero along with a list of their friends, we can imagine that as the field resolvers execute there will be one request to the underlying data source to get the character object, another to fetch their friends, and then three subsequent requests to load the lists of starships for each human friend:
30+
31+
```graphql
32+
# { "graphiql": true }
33+
query HeroWithFriends {
34+
# 1 request for the hero
35+
hero {
36+
name
37+
# 1 request for the hero's N friends
38+
friends {
39+
name
40+
# N starship requests - one for each of the hero's N human friends
41+
... on Human {
42+
starships {
43+
name
44+
}
45+
}
46+
}
47+
}
48+
}
49+
```
50+
51+
This is known as the N+1 problem, where an initial request to an underlying data source (for a hero's friends) leads to N subsequent requests to resolve the data for all of the requested fields (for the friends' starship data).
52+
53+
This is commonly solved by a batching technique, where multiple requests for data from a backend are collected over a short period and then dispatched in a single request to an underlying database or microservice by using a tool like Facebook's [DataLoader](https://github.com/facebook/dataloader). Additionally, certain GraphQL implementations may have built-in capabilities that allow you to translate operation selection sets into optimized queries to underlying data sources.
54+
55+
## Demand control
56+
57+
Depending on how a GraphQL schema has been designed, it may be possible for clients to request highly complex operations that place excessive load on the underlying data sources during execution. These kinds of operations may be sent inadvertently by an known client, but they may also be sent by malicious actors.
58+
59+
Certain demand control mechanisms can help guard a GraphQL API against these operations, such as paginating list fields, limiting operation depth and breadth, and query complexity analysis. [You can read more about demand control on the Security page](/learn/security/#demand-control).
60+
61+
## JSON (with GZIP)
62+
63+
GraphQL services typically respond using JSON even though the GraphQL spec [does not require it](http://spec.graphql.org/draft/#sec-Serialization-Format). JSON may seem like an odd choice for an API layer promising better network performance, however, because it is mostly text it compresses exceptionally well with algorithms such as GZIP, deflate and brotli.
64+
65+
It's encouraged that any production GraphQL services enable GZIP and encourage their clients to send the header:
66+
67+
```text
68+
Accept-Encoding: gzip
69+
```
70+
71+
JSON is also very familiar to client and API developers, and is easy to read and debug. In fact, the GraphQL syntax is partly inspired by JSON syntax.
72+
73+
## Performance monitoring
74+
75+
Monitoring a GraphQL API over time can provide insight into how certain operations impact API performance and help you determine what adjustments to make to maintain its health. For example, you may find that certain fields take a long time to resolve due to under-optimized requests to a backing data source, or you may find that other fields routinely raise errors during execution.
76+
77+
Observability tooling can provide insight into where bottlenecks exist in the execution of certain GraphQL operations by allowing you to instrument the API service to collect metrics, traces, and logs during requests. For example, [OpenTelemetry](https://opentelemetry.io/) provides a suite of vendor-agnostic tools that can be used in many different languages to support instrumentation of GraphQL APIs.
78+
79+
## Recap
80+
81+
To recap these recommendations for improving GraphQL API performance:
82+
83+
- Using `GET` for GraphQL query operations can support HTTP caching and CDN usage, particularly when used in conjunction with hashed query documents
84+
- Because an operation's selection set can express relationships between different kinds of objects, the N+1 problem can be mitigated during field execution by batching and caching requests to underlying data sources
85+
- Field pagination, limiting operation depth and breadth, and rate-limiting API requests can help prevent individual GraphQL operations from placing excessive load on server resources
86+
- GZIP can be used to compress the size of GraphQL JSON-formatted responses when servers and clients support it
87+
- The overall health of a GraphQL API can be maintained over time by using performance monitoring tools like OpenTelemetry to collect metrics, logs, and traces related to request execution

0 commit comments

Comments
 (0)