Skip to content
ADP
API Design PrincipleBETA

[ADP-124] Cache-Control

Overview

The Cache-Control HTTP header field is used to specify directives for caching mechanisms in both requests and responses. Proper use of caching can significantly improve the performance and scalability of an API by reducing the load on the server and minimizing network traffic.

Guidance

  • MUST use Cache-Control headers appropriately to optimize API performance and reduce server load.
  • SHOULD set reasonable max-age values based on the frequency of resource changes.
  • MUST use no-store for responses containing sensitive or personal information.
  • SHOULD leverage no-cache for frequently updated resources to ensure freshness while still allowing caching.
  • SHOULD use the Vary header in conjunction with Cache-Control to specify which headers affect the caching behavior.
  • MUST consider the implications of caching on API versioning and updates.
  • Balance between freshness and server load when setting cache durations.
  • Consider the needs of different clients (e.g., mobile vs. web) when setting cache policies.
  • Regularly review and update caching strategies as your API evolves.

Key Directives

For Responses

  1. max-age=<seconds>: Specifies the maximum amount of time a resource is considered fresh.
  2. s-maxage=<seconds>: Similar to max-age, but applies only to shared caches.
  3. public: Indicates that the response may be cached by any cache.
  4. private: Indicates that the response is intended for a single user and must not be stored by a shared cache.
  5. no-cache: Requires validation with the origin server before using a cached copy.
  6. no-store: Indicates that the response must not be stored in any cache.
  7. must-revalidate: Cache must verify the status of stale resources before using them.

For Requests

  1. no-cache: Requires the server to validate the resource before using a cached copy.
  2. no-store: Requests that the response not be stored in any cache.
  3. max-age=<seconds>: Indicates that the client is willing to accept a response whose age is not greater than the specified time in seconds.

Best Practices

  1. Use max-age Appropriately: Set a reasonable max-age value based on how frequently your resource changes.

  2. Leverage no-cache for Dynamic Content: For frequently updated resources, use no-cache to ensure freshness while still allowing caching.

  3. Apply no-store for Sensitive Data: Use no-store for responses containing sensitive or personal information.

  4. Utilize public and private: Use public for responses that can be cached by shared caches, and private for user-specific responses.

  5. Consider s-maxage for CDNs: When using a CDN, leverage s-maxage to set different cache durations for the CDN and the browser.

  6. Implement Conditional Requests: Use ETags or Last-Modified headers in conjunction with Cache-Control for efficient validation.

Example

http
Cache-Control: max-age=3600, must-revalidate, public

This example sets the maximum age to one hour, requires revalidation of stale resources, and allows public caching.

OpenAPI Example

yaml
openapi: 3.1.0
info:
  title: Example API
  version: 1.0.0
paths:
  /users/{id}:
    get:
      summary: Get user information
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        '200':
          description: Successful response
          headers:
            Cache-Control:
              description: Cache-Control header
              schema:
                type: string
                example: max-age=3600, must-revalidate, public
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: integer
                  name:
                    type: string

This OpenAPI example demonstrates how to document the Cache-Control header in an API specification using OpenAPI 3.1.0.

References

  1. MDN Web Docs: Cache-Control
  2. RFC 7234: Hypertext Transfer Protocol (HTTP/1.1): Caching
  3. Google Developers: HTTP Caching