Redis Caching & Messaging (Pub/Sub, Streams) · Lección

Limitación y recorte de streams

Controle el crecimiento de memoria de los streams limitando su longitud con MAXLEN, recortándolos por ID con MINID y usando recorte aproximado para mejorar el rendimiento.

Lección 4 de 413 pasos

Limitación y recorte de streams es una lección gratuita de Redis Caching & Messaging (Pub/Sub, Streams) en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Redis Caching & Messaging (Pub/Sub, Streams), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Redis Caching & Messaging (Pub/Sub, Streams) incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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 42

Exact vs Approximate

Exact trimming can be costly. The ~ modifier requests approximate trimming, which is far cheaper because Redis only removes whole macro-nodes.

  • MAXLEN 1000 exact
  • MAXLEN ~ 1000 approximate, may keep slightly more
XADD events MAXLEN ~ 1000 * action click

Trimming 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 ~ 500

Trimming 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 1

Time-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 1716813600000

Limiting 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 100

Checking 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 events

Trimming 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.

Gratis para empezar

Aprende Redis Caching & Messaging (Pub/Sub, Streams) con un tutor de IA — gratis

Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.

Cursos
12
Lecciones
48

Preguntas frecuentes

¿La lección «Limitación y recorte de streams» es gratis?

Sí — el texto completo de «Limitación y recorte de streams» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Redis Caching & Messaging (Pub/Sub, Streams), actualiza a CoddyKit PRO. El curso de Redis Caching & Messaging (Pub/Sub, Streams) incluye 4 lecciones en total.

¿Qué aprenderé en «Limitación y recorte de streams»?

Controle el crecimiento de memoria de los streams limitando su longitud con MAXLEN, recortándolos por ID con MINID y usando recorte aproximado para mejorar el rendimiento. Practicas Redis Caching & Messaging (Pub/Sub, Streams) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Redis Caching & Messaging (Pub/Sub, Streams)?

No se requiere experiencia previa. Redis Caching & Messaging (Pub/Sub, Streams) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Limitación y recorte de streams»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Redis Caching & Messaging (Pub/Sub, Streams)?

Sí. Cada lección de Redis Caching & Messaging (Pub/Sub, Streams) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. ¿Qué son los Redis Streams?
  2. Estructuras de datos y comandos de Streams
  3. Persistencia de mensajes en Streams
  4. Limitación y recorte de streams
← Volver a Redis Caching & Messaging (Pub/Sub, Streams)