TTL und Ablauf in Redis
Lernen Sie, wie Redis den Ablauf von Schlüsseln verwaltet: TTLs setzen, die Befehle EXPIRE und SETEX verwenden, Lazy und Active Expiry verstehen und nachvollziehen, wie sich maxmemory-Eviction-Policies auf TTLs auswirken.
TTL und Ablauf in Redis ist eine kostenlose Caching Strategies: Redis + CDN + Edge Computing-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 Caching Strategies: Redis + CDN + Edge Computing-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Caching Strategies: Redis + CDN + Edge Computing-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Why Expire Cache Keys?
Cached data goes stale. Time To Live (TTL) lets Redis automatically remove a key after a set duration, so your cache stays reasonably fresh without manual cleanup.
Setting a TTL with EXPIRE
The EXPIRE command sets a timeout in seconds on an existing key. After that time, Redis deletes the key automatically.
SET user:1 "Alice"
EXPIRE user:1 60
TTL user:1SET with EX in One Step
You can set a value and its TTL together. SET key value EX seconds (or the older SETEX) avoids a race between writing and expiring.
SET session:abc "data" EX 300Checking Remaining TTL
The TTL command returns seconds left. PTTL returns milliseconds. Special return values matter:
-1: key exists but has no expiry-2: key does not exist
def interpret_ttl(value):
if value == -1:
return 'no expiry set'
if value == -2:
return 'key does not exist'
return str(value) + 's remaining'
print(interpret_ttl(-2))
print(interpret_ttl(45))Removing an Expiry
The PERSIST command removes the TTL from a key, making it permanent again. Useful when a value should stop expiring after some condition is met.
PERSIST user:1Lazy Expiration
Redis does not check every key constantly. With lazy expiry, an expired key is only removed when someone tries to access it. Until then it lingers in memory.
Active Expiration
To reclaim memory from keys nobody touches, Redis also runs a background sampler that periodically removes a batch of expired keys. This is active expiry. Together with lazy expiry it keeps memory in check.
TTL vs Eviction
TTL is voluntary removal at a set time. Eviction happens when Redis hits its maxmemory limit and must drop keys to make room. They are different mechanisms that often work together.
volatile vs allkeys Policies
Eviction policies decide what to drop under memory pressure:
volatile-lru: evict least-recently-used keys that have a TTLallkeys-lru: evict LRU among all keys
For a pure cache, allkeys policies are common.
Choosing TTL Values
Pick TTLs from the data's tolerance for staleness: seconds for fast-changing prices, hours for catalogs, days for rarely-changing config. Add jitter to avoid synchronized mass expiry.
import random
base = 3600
print('TTL:', base + random.randint(0, 300), 'seconds')Common Pitfall: No TTL
Forgetting to set a TTL turns a cache into an ever-growing store that eventually exhausts memory. For cache use cases, almost every key should have an expiry or rely on an allkeys eviction policy.
Quick Check
What does the Redis command TTL mykey return if the key exists but has no expiration set?
Recap
You learned TTL and expiration in Redis:
- EXPIRE / SET ... EX set timeouts; TTL/PTTL inspect them; PERSIST removes them.
- Redis uses lazy plus active expiration.
- Eviction policies (volatile vs allkeys) handle memory pressure.
- Always set TTLs (or an allkeys policy) for cache keys.
Good expiration keeps a cache both fresh and bounded.
Lerne Caching Strategies: Redis + CDN + Edge Computing mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 12
- Lektionen
- 48
Häufig gestellte Fragen
Ist die Lektion „TTL und Ablauf in Redis“ kostenlos?
Ja — der vollständige Text von „TTL und Ablauf in Redis“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Caching Strategies: Redis + CDN + Edge Computing-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Caching Strategies: Redis + CDN + Edge Computing-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „TTL und Ablauf in Redis“?
Lernen Sie, wie Redis den Ablauf von Schlüsseln verwaltet: TTLs setzen, die Befehle EXPIRE und SETEX verwenden, Lazy und Active Expiry verstehen und nachvollziehen, wie sich maxmemory-Eviction-Polici… Du übst Caching Strategies: Redis + CDN + Edge Computing 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 Caching Strategies: Redis + CDN + Edge Computing zu starten?
Keine Vorkenntnisse erforderlich. Caching Strategies: Redis + CDN + Edge Computing 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 „TTL und Ablauf in Redis“?
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 Caching Strategies: Redis + CDN + Edge Computing-Lektion Code schreiben und ausführen?
Ja. Jede Caching Strategies: Redis + CDN + Edge Computing-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
- Einführung in das Redis-Caching
- Redis-Datenstrukturen für Caches
- Grundlegende Redis-Cache-Operationen
- TTL und Ablauf in Redis