1. Course overview and setup
10m

πŸ‘‹ Welcome to Caching in the router!

We're so glad you've decided to join us. In this course, we'll take a deep dive into the 's caching capabilities. We'll talk about how the router's in-memory cache works, how to set up to improve performance, and how to configure a distributed cache that multiple router instances can access.

Let's jump in!

Why caching?

When we build a federated architecture, we're prioritizing performance from the start: a centralized receives a client request, divides it up by responsibility, and executes smaller requests to gather all the data that's needed.

Supergraph architecture diagram

Using the , the already understands how to ask each for just the data it's responsible forβ€”and nothing more! This keeps our lean, performant, and composable.

Caching makes a good thing even better: we can implement caching throughout our system, from the frontend to the back. But we can also make sure that the piece that ties everything together, the , can also benefit from our caching strategy.

The comes equipped out-of-the-box with its own robust in-memory cache. This cache accelerates the router's ability to resolve queries by storing and reusing documents useful to the execution process, such as responses and .

The biggest limitation with the 's in-memory cache is that it's exactly that: in-memory. Enterprise environments with a lot of traffic typically involve multiple instances running simultaneously, but each router consults its own cache in isolation when looking to save time executing queries.

We can remedy this problem by configuring a distributed cache, one shared by all the instances. With a distributed cache, each router can have access to information that may have originally been cached by a different router instance.

What we're building

In this course, we'll tinker with the that powers requests for Airlock, an intergalactic trip booking app. To keep our focus on caching, we'll work with just two of Airlock's many : listings and reviews. But you won't need to be familiar with the code: we'll stay in the 's domain, and let our remotely-hosted subgraphs do their job of returning data when queried.

Sign up for an Apollo GraphOS account with an Enterprise plan

To work with the , you'll need an Apollo account with an . You can still follow along if your organization is on a different plan, but you won't be able to complete certain hands-on tasks. You can also test out this functionality by signing up for a free Enterprise trial.

Task!

Project setup

To follow along with the course, you will need the following:

Prerequisite knowledge

We assume that you are familiar with concepts like types, queries, and . Check out our Intro to GraphQL course in the language of your choice if you need a refresher. And to brush up on , check out our Federation course.

Additionally, familiarity with YAML is a plusβ€”but we'll walk through all the configuration as we write it! Lastly, you'll need a text editor or an IDE (we're using Visual Studio Code).

Create the project directory

No code to clone here: just create a project directory on your computer that we can work in.

We'll start by creating two new (empty) files: router-config.yaml and .env.

πŸ“‚ odyssey-caching-in-router
┣ πŸ“„ .env
β”— πŸ“„ router-config.yaml

.env will hold our -specific environment variables. router-config.yaml will server as our configuration file for the that we'll install next.

Install the router

The is a high-performance graph router, available as an executable binary that you can add to your project with a curl command.

We'll download it now by running the install command in a terminal opened to our project directory.

odyssey-caching-in-router
curl -sSL https://router.apollo.dev/download/nix/latest | sh

Note: Visit the official documentation to explore alternate methods of downloading the Router.

Now when we check the contents of our project directory, we'll see that we have a new binary file called router!

πŸ“‚ odyssey-caching-in-router
┣ πŸ“„ .env
┣ πŸ“„ router
β”— πŸ“„ router-config.yaml

We'll boot up our in the next lesson. Before we do that, we'll need to make sure we have the running on our computer.

Rover setup

is Apollo's command line interface (CLI) tool that helps developers work with and interact with . It's a handy and versatile tool that can be used for both local development and CI/CD. We'll use Rover to publish our .

Task!

Up next

With all of our installations out of the way, it's time to set up our !

Next

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.