0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · レッスン

ストリームの上限設定とトリミング

MAXLENで長さに上限を設定し、MINIDでIDに基づいてトリミングし、パフォーマンスのために近似トリミングを使ってストリームのメモリ増加を制御します。

「ストリームの上限設定とトリミング」はCoddyKit上の無料Redis Caching & Messaging (Pub/Sub, Streams)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 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.

よくある質問

「ストリームの上限設定とトリミング」レッスンは無料ですか?

はい。「ストリームの上限設定とトリミング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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)を演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Redis Streams とは?
  2. ストリームのデータ構造とコマンド
  3. ストリームメッセージの永続化
  4. ストリームの上限設定とトリミング
← Redis Caching & Messaging (Pub/Sub, Streams)に戻る