Treat Redis cache lookup failure as a cache miss when the primary data source remains available.
The examples focus on the operation itself, including the important distinction: A cache outage can degrade performance; it should not corrupt primary records.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
Read the input requirements before applying this pattern to production data.
Example 1: Basic Use
A cache miss or outage can fall back to primary computation. Never use this pattern when Redis is authoritative.
<?php
function loadReport(int $id): array { return ["id" => $id, "status" => "ready"]; }
$result = null;
try {
$redis = new Redis();
$redis->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379, 0.2);
$cached = $redis->get("report:3");
if ($cached !== false) { $result = json_decode($cached, true, 512, JSON_THROW_ON_ERROR); }
} catch (RedisException $e) { error_log("Optional cache unavailable"); }
$result ??= loadReport(3);
echo json_encode($result);
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
A cache miss or outage can fall back to primary computation. Never use this pattern when Redis is authoritative.
<?php
function loadReport(int $id): array { return ["id" => $id, "status" => "ready"]; }
$result = null;
try {
$redis = new Redis();
$redis->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379, 0.2);
$cached = $redis->get("report:8");
if ($cached !== false) { $result = json_decode($cached, true, 512, JSON_THROW_ON_ERROR); }
} catch (RedisException $e) { error_log("Optional cache unavailable"); }
$result ??= loadReport(8);
echo json_encode($result);
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
A cache miss or outage can fall back to primary computation. Never use this pattern when Redis is authoritative.
<?php
function loadReport(int $id): array { return ["id" => $id, "status" => "ready"]; }
$result = null;
try {
$redis = new Redis();
$redis->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379, 0.2);
$cached = $redis->get("report:21");
if ($cached !== false) { $result = json_decode($cached, true, 512, JSON_THROW_ON_ERROR); }
} catch (RedisException $e) { error_log("Optional cache unavailable"); }
$result ??= loadReport(21);
echo json_encode($result);
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
Never silently ignore failure if Redis holds authoritative data such as required locks or session state.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Handling Redis Cache Failures Gracefully | Treat Redis cache lookup failure as a cache miss when the primary data source remains available. |
| Related API or alternative | A cache outage can degrade performance; it should not corrupt primary records. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Never silently ignore failure if Redis holds authoritative data such as required locks or session state. |
| Confusing it with a related API | A cache outage can degrade performance; it should not corrupt primary records. |
Key Takeaways
- Treat Redis cache lookup failure as a cache miss when the primary data source remains available.
- Never silently ignore failure if Redis holds authoritative data such as required locks or session state.
- A cache outage can degrade performance; it should not corrupt primary records.
Use Handling Redis Cache Failures Gracefully when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.