How it works
A cache is fast, usually in-memory storage for results you expect to need again. In the common cache-aside pattern, a hit returns a stored result without querying the database. On a miss, the application reads from the database, stores the result for later, and returns it. Frequently read, rarely changed data is a strong candidate. Important data still belongs in persistent storage.
The hit rate, the share of lookups answered by the cache, is what protects the database. With an example load of 10,000 reads per second, a 95% hit rate leaves the database 500 queries per second. At 90% it sees 1,000: five points of hit rate doubled its load. If a cache outage drops the rate to zero, all 10,000 arrive at once. Multiple cache nodes, spare capacity, and a deliberate fallback help prevent a speed optimization from becoming an availability problem. Redis and Memcached are the usual tools.
An expiration time, or time to live (TTL), that is too short causes repeated database work; too long risks stale answers. Updates may need explicit cache invalidation or replacement. When memory fills, an eviction rule such as least recently used, least frequently used, or first in first out chooses what to remove.
Do not cache what must be exactly current, such as a balance checked before a payment, or what is rarely requested twice. Each cache adds a system to run and a new way to show wrong data.
A product page is popular, but its price changes throughout the day.
Cache stable product details and give the price a short lifetime or explicit invalidation on updates. This reduces repeated reads, but stale prices remain possible if database and cache changes are not coordinated.
Use this as a guide for self review; written answers are not graded.Keep in mind
- A cache speeds repeated reads but may serve stale data.
- Expiration, invalidation, eviction, and failure handling are separate decisions.