How to Edge Cache GraphQL APIs

Rate this content
Bookmark

For years, not being able to cache GraphQL was considered one of its main downsides compared to RESTful APIs. Not anymore. GraphCDN makes it possible to cache almost any GraphQL API at the edge, and not only that but our cache is even smarter than any RESTful cache could ever be. Let's dive deep into the inner workings of GraphCDN to figure out how exactly we make this happen.

This talk has been presented at React Advanced Conference 2021, check out the latest edition of this React Conference.

FAQ

The speaker lived in London for a few months during an internship and got their first job there. London holds a special place in their heart.

The speaker was the CTO of Spectrum, a young and growing startup focused on building a modern take on community forums.

Spectrum faced significant challenges with their chosen database, which was advertised as a real-time database but failed to scale with their growing user base. The servers crashed multiple times a day, and the database couldn't handle more than a hundred real-time update connections.

GraphQL allowed Spectrum to build an API quickly and efficiently. It helped them manage data access and security, ultimately contributing to their successful acquisition by GitHub.

Edge Caching involves caching GraphQL queries at servers located close to users to reduce traffic and improve performance. This approach can significantly lower the load on the origin servers and enhance user experience.

Fastly offers incredibly fast global cache purging, invalidating cached query results worldwide in about 150 milliseconds. This speed helps maintain up-to-date data across multiple data centers.

GraphCDN is a service designed to handle GraphQL caching at the edge. It was created to solve the caching problems faced by Spectrum and is now used by various clients to improve performance and scalability.

GraphCDN is used by various clients, including e-commerce stores like italic.com, which benefits from reduced traffic load on their servers and improved performance.

GraphQL can use defer and stream directives to split queries into separate parts. This allows static data to be cached while real-time data is fetched directly from the origin, improving performance without compromising data freshness.

The __typename field in GraphQL helps identify the type of objects in a query response, enabling efficient caching and invalidation. It allows the cache to link mutation updates to specific query results, ensuring data consistency.

Max Stoiber
Max Stoiber
29 min
22 Oct, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The speaker discusses their experience with edge caching GraphQL APIs, starting with the struggles they faced with a poor database choice. They found success with GraphQL, which helped their servers scale and led to an acquisition by GitHub. The speaker delves into the key piece that enables GraphQL caching, the __typename metafield. They also address challenges with caching at the edge, including authorization and global cache purging. The speaker shares a success story with GraphCDN and mentions the ongoing challenge of handling cache for lists with pagination.

1. Introduction to Edge Caching GraphQL APIs

Short description:

Thank you for that lovely introduction. I'm so glad to be back and I'm really excited to be here today and to talk to you about edge caching GraphQL APIs.

Thank you for that lovely introduction. Just like every speaker here and I assume also every one of you, this feels absolutely surreal. Standing in front of actual people, looking in your faces. This is insane. I love it. I'm so glad to be back and I'm really excited to be here. I love London. I actually got my very first job a long time ago. I used to live in London for a couple months and I did an internship here. So this city has got a special place in my heart and I'm really excited to be here today and to talk to you about edge caching GraphQL APIs.

2. The Story of Spectrum and the Realtime Database

Short description:

I already had a super lovely introduction so I can skip a little bit of that but if you want to follow me anywhere on the internet, my handle is at mxsdbr which looks super complicated but it's actually just my name without the vowels. So just take my name, take out the vowels and you'll arrive at my handle. So, this story starts in 2018. I was the CTO of a young and growing startup called Spectrum. And we were building sort of a modern take on community forums. And for some godforsaken reason that I cannot remember, I chose a really terrible database that I will not name, because I don't want to shame anybody, but it's a really small database that probably none of you have ever used, and they advertised themselves as the Realtime Database.

I already had a super lovely introduction so I can skip a little bit of that but if you want to follow me anywhere on the internet, my handle is at mxsdbr which looks super complicated but it's actually just my name without the vowels. So just take my name, take out the vowels and you'll arrive at my handle. I know I wish I chose something different when I was way younger and didn't know what I was doing but it is what it is now. I can't change it anymore.

So, this story starts in 2018. I was the CTO of a young and growing startup called Spectrum. And we were building sort of a modern take on community forums. So we built this platform where mostly open source projects had all of their users talk with each other. And they, you know, reported bugs, they talked with each other about how to use style components in my case but also many, many other open source projects and many developers used it And we actually grew pretty quickly. Now, I was the CTO of that startup and I had no idea what I was doing. Literally zero. Particularly not a realtime public chat app, which is quite an interesting set of problems to solve because we were taking what forums were, where you can post posts, you can post threads, and you can comment under them, and we made everything realtime. We tried to combine sort of the best of what the 2000s forums give you with what Slack and Discord give you nowadays. We tried to combine the best of both of those worlds. And I came with some really interesting technical problems, because we had all of the read-heavy traffic of a forum. Lots and lots of people came to our platform and read all of the threads and posts and the content that people shared, but all of it was realtime. So all of them subscribed to a WebSocket connection and streamed updates in realtime as people were conversing with each other.

And for some godforsaken reason that I cannot remember, I chose a really terrible database that I will not name, because I don't want to shame anybody, but it's a really small database that probably none of you have ever used, and they advertised themselves as the Realtime Database. And their whole thing was, you can take any database query and you can put a thing at the end and then you get a realtime subscription to any changes on that database query. Damn, that sounds amazing. That sounds exactly like what we need. We're building this realtime chat platform. Surely, that's exactly what we need. So we used it, but the problem was it was really badly built. And I found out in hindsight that this company that built the database had raised some money from investors and they weren't growing very quickly. They didn't manage to get any market share compared to a MongoDB, right? Or even a Postgres or MySQL. And so their idea was, okay, we have this really cool database. We're going to just make it realtime. And then we're going to sell that. And they spent half a year building that, but they never really ran it in production.

QnA