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

Schlüsselablauf und TTL in Redis

Lernen Sie, wie Sie Schlüsseln in Redis Time-to-Live-Werte zuweisen, damit sie automatisch ablaufen, welche Befehle die TTL steuern und wann Redis abgelaufene Schlüssel entfernt.

Schlüsselablauf und TTL in Redis 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 Keys Need to Expire

Redis often holds temporary data — sessions, one-time codes, cached responses. Expiration lets a key delete itself after a set time so memory stays lean.

Setting a TTL with EXPIRE

EXPIRE attaches a time-to-live in seconds to an existing key. Once the clock runs out, the key vanishes. See the example below.

SET session:42 "active"
EXPIRE session:42 3600

Set and Expire in One Step

Set value and TTL together with SET ... EX — safer than two separate commands, since there's no window where the key has no expiry.

SET otp:99 "123456" EX 300

Checking Remaining Time

TTL returns the seconds left on a key. It returns -1 if the key has no expiry, and -2 if the key doesn't exist at all.

TTL session:42

Millisecond Precision

For finer control, PEXPIRE and PTTL work in milliseconds instead of seconds. The code below sets and reads a millisecond TTL.

PEXPIRE otp:99 1500
PTTL otp:99

Expiring at a Specific Time

EXPIREAT takes a Unix timestamp, so a key can die at an exact wall-clock moment — like precisely at midnight.

EXPIREAT daily:report 1893456000

Removing an Expiry

PERSIST strips a key's TTL, making it permanent again — handy when temporary data gets promoted to durable.

PERSIST session:42
TTL session:42

How Redis Deletes Expired Keys

Redis removes expired keys two ways: lazy deletion checks on access, and active deletion samples keys in a background task and purges expired ones.

Writes Can Clear TTL

Watch out: a plain SET (no EX) overwrites a value and clears its existing TTL. Modifying commands like APPEND or INCR keep the TTL intact.

Common Use Cases

TTL powers real patterns: auto-expiring sessions, rate-limit counters, one-time passwords and verification tokens, and caches that refresh after N seconds.

Keyspace Notifications on Expiry

Redis can publish an event when a key expires via keyspace notifications, so your app can react. Enable expired-key events, then subscribe. See below.

CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expired

Quick Check

Test your expiration knowledge.

Recap

Recap: EXPIRE and SET ... EX set a TTL, TTL reads it (-1 none, -2 missing), PEXPIRE/EXPIREAT give finer control, PERSIST removes it, plain overwrites clear it.

Häufig gestellte Fragen

Ist die Lektion „Schlüsselablauf und TTL in Redis“ kostenlos?

Ja — der vollständige Text von „Schlüsselablauf und TTL 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 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 „Schlüsselablauf und TTL in Redis“?

Lernen Sie, wie Sie Schlüsseln in Redis Time-to-Live-Werte zuweisen, damit sie automatisch ablaufen, welche Befehle die TTL steuern und wann Redis abgelaufene Schlüssel entfernt. 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 „Schlüsselablauf und TTL 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 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. Einführung in Redis
  2. Grundlagen der Redis-CLI
  3. Zentrale Redis-Datenstrukturen
  4. Schlüsselablauf und TTL in Redis
← Zurück zu Redis Caching & Messaging (Pub/Sub, Streams)