How caching with Fastly works

The Fastly edge cache is an enormous pool of storage across the platform's network. While the server hosting your content (your origin) may be far away from your users, causing latency when they visit your site, Fastly caches your content closer to users. By delivering content directly to users instead of having to fetch from origin each request, caching with Fastly can help you reduce data transfer costs and make your site more efficient and scalable.

How caching with Fastly works

To cache content with Fastly, you create a service, which defines the caching rules and behaviors for your website or application. Once your service is configured to deliver your site or application through Fastly, the following takes place whenever a user makes a request for content:

  1. If the content is in the cache, Fastly will return it without visiting the origin.
  2. If the content is not in cache, Fastly will fetch it from the origin and store it in cache (assuming the content is cacheable).

Caching features

Caching use cases vary widely. Fastly has several built-in features that help with both simple and complex scenarios:

  • HTTP caching semantics let HTTP responses carry caching instructions, such as the Cache-Control header, that the Fastly cache uses to decide how they should be cached, as defined by the HTTP Caching standard (RFC 9111).
  • Request collapsing identifies multiple simultaneous requests for the same resource and makes a single backend fetch, using the resulting response to populate the cache and satisfy all waiting clients.
  • Range collapsing merges requests for separate byte ranges of a backend object into a single backend fetch for the entire object, using the resulting response to populate the cache and fulfill future requests for any byte range while managing the lifetime of the entire object.
  • Streaming miss writes a response stream to cache and to an end user at the same time.
  • Client revalidation evaluates conditional headers from a client to determine whether its cached copy is still valid, sending the response body only if necessary, and may forward the request to the backend to refresh the cached object depending on cache state.
  • Backend revalidation adds conditional headers when forwarding a request for a stale cached object to a backend, allowing the backend to extend the object's lifetime without resending the body if the cached content is still valid.
  • Purging expunges cache entries ahead of their normal expiry, so that changes to the source content can be reflected at the edge immediately.

IMPORTANT: All data stored in the Fastly cache is ephemeral and will expire. It may be evicted by the platform before it expires depending on how frequently the data is used. If you require persistent storage at the edge, consider using dynamic configurations like dictionaries, access control lists, or data stores instead.

About the cache interfaces

Fastly provides three interfaces for interacting with the cache: readthrough (HTTP), simple, and core. The interface you choose depends on your service type and caching requirements.

About the readthrough (HTTP) cache

The readthrough (HTTP) cache is the most commonly used cache interface. It automatically caches HTTP responses according to HTTP caching semantics as requests pass through your Fastly service. It is enabled by default for both CDN and Compute services and is the only cache interface available for CDN services.

  1. CDN
  2. Rust
  3. JavaScript
  4. Go
  5. C++

In a CDN service, the readthrough interface works without any configuration or code required.

About the simple cache

The simple cache interface is available exclusively for Compute services and provides straightforward programmatic access to the cache through a getOrSet operation. Use it to cache data directly from your Compute application as volatile key-value data. Common use cases include caching authentication flow state or A/B test flags.

Simple cache has always-on request collapsing: if two operations try to populate the same key simultaneously, the setter callback runs only once. Values are treated as opaque data with no headers or metadata, so simple cache does not support staleness, revalidation, or variation.

  1. CDN
  2. Rust
  3. JavaScript
  4. Go
  5. C++

The simple cache interface is not available for CDN services.

For complete documentation on the simple cache interface, refer to the reference for the Compute SDK of your choice.

About the core cache

The core cache interface is available exclusively for Compute services. It provides low-level programmatic access to the cache, with manual control over cache metadata and behavior. Use it for advanced caching requirements or to build custom higher-level caching abstractions.

Items cached via this interface consist of:

  • A cache key: up to 4KiB of arbitrary bytes identifying a cached item. Since a key may not uniquely identify an item, headers can further distinguish items that share a key. See LookupBuilder::header() in the Rust SDK documentation for details.
  • General metadata: expiry data, including item age, expiration time, and surrogate keys for purging.
  • User-controlled metadata: arbitrary bytes stored alongside the cached content, updatable during revalidation.
  • The object itself: arbitrary bytes, read via Body and written via StreamingBody. For complete documentation on the core cache interface, refer to the reference for the Compute SDK of your choice.
  1. CDN
  2. Rust
  3. JavaScript
  4. Go
  5. C++

The core cache interface is not available for CDN services.

Compare the cache interfaces

The following table summarizes the availability, behavior, and capabilities of each cache interface.

Readthrough (HTTP cache)SimpleCore
Service typeCDN and ComputeComputeCompute
Use it for...Automatic cachingSimple key-value cachingComplex requirements
Cache freshnessHTTP semanticsExplicitExplicit
Request collapsingHeuristicAlways-onManual control
Range collapsing✅ (automatic)✅ (manual)
Streaming miss✅ (automatic)✅ (manual)
Client revalidation✅ (automatic)✅ (manual)
Backend revalidation✅ (automatic)✅ (manual)
Surrogate keys
Purging

Interoperability

All three cache interfaces store data in the same namespace, but interoperability is limited:

  • The core cache interface can read and overwrite objects inserted via the simple cache interface.
  • The simple cache can read (but cannot overwrite existing) objects inserted using core cache but provides only the body of the object.
  • The readthrough cache interface is not interoperable with other cache interfaces and cannot read data written through another interface, nor can it write data that is visible to other cache interfaces.
  • Interoperability also affects purging.

Limitations and constraints

The following limitations apply across all cache interfaces:

  • Variants (created explicitly in the core cache interface or when the readthrough cache processes the Vary HTTP response header) are limited differently depending on platform.

    1. CDN services
    2. Compute services

    In CDN services, the number of variants is limited to 50 per cache object, regardless of the number of Vary rule permutations.

  • In the core cache interface, writes target only the primary storage node for a given cache address. If an existing object is overwritten, replicated copies may continue to be served until their TTL expires or the object is purged.

  • Purges are asynchronous while writes are synchronous. Performing a purge immediately before a write can create a race condition where the purge clears the primary object after the write completes.

For platform-specific limits, refer to CDN service constraints and Compute service constraints.

What's next

Start exploring the different caching features available in Fastly. Or, check out our tutorials to find and implement solutions for common caching scenarios.