Apollo Cache is Your Friend, If You Get To Know It

Shopify's Raman Lally delves into the Apollo GraphQL client cache and the life cycle of objects that are cached within it.

Raman Lally
16 min readadvanced
--
View Original

Overview

The article discusses the Apollo GraphQL client cache, particularly focusing on its lifecycle, including fetching, normalization, updating, and garbage collection. It highlights common misunderstandings and provides insights into how to effectively utilize the cache to improve application performance.

What You'll Learn

1

How to understand the lifecycle of objects in the Apollo cache

2

Why fetch policies are crucial for data retrieval in Apollo Client

3

How to implement normalization for efficient data storage in the cache

4

When to use garbage collection to manage cache size

Prerequisites & Requirements

  • Basic understanding of GraphQL and caching concepts
  • Familiarity with Apollo Client(optional)

Key Questions Answered

What is the Apollo cache and how does it function?
The Apollo cache is an InMemoryCache that stores data from network queries during a browser session. It does not persist data between sessions and serves as a representation of the queried data, allowing for efficient data retrieval and management.
How do fetch policies affect data retrieval in Apollo Client?
Fetch policies determine whether data is retrieved from the cache, the network, or both. The default policy is cache-first, meaning Apollo will check the cache for data before making a network request, which can help reduce load times and improve performance.
What are the steps involved in data normalization within the Apollo cache?
Data normalization in the Apollo cache involves breaking down queried data into individual objects, assigning globally unique cache identifiers, and storing them in a flattened data structure for efficient lookups. This process ensures that duplicate objects are stored in the same location.
When should garbage collection be used in Apollo Client?
Garbage collection should be used when there are unreachable items in the cache that take up memory. Developers can call the cache.gc() method to clear these items, which helps maintain application performance over time.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Frontend
Apollo Client
Used for querying GraphQL services and managing cache in frontend applications.
Backend
Graphql
Serves as the API for querying data in the applications discussed.

Key Actionable Insights

1
Understanding fetch policies can significantly enhance data retrieval efficiency in your applications.
By explicitly setting fetch policies, developers can optimize performance and reduce unnecessary network requests, leading to a smoother user experience.
2
Implementing normalization helps manage data effectively in the Apollo cache.
Normalization reduces redundancy and ensures that data is stored in a way that allows for quick access, which is crucial for applications that handle large datasets.
3
Regularly using garbage collection can prevent performance degradation in long-lived applications.
As applications accumulate data over time, running garbage collection helps free up memory, ensuring that the application remains responsive and efficient.

Common Pitfalls

1
Misunderstanding how the Apollo cache normalizes data can lead to unexpected results.
If developers are not aware that objects with the same ID will be merged, they may encounter issues where data appears to be missing or incorrect. It's essential to ensure that all queried objects have unique identifiers.
2
Failing to implement proper garbage collection can result in memory bloat.
Without regular garbage collection, applications can slow down as they accumulate unreachable data in the cache. Developers should proactively manage cache size to maintain performance.

Related Concepts

Caching Strategies
Graphql Best Practices
Data Normalization Techniques