Streams begrenzen und kürzen
Kontrollieren Sie das Speicherwachstum von Streams, indem Sie ihre Länge mit MAXLEN begrenzen, sie anhand der ID mit MINID kürzen und für bessere Leistung approximatives Kürzen verwenden.
Streams begrenzen und kürzen 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.
Streams Grow Forever
Unless told otherwise, a Redis Stream keeps every entry you append. For high-throughput producers this means unbounded memory growth. Capping and trimming let you keep streams to a manageable size.
Capping at Add Time
The cleanest approach is to cap during XADD using MAXLEN. Redis evicts the oldest entries to keep the length at or below the limit.
XADD events MAXLEN 1000 * action login user 42Exact vs Approximate
Exact trimming can be costly. The ~ modifier requests approximate trimming, which is far cheaper because Redis only removes whole macro-nodes.
MAXLEN 1000exactMAXLEN ~ 1000approximate, may keep slightly more
XADD events MAXLEN ~ 1000 * action clickTrimming After the Fact
You can trim an existing stream with XTRIM, using the same options as the XADD capping clause.
XTRIM events MAXLEN 500
XTRIM events MAXLEN ~ 500Trimming by ID with MINID
Instead of length, you can trim by entry ID. MINID removes all entries older than a given ID, which maps naturally to time since IDs start with a millisecond timestamp.
XTRIM events MINID 1716900000000
XADD events MINID ~ 1716900000000 * a 1Time-Window Retention
Because the first part of a stream ID is a Unix millisecond timestamp, MINID lets you keep only the last hour or day of events. Compute the cutoff timestamp in your app and pass it in.
# keep last 24h: cutoff = (now_ms - 86400000)
XTRIM events MINID 1716813600000Limiting Eviction Work
The LIMIT option (with approximate trimming) caps how many entries a single trim removes, smoothing out latency spikes on very large streams.
XTRIM events MAXLEN ~ 1000 LIMIT 100Checking Stream Length
Use XLEN to see the current entry count and XINFO STREAM for detailed metadata including first and last IDs.
XLEN events
XINFO STREAM eventsTrimming and Consumer Groups
Trimming removes entries regardless of whether consumer groups have read them. If a slow consumer falls behind the trim point, those messages are lost for it. Size your cap to cover your slowest consumer's lag.
Choosing a Strategy
Use MAXLEN when you care about count and bounded memory. Use MINID when you care about a time window. Prefer the ~ approximate form in production for performance unless exact bounds are required.
Memory in Practice
Capping at add time is usually best: it keeps memory steady without a separate cleanup job. Reserve XTRIM for ad-hoc cleanups or when retention rules change.
Quick Check
Test your understanding of stream trimming.
Recap
You learned to control stream growth with MAXLEN capping, MINID time-window trimming, the cheaper ~ approximate mode, the LIMIT option to bound eviction work, and the caveat that trimming can drop messages slow consumers have not yet read.
Häufig gestellte Fragen
Ist die Lektion „Streams begrenzen und kürzen“ kostenlos?
Ja — der vollständige Text von „Streams begrenzen und kürzen“ 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 „Streams begrenzen und kürzen“?
Kontrollieren Sie das Speicherwachstum von Streams, indem Sie ihre Länge mit MAXLEN begrenzen, sie anhand der ID mit MINID kürzen und für bessere Leistung approximatives Kürzen verwenden. 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 „Streams begrenzen und kürzen“?
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
- Was sind Redis Streams?
- Datenstrukturen und Befehle für Streams
- Persistenz von Stream-Nachrichten
- Streams begrenzen und kürzen