Enlist the Help of Your Schema for Caching!

Rate this content
Bookmark

Based on our experience running GraphCDN we’ve seen schemas that make it easier to cache and others that might throw some stones in your way. Let me share how to avoid common pitfalls and stay clear of the boulders and let your schema help you with caching GraphQL responses.

This talk has been presented at GraphQL Galaxy 2021, check out the latest edition of this Tech Conference.

FAQ

Marco Locher is part of the GraphCDN team.

Consistent naming for cacheable types is crucial because most clients and GraphQL tooling default to using standard fields like ID or _ID for caching. Inconsistent naming can lead to difficulties in caching and purging objects from the cache when backend modifications occur.

Schema awareness allows the cache to make smarter decisions based on the types returned by queries. This includes optimizing cache storage, handling fragments and interfaces efficiently, and potentially returning partial results while fetching missing fields in the background, enhancing performance and user experience.

Duplicating types in the schema to add a single field can lead to inefficiencies in the cache, as it may need to store the data twice depending on the presence of the extra field. This reduces cache efficiency and increases resource consumption.

It is recommended to use standardized specifications like the cursor connection specification for handling metadata, especially in contexts like pagination. This avoids extending types with fields that are only required in specific contexts, thus maintaining efficiency and consistency in the cache.

Splitting queries can be beneficial as it allows for caching public data that can be reused across different users, enhancing cache effectiveness. This approach separates public and private data, allowing public parts of queries to be cached and reused, thus optimizing performance and resource utilization.

Marko Locher
Marko Locher
6 min
09 Dec, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
In this lightning talk, the speaker discusses best practices for caching GraphQL APIs. They emphasize the importance of consistent naming and configuration for caching fields. Keeping types consistent and making the cache aware of the schema can improve efficiency. The speaker also suggests splitting queries to optimize caching and reduce server round trips.

1. Caching Best Practices

Short description:

In this lightning talk, I'd like to give you some pointers on how your schema can help when caching GraphQL APIs, both via document caches like GraphCDN, but also normalized caches like they're implemented in clients like Apollo, Client, or Urcl. The very first item that I'd like to talk about is having consistent naming for the fields you want to cache. It is important to configure your clients accurately to use the appropriate fields. Keeping your types consistent is also crucial to avoid duplication and make your cache more efficient. Additionally, making your cache aware of your schema unlocks additional functionality and allows for smarter decisions based on the types returned by your queries. Lastly, consider splitting queries in some cases to optimize caching and reduce round trips to the server.

Hello, everybody. My name is Marco Locher and I'm part of the GraphCDN team. If you're interested in GraphCDN or caching GraphQL in general, I hope you didn't miss Max's talk on how to etch GraphQL APIs earlier today.

In this lightning talk, I'd like to give you some pointers on how your schema can help when caching GraphQL APIs, both via document caches like GraphCDN, but also normalized caches like they're implemented in clients like Apollo, Client, or Urcl.

The very first item that I'd like to talk about might seem very obvious, but having ideas on the types that you want to cache and having a consistent naming for those fields is quite important. We've seen a couple of projects from our own users where changing those had a ton of impact. If you can stick to ID or underscore ID, most clients and related GraphQL tooling will use those fields by default. However, if you can't because you're already using other names for those fields in your legacy APIs or it's not easy, you can configure your clients to use the appropriate fields. It is very important to make sure that that configuration is accurate. On GraphCDN, for example, you would configure those fields as so-called key fields, and they define how you can find those objects in the cache again, and more importantly, how you can purge them if you make modifications on your backend. Similarly, we would recommend that you stick to globally unique IDs in your application, for example, using UUIDs or something similar, but if that's not something that you can accommodate, there are workarounds for that as well that most clients implement.

The second item is keeping your types consistent. When working with our users, we have seen schemas where types are duplicated to add a single field. However, they were otherwise identical and they were representing the same data. This will make your cache less efficient as it will now need to store the data twice, depending on whether that extra field is present or not. Similarly, if you want to enrich the data with metadata that might only be required in a very specific context, thinking about search results, for example, where you want to show the search string in a highlighted way, we would recommend implementing a concept like the cursor connection specification for that metadata, instead of extending your type with fields that are only required in a very specific context. Even though the cursor connection spec is mainly aimed at handling pagination, it lends itself very well to being extended for other similar use cases like the one that I talked about just now.

Very important, as well, is to make sure that your cache is aware of your schema. Every cache will offer some functionality, whether it's aware of the schema or not. However, if you make your cache aware of the schema of your data, it will unlock additional functionality that wouldn't be possible otherwise. It can make smarter decisions based on what types are returned by your queries, something that is especially important in the context of fragments or when you're using interfaces. Having your cache aware of your schema will also allow it to return partial results based on already cached data if the missing fields are designated as optional. And while your app is already displaying some information to the user, the cache fetches the missing fields in the background. Without that knowledge, that would have been a query that would have been returned by your API directly and the cache would not have been involved with that at all.

And lastly, if you're using a document cache like RefCDN, in some cases, you might actually be better splitting your queries instead of submitting just a single one. I know GraphQL is known for its flexibility and for the fact that you can customize each query to get you exactly the data that you need. But in some cases, splitting queries and having more than one might actually be beneficial. For example, let's take a look at a query that fetches a list of the most recent articles from a blog as well as a list of recommendations based on the currently logged in user. A document-based cache like RefCDN, for example, takes a look at the whole response that you get. And if it is tied to a specific user, then it would only be able to use that cache data for that specific user in the future again. However, if you split that query into a public part and a more private part, the public part can be reused for every single user, no matter whether they are logged in or not, no matter where they're based. And especially when working with both a document-based cache and a normalized cache as part of your GraphQL client, the client will not be impacted by that either, since it will already have most of the data or even all of the data required in its local cache and would not require a round trip to the server at all.

Thank you very much for listening in. I hope there were some valuable takeaways for all of you. If you have any questions, I'm happy to answer them during the Q&A session or you can ping me on Twitter as well.

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

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
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
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
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
The Talk discusses the challenges and advancements in using GraphQL and React together. It introduces RedwoodJS, a framework that simplifies frontend-backend integration and provides features like code generation, scaffolding, and authentication. The Talk demonstrates how to set up a Redwood project, generate layouts and models, and perform CRUD operations. Redwood automates many GraphQL parts and provides an easy way for developers to get started with GraphQL. It also highlights the benefits of Redwood and suggests checking out RedwoodJS.com for more information.
Adopting GraphQL in an Enterprise
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
Adopting GraphQL in an Enterprise
Today's Talk is about adopting GraphQL in an enterprise. It discusses the challenges of using REST APIs and the benefits of GraphQL. The Talk explores different approaches to adopting GraphQL, including coexistence with REST APIs. It emphasizes the power of GraphQL and provides tips for successful adoption. Overall, the Talk highlights the advantages of GraphQL in terms of efficiency, collaboration, and control over APIs.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
GraphQL has made a huge impact in the way we build client applications, websites, and mobile apps. Despite the dominance of resolvers, the GraphQL specification does not mandate their use. Introducing Graphast, a new project that compiles GraphQL operations into execution and output plans, providing advanced optimizations. In GraphFast, instead of resolvers, we have plan resolvers that deal with future data. Graphfast plan resolvers are short and efficient, supporting all features of modern GraphQL.

Workshops on related topic

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
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced 2022React Advanced 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.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura