Guide

How to test an optional cache fallback

The PainHunt Team· Published · 5 min read

TL;DR: Test the response and the dependency work separately. A cache miss or outage may fall back to an authoritative read, but it must not turn a failed read into a successful empty result. Include failed writes, corrupt entries, a shared deadline and a later recovery attempt.

Decide whether the store is actually optional

Draw the path from a request to the result. Identify which system establishes the answer and which system stores a reusable copy. This guide concerns the second role: losing the copy may increase latency or database work, but the authoritative data still exists elsewhere.

A Redis connection can serve other purposes. If it holds the only job record, an authorization decision, a quota or a lock, continuing without it can change correctness. Do not adopt an optional-cache policy merely because the dependency is called Redis.

Use a controlled fixture with a clearly recognizable answer, such as two synthetic records with IDs alpha and bravo. Write down the expected representation and authorization scope before injecting faults. Use a test environment or a specifically approved production check; do not disconnect a shared database to create an outage.

Run a matrix that separates failures from no matches

Keep the business input constant while changing one dependency condition. Capture the returned status and data, whether the authoritative reader ran, and whether a cache write was attempted.

Cache Read Result
Hit Not used The permitted cached representation
Miss 2 records The two records; optional population
Read error 2 records The same answer within the deadline
Bad JSON 2 records A miss or invalid-entry path, not a fabricated answer
Write error 2 records Preserve the completed read
Miss Zero A successful empty result
Miss Error An unavailable result, not successful emptiness

We executed this matrix in a small in-memory fixture. The hit made zero authoritative calls; each miss, unavailable-read and corrupt-entry case made one. A failed write preserved the two-record answer. A verified zero-record read succeeded, while a failed authoritative read remained unavailable.

These results demonstrate the fixture's control flow. They do not establish a particular Redis client's timeout behavior or prove that a deployed endpoint follows the same policy. Repeat the relevant cases against your own adapter and response contract.

Give cache work a share of the whole request

A connection limit and a command limit do different jobs. The current node-redis production guide documents connection timeouts, command cancellation and offline-queue behavior. Check the API supported by the installed client version before copying an example.

For an acceptance test, choose a request deadline and record the time remaining before every optional phase. A cache read that consumes most of the deadline cannot be followed by a fresh, unrestricted database timeout. Conversely, after a successful authoritative read, starting an optional write with no time left can sacrifice the result the user needed.

Test a slow connection, a slow command and a late authoritative result separately. Advancing a fake clock can verify the budget arithmetic without making real network requests. An adapter integration test is still needed to check what happens to sockets, queued commands and server work when the caller stops waiting.

Stopping a local wait does not prove that a remote command never executed. For more on controlling repeated attempts, use the request retry budget guide.

Verify population after the response

A log saying that a write was started is weaker evidence than an entry another process can read. For an owned successful request, inspect the expected cache key from a separate client, compare the permitted representation and check its expiration.

Do not seed invented business results into a shared production namespace to make this test pass. Use controlled source records in a test environment, or an explicitly approved query whose actual returned value can be compared privately. A small operational canary belongs in a separate namespace.

Decide how the runtime completes optional writes: an awaited operation with an allowance, or a supported background-work facility with its own limits. An untracked promise can outlive the request's useful execution window. Test the chosen mechanism rather than assuming that successful local development implies reliable deployment behavior.

Exercise recovery without a retry storm

A cache that falls back correctly once can remain ineffective for the lifetime of a warm process. Inject an initial connection failure, restore the fixture dependency, then issue a later request under the documented retry policy.

Record the state transitions: unavailable, temporarily ineligible, eligible to reconnect and ready. The node-redis connection documentation distinguishes isReady from isOpen; an open socket alone is not the complete application-readiness condition.

Also issue several concurrent fixture requests after a failed connection. Check that the adapter limits connection attempts and pending work. A cooldown should reduce repeated attempts while still allowing recovery; a permanent disable flag and an immediate reconnect loop fail in different ways.

Keep authority and isolation in the final check

Fallback can be available and still serve the wrong representation. Preserve current authorization before cache reuse, and test the account or role dimensions that actually change the result. The multi-account isolation guide covers those transitions in detail.

Keep an acceptance record with the application version, fixture IDs, injected faults, expected status, observed data, dependency-call counts and time budget. Record a verified zero separately from an error. That makes the next outage useful evidence about a defined contract, rather than another ambiguous empty response.

Frequently asked questions

Should a cache outage return an empty successful result?

No. An unavailable cache is not evidence that the underlying query has no matches. Use the authoritative read when it fits the request budget; preserve an unavailable result if that read fails. A verified empty database result is a separate case.

Does a successful response prove the cache write finished?

No. The response may have returned before an asynchronous write completed. Verify the owned entry from a separate client after the response, and test the runtime's supported method for completing optional work within a bounded lifetime.

Should every endpoint continue when Redis fails?

Only when Redis is genuinely optional for that endpoint. A cache of reproducible public data differs from a store enforcing a quota, a session decision or a lock. Define the authority and failure policy before applying fallback behavior.

Research the problem behind your idea

Explore problem summaries and source links to compare workflows and decide what to investigate next.

Open the Pain Point Browser

Related articles