Database Management · Advanced level

Advanced level · Module 5: NoSQL and Choosing a Data Store · Lesson 3 of 4

Caching and Consistency

Use caches to reduce database load, and understand the consistency trade-offs of distributed data.

In this lesson you will

  • Use a cache with expiry and invalidation
  • Explain consistency trade-offs in distributed systems

Key concepts

cache-aside patternexpiry (TTL)cache invalidationeventual consistency

The lesson

A cache such as Redis keeps frequently read data in memory. In the cache-aside pattern, the application checks the cache first and, on a miss, reads the database and stores the result with an expiry time.

The hard part is invalidation: when data changes, cached copies must be removed or updated, or users see stale data. Short expiry times limit the damage.

Distributed databases may be eventually consistent: different servers can briefly return different answers. That is acceptable for a news feed but not for a bank balance or a stock level.

Step by step

  1. Cache a slow report result with an expiry.
  2. Measure the speed-up.
  3. Invalidate the cache when the underlying data changes.
  4. Classify five data types by the consistency they need.

Practice activity

Add a cache to a slow dashboard query and document its invalidation rule.

Expected result: The dashboard is faster and changes appear within the documented time.

Check your understanding

  1. What is the cache-aside pattern?

    Show answer

    Check the cache first; on a miss, read the database and store the result in the cache.

  2. Where is eventual consistency unacceptable?

    Show answer

    Where correctness at every moment matters, such as account balances or stock levels.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.