0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · Lektion

Strategien zur Cache-Invalidierung

Lernen Sie, zwischengespeicherte Daten mit TTLs, Write-through, Write-behind und ereignisgesteuerter Invalidierung in Redis aktuell und konsistent zu halten.

Strategien zur Cache-Invalidierung ist eine kostenlose Redis Caching & Messaging (Pub/Sub, Streams)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Redis Caching & Messaging (Pub/Sub, Streams)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Redis Caching & Messaging (Pub/Sub, Streams)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Why Invalidation Matters

Caching speeds up reads, but a cache that serves stale data can be worse than no cache at all. Cache invalidation is the discipline of removing or refreshing entries when the underlying source of truth changes.

It is famously hard: there are only two hard things in computer science, and one of them is cache invalidation.

TTL-Based Expiry

The simplest strategy is time-to-live. You accept that data may be stale for at most N seconds.

  • SET key value EX 60 expires after 60 seconds
  • EXPIRE key 60 sets a TTL on an existing key
  • TTL key shows remaining seconds
SET user:42:profile "...json..." EX 60
TTL user:42:profile
EXPIRE user:42:profile 120

Write-Through Caching

In write-through caching, every write goes to both the database and the cache in the same operation. Reads are always fresh, at the cost of slower writes.

The application is responsible for keeping the two in lockstep.

def save_user(user):
    db.update(user)
    redis.set('user:' + user.id, serialize(user))
    return user

Write-Behind Caching

Write-behind (write-back) writes to the cache first and flushes to the database asynchronously. Writes are fast, but you risk data loss if Redis goes down before the flush.

Use a queue or stream to buffer pending writes.

redis.set('order:' + id, data)
redis.lpush('pending_writes', id)

Explicit Deletion on Update

The cache-aside + delete pattern: on every update to the database, delete the cached key. The next read repopulates it.

This avoids serving stale data and is simpler than keeping the cache value in sync.

def update_product(p):
    db.update(p)
    redis.delete('product:' + p.id)

Why Delete Beats Update

Deleting the key (instead of overwriting it) avoids a race condition: two concurrent updates could otherwise write values in the wrong order. With deletion, the next read always pulls the latest from the source of truth.

Versioned Keys

Instead of invalidating, bump a version number embedded in the key. Old keys expire naturally via TTL while new reads use the new key.

INCR config:version
GET config:version
SET config:v7:settings "..."

Event-Driven Invalidation

Publish an invalidation event whenever data changes. Other application nodes subscribe and clear their local caches. This pairs Redis Pub/Sub with caching.

redis.publish('invalidate', 'user:42')
# subscribers:
redis.subscribe('invalidate')

Tag-Based Invalidation

Group related keys under a tag set. When a tag becomes invalid, delete all member keys in one sweep.

  • SADD tag:user:42 product:1 product:2
  • On change: SMEMBERS tag:user:42 then DEL each
SADD tag:user:42 cart:42 wishlist:42
SMEMBERS tag:user:42

Avoiding Stampedes

When a hot key expires, many requests may hit the database at once (a cache stampede). Mitigate with a short lock, probabilistic early expiry, or serving slightly stale data while one worker refreshes.

SET lock:user:42 1 NX EX 5

Choosing a Strategy

There is no single best approach:

  • TTL for tolerable staleness
  • Delete-on-write for correctness
  • Event-driven for multi-node consistency
  • Versioning for bulk config changes

Quick Check

Test your understanding of invalidation strategies.

Recap

You learned the major cache invalidation strategies: TTL expiry, write-through, write-behind, delete-on-write, versioned keys, event-driven, and tag-based invalidation, plus how to avoid cache stampedes. Pick the strategy that matches your tolerance for staleness and your consistency needs.

Häufig gestellte Fragen

Ist die Lektion „Strategien zur Cache-Invalidierung“ kostenlos?

Ja — der vollständige Text von „Strategien zur Cache-Invalidierung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Redis Caching & Messaging (Pub/Sub, Streams)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Redis Caching & Messaging (Pub/Sub, Streams)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Strategien zur Cache-Invalidierung“?

Lernen Sie, zwischengespeicherte Daten mit TTLs, Write-through, Write-behind und ereignisgesteuerter Invalidierung in Redis aktuell und konsistent zu halten. Du übst Redis Caching & Messaging (Pub/Sub, Streams) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Redis Caching & Messaging (Pub/Sub, Streams) zu starten?

Keine Vorkenntnisse erforderlich. Redis Caching & Messaging (Pub/Sub, Streams) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Strategien zur Cache-Invalidierung“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Redis Caching & Messaging (Pub/Sub, Streams)-Lektion Code schreiben und ausführen?

Ja. Jede Redis Caching & Messaging (Pub/Sub, Streams)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Fortgeschrittene Cache-Muster
  2. Sitzungsverwaltung mit Redis
  3. Rate Limiting und Anti-Patterns
  4. Strategien zur Cache-Invalidierung
← Zurück zu Redis Caching & Messaging (Pub/Sub, Streams)