Fallback cache Logic
The fallback cache serves the most recent successful response from cache when backend requests fail, ensuring resilience and continuity
The fallback cache provides resilience for API requests by serving previously cached responses when the backend is unavailable or returns an error. When enabled, the system attempts to retrieve a valid cached response if a request to the backend fails (for example, due to a server error).
What is the fallback cache?
The fallback cache is a secondary caching mechanism that stores successful API responses. It activates only when a backend request encounters an error and cannot return a fresh response. Instead of failing the request or showing an error to the user, the system serves the most recent cached version of the response.
How fallback caching works
- Successful response handling
When a request to the backend succeeds, the system stores the response in the fallback cache for future use. The cache entry includes metadata, such as a Last-Modified header, to track when the data was last updated. - Handling failed requests
If a subsequent request for the same resource fails—due to a server error, network issue, or timeout—the system checks the fallback cache:- If a valid cached response is available, the system returns it to the client instead of failing the request.
- If no cached response exists, the system returns the original error response.
- Client-side freshness check
The cached response includes aLast-Modified
header. Clients can use this header to determine how fresh or stale the returned data is. This is especially useful for displaying fallback data with visual indicators or warning messages in the UI.
Requirements
- Fallback caching is not enabled by default. You must configure it explicitly.
- Only requests that meet the system’s caching criteria are stored in the fallback cache. For example, requests that contain certain headers or query parameters might be excluded from caching.
Benefits of using Fallback cache logic
- Improves reliability during backend outages.
- Reduces visible service interruptions.
- Delivers the most recent available data, even if the backend is unavailable.
Note:
- Fallback cache responses are read-only. The system does not let users make updates or deletions while operating in fallback mode.
- Always validate the Last-Modified timestamp before assuming the returned data is current.
- Fallback cache should not be used as a substitute for proper error handling or data freshness requirements.
Updated 1 day ago