Skip to Content

Edge Caching

If you've read through the previous section, you should already be aware that we deploy WunderNodes all across the globe. Each environment you create is backed by a number of WunderNodes in different regions. Additionally, for each environment, we also create domain name, e.g. example.wundergraph.dev backed by a Anycast IP.

Most if not all applications have some caching requirements so that data can be delivered to clients as fast as possible. Lower latency means faster applications and better user experiences which directly translates into better conversion rates.

Our infrastructure allows you to cache data as close as possible to your users. All you have to do is configure your Operations. The WunderNode together with the generated client does all the heavy lifting.

Here's an example configuration:

const operations: ConfigureOperations = {
...
queries: config => {
return {
...config,
kind: "query",
caching: {
enable: false,
public: true,
maxAge: 10,
staleWhileRevalidate: 5,
},
liveQuery: {
enable: false,
pollingIntervalSeconds: 5,
}
}
},
...
custom: {
Countries: enableCaching,
TopProducts: config => ({
...config,
caching: {
...config.caching,
enable: true
},
}),
Users: config => ({
...config,
caching: {
...config.caching,
enable: true,
maxAge: 120,
}
}),
}
}

By default, caching is disabled for all Queries (enable: false), but we set a few other defaults. Caching should be public (public: true), so cached objects are the same for all users. MaxAge is set to 10 (seconds), that is, we're caching for 10 seconds. Stale while revalidate is set to 5 (seconds), so we allow an additional 5 seconds where stale data is acceptable but the cache should revalidate in the background.

Then, in the custom section, you can see some overrides where we configure individual Operations to be cached or not. E.g. the Users Operation should be cached for 120 seconds.

Thats it! Write a GraphQL Query, configure caching, ship your application. You might be thinking, how we revalidate cached content. That's covered in one of the next sections once we've talked about JSON-RPC.