跪拜 Guibai
← All articles
Frontend · Backend · Full-Stack

Redis Isn't localStorage: A Backend Caching Primer for Frontend Devs

By swipe ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Backend caching mistakes—like forgetting a TTL, deleting the cache before a DB write, or letting a Redis outage crash a read endpoint—affect every user at once, not just a single browser tab. This walkthrough shows how to structure a cache layer that degrades gracefully, resists penetration, and stays out of the way of the source of truth.

Summary

A full-stack e-commerce project demonstrates how to introduce Redis caching for product details, using a layered architecture that separates low-level Redis commands from business orchestration. The design enforces explicit cache states—HIT, MISS, NULL_HIT, DEGRADED, BYPASS—instead of relying on ambiguous null checks, making the data flow predictable and testable.

The implementation follows the Cache Aside pattern: the application checks Redis first, falls back to MySQL on a miss, and backfills the cache. After any database write, the corresponding cache key is deleted to prevent stale data. A short-TTL null-value marker for non-existent products prevents cache penetration from repeatedly hammering the database.

When Redis is unavailable, the service degrades gracefully by querying MySQL directly rather than failing the entire request. The article also maps each backend concept to a familiar frontend analogy, from localStorage TTLs to discriminated unions, and includes hands-on verification steps using Docker, curl, and redis-cli.

Takeaways
Redis is an independent network service, not a browser cache or an in-process Java Map; it is shared across all users and backend instances.
MySQL remains the source of truth; Redis is a performance layer whose data must be rebuildable from the database.
Every cache key needs a deliberate naming convention (system, module, object, version, ID) to avoid collisions and ease debugging.
All cached values require a TTL; permanent caches risk serving stale data indefinitely and wasting memory.
Using an explicit cache-lookup object (HIT, MISS, NULL_HIT, DEGRADED, BYPASS) eliminates the ambiguity of returning null.
Short-TTL null-value markers for stable 404s prevent cache penetration without blocking newly created resources for long.
A fail-open strategy on Redis exceptions lets the interface degrade to MySQL instead of becoming completely unavailable.
The Cache Aside pattern mandates updating the database first, then deleting the cache; reversing that order risks inconsistency.
Corrupted cache entries should be deleted immediately on detection to avoid repeated deserialization failures until TTL expiry.
Logging cache events (MISS, HIT, DEGRADED, etc.) is essential for troubleshooting slow endpoints in production.
Conclusions

Framing Redis through frontend analogies—Map, localStorage, discriminated unions—lowers the barrier for full-stack transitions without dumbing down the engineering.

The project’s decision to wrap StringRedisTemplate in a thin RedisOperatorClient mirrors the frontend best practice of centralizing API calls instead of scattering fetch requests.

Explicit cache states (HIT, MISS, NULL_HIT, DEGRADED, BYPASS) turn a fuzzy null-check into a state machine that is straightforward to test and reason about.

Writing a null-value cache for stable 404s is a pragmatic, low-cost defense against cache penetration that doesn’t require a Bloom filter or complex heuristics.

The insistence on deleting corrupted cache entries on sight, rather than waiting for TTL, shows a production-minded attention to self-healing behavior.

Accepting brief inconsistency after a write—by deleting the cache rather than updating it—is a deliberate trade-off that prioritizes simplicity and correctness for read-heavy display data.

Concepts & terms
Cache Aside Pattern
An application-level caching strategy where code checks the cache first; on a miss, it loads data from the database, writes it to the cache, and returns it. Writes update the database first, then invalidate the cache.
TTL (Time To Live)
A duration set on a cache entry after which it is automatically deleted. Essential for preventing stale data and unbounded memory growth.
Cache Penetration
A scenario where requests for data that exists in neither the cache nor the database repeatedly hit the database. Mitigated by caching a short-lived null marker for non-existent keys.
Fail-Open (Degradation)
A resilience strategy where a system continues to serve requests by falling back to a slower or less optimal path (e.g., querying the database directly) when a dependency like Redis is unavailable, rather than failing outright.
Null-Value Caching
Storing a special marker in the cache for keys known to correspond to non-existent data, with a short TTL, to prevent repeated, pointless database queries for the same missing resource.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗