限制与裁剪流
使用 MAXLEN 限制长度、使用 MINID 按 ID 裁剪,并通过近似裁剪提升性能,从而控制流的内存增长
限制与裁剪流 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
用 AI 导师学习 Redis Caching & Messaging (Pub/Sub, Streams) — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「限制与裁剪流」课时是免费的吗?
是的 — 「限制与裁剪流」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
「限制与裁剪流」这节课中我会学到什么?
使用 MAXLEN 限制长度、使用 MINID 按 ID 裁剪,并通过近似裁剪提升性能,从而控制流的内存增长 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「限制与裁剪流」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?
能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。