Testing degraded operation: how a page behaves without its cache service

Bound origin work during cache failure, coordinate a permit through MySQL/InnoDB and choose an eligible retained copy or 503.

A narrow bypass channel represents fallback access with limited capacity.
A narrow bypass channel represents fallback access with limited capacity.

A cache makes a page faster while its most expensive work is needed only occasionally. If the cache store stops responding, the simple rule “read the database instead” changes that ratio: every visitor triggers the full read path again. The failure of an auxiliary service can overload a source that was working normally.

Testing degraded operation therefore starts with a promise to the reader and a work budget. Which data may temporarily come from a retained copy? How many computations may run concurrently? When should the page clearly report that it is unavailable? These answers are needed before disabling a dependency; otherwise, almost any outcome can be called acceptable afterwards.

The independent PHP example runs in one Symfony Demo with MySQL 8.4 and InnoDB through native PDO MySQL. Separate process connections share a schema; the checks focus on origin admission, responses during contention and release only by the permit owner. This is a component experiment without an HTTP request, live cache service or production outage.

A miss and a failure raise different questions

A miss means that no suitable entry exists. A failure means that the store could not be read. Both situations may require a new result, but they differ diagnostically. Widespread misses after startup, a connection error, a full cache and an unavailable origin should not collapse into one “slow” counter.

In this exercise, origin means building a public catalogue from its source. Whether this involves one SQL query, several reads or an external service, the work has a cost and permits a limited number of concurrent calls. We deliberately choose a capacity of one so that contention between two requests is clear. This is a teaching parameter, not a recommendation for a production website.

Symfony Cache describes Cache Contracts' built-in protection against concurrent recomputation: per-host key locking and early refresh. The example below adds a separate application decision: how much work to admit to origin at all when the cache dependency has failed.

A retained copy must remain accessible

The phrase “serve old data” hides a storage question. If the only copy lives in the same unavailable service, it cannot be read. An independent accessible source or an explicit failure is required. A flag permitting an older version does not create its bytes.

The fixture has a separate port for reading a retained copy. It returns only predefined public HTML, with a page key, locale, version and logical creation time. Replacing this port with a real file store would require its own writing, cleanup and availability rules. The example does not present a second port as an already deployed backup service.

The policy permits an ordinary cached result younger than 30 seconds. During contention or an origin error, a retained copy may be used only before it reaches 120 seconds: age 119 is allowed, age 120 is not. A future creation time, another locale or non-public data makes the copy ineligible. Reading it again does not reset its age.

This is an application contract. HTTP caches separately follow RFC 9111, Section 4.2.4: serving a stale response requires an allowed basis, and an explicit prohibition on reuse must be respected. The example adds no browser or CDN cache; its proposed HTTP presenter returns no-store.

Permission to display comes before speed

A snapshot being public when created does not guarantee permission to display it now. A resource may have been withdrawn, or a representation in another locale selected accidentally. Before reading any bytes, the application therefore obtains a current decision about whether this public resource is allowed.

In the synthetic example, this is a trusted service input supplied by the test. It is not taken from a query parameter and does not replace real authorization. A negative scenario denies the resource before consulting the cache, retained copy or origin. The result contains none of their HTML. In a real integration, the policy owner must also define refusal when a current access decision cannot be obtained.

One permit for expensive work

Before origin, there must be an atomic attempt to occupy a shared slot. A variable inside one PHP process cannot constrain a neighbouring process. Likewise, a lock for each individual key does not establish an overall budget for many different pages that call the same source.

The prepared model stores an occupied permit as a row in an InnoDB table. The first process records a random owner token and completes a short transaction. Through a separate native PDO connection to the same schema, the second process sees an occupied slot and does not call origin. The row exists throughout computation; a long SQL transaction is not held for that purpose. InnoDB coordinates competing writes rather than locking an entire file. The MySQL documentation separately describes InnoDB error handling, including lock waits and deadlocks.

The admission store is separate from the failed cache. If admission itself is unavailable, the chosen policy returns 503 without contacting origin or serving a retained copy. This conservative choice simplifies the boundary: unknown permission state does not become unlimited work. Another policy is possible, but it needs a separate definition and test.

Reproducing the experiment requires MySQL 8.4, the PDO MySQL extension and the shared Symfony Demo's guarded disposable MySQL fixture. The App\Infrastructure\MysqlLab helper allocates a schema, and a separate migrator prepares tables before business transactions; the runtime account has DML permissions only, without CREATE, ALTER or DROP. MySQL DDL can implicitly commit, so this preparation is neither an atomic schema upgrade nor an automatic copy of data from the old SQLite store. A shared MySQL server removes the dependency on a coordination file, but does not by itself establish the system's behaviour when a connection is lost.

Capacity is not a rate limit either. If each read completes quickly, one available slot can admit many sequential operations per second. When the source has a separate request budget over time, an additional rate limit is needed. Likewise, a shared slot does not ensure fairness between pages: an active client may win admission more often. The teaching model checks overlapping work, rather than these other properties.

Permit ownership and the cost of recovery

Release deletes the row only for the exact token. An old or unrelated worker cannot remove someone else's permit. The operation sits in finally, so an ordinary origin exception triggers a release attempt. If release cannot be confirmed, a separate event is recorded; the code does not declare capacity free.

The model has no automatic permit expiry. A crashed process may leave its committed permit row in MySQL even after its connection has closed. This deliberately trades availability for simplicity: recovery requires confirming that workers have stopped and their work has ended before clearing an orphaned record. The permit row must not be deleted, nor the coordination store rebuilt, while workers are alive.

A simple TTL lease changes the guarantee. After expiry, a new worker may acquire a permit while its predecessor is still working. Symfony Lock explains this boundary. An owner token does not itself stop an old operation; protecting writes requires enforcement by the resource or another protocol. Our local example does not provide such a distributed mechanism.

Two processes make contention observable

In the executed interleavings, both processes initially waited for a start signal. A acquired the permit, entered origin and paused at a barrier. Only after entry was confirmed did B attempt admission and complete its response while A was still working. After the barrier was released, both workers exited normally with exit 0.

The expectations are fixed beforehand:

Condition for B Expected response New origin read
Permit occupied; eligible retained copy allowed 200, version A with a notice about previously prepared content No
Copy forbidden, expired or unavailable 503 without catalogue data No
Admission store unavailable 503 without bypassing the limit No
A finished and released its permit A subsequent request may obtain version B from origin Yes, after fresh admission

In both interleavings, the separate origin counter during overlap was reads=1, inflight=1, max_inflight=1; after A it was reads=1, inflight=0, max_inflight=1. B never entered origin: allowed retained policy gave A/200, forbidden policy gave 503/null; A received B/200. Exact-owner release allowed a new reservation after completion. These two scenarios do not prove crash recovery, fairness or capacity under every load.

Every dependency needs a timeout

A bounded number of concurrent calls does not guarantee a short response. One admitted worker may stall on the network while every other request is refused. Connection, read, write and coordination waits need separate limits, together with a controlled request lifetime.

In MySQL, a row-lock wait timeout and a deadlock are different failure causes: native errors 1205 and 1213 must not be hidden as ordinary slot contention. The cause propagates, and the adapter rolls back the transaction it owns. If a store error prevents admission from being confirmed, GuardedPage's policy remains fail-closed: 503/unavailable/null, without origin or a retained copy. The InnoDB lock-wait limit is not a deadline for the whole response. This component experiment establishes no guarantees for slow TCP or connection, read and write deadlines.

A separate test called SymfonyPresenter and verified actual HttpFoundation Response objects: 200 origin with fixed HTML; 200 retained with a notice; 503 with Retry-After: 2 and no-store; 403 without catalogue data. Diagnostic failure causes did not enter the response. This tests presentation, not an HTTP route or kernel request. These are four response scenarios within one PHPUnit test, not four additional tests.

Count errors alongside the content shown

Each scenario needs request counts, 503 responses, retained versions served, origin admissions and maximum concurrent work. Calculate the refusal fraction as number of 503 responses / number of completed requests within a predefined set. Do not confuse it with the number of internal errors: a single request can experience a cache failure and successfully obtain origin content.

Record response mode, data version and the reason for each transition separately. An empty page with status 200 is not a successful catalogue read. Nor does 200 with an allowed older version mean that the result is fully current. The presenter shows a short notice about previously prepared content and keeps technical causes in diagnostics.

After recovery, check that the permit can be reused, the correct new version is returned and ordinary cache reads resume. A failed cache write must not corrupt public content that has already been obtained; it should leave a separate signal. Degraded operation is well defined when every response and every additional unit of work has an understandable, permitted reason, including during recovery.

The shared MySQL fixture and its files are introduced in the transaction chapter. Run commands from the single article_demo root; do not create a separate Symfony application for each article.

Check the current example against an owned disposable MySQL schema:

bash fixtures/mysql_labs/run.sh php vendor/bin/phpunit tests/CacheFailureLab
  1. Symfony Core Errors, empty results and missing records: three application responses
  2. Caching When the cache is empty for everyone: examining cache stampedes
  3. Caching Stale results or errors: choosing a stale-data policy

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article