Skip to content

streaming: add stream TTL (absolute and sliding)#65

Merged
raphael merged 4 commits intomainfrom
feat/streaming-stream-ttl
Jan 18, 2026
Merged

streaming: add stream TTL (absolute and sliding)#65
raphael merged 4 commits intomainfrom
feat/streaming-stream-ttl

Conversation

@raphael
Copy link
Member

@raphael raphael commented Jan 18, 2026

What

This adds built-in TTL support for Redis streams in Pulse, configurable via stream options:

  • options.WithStreamTTL(ttl): absolute TTL (set once when the Redis key is created; never extended)
  • options.WithStreamSlidingTTL(ttl): sliding TTL (refreshed on each publish)

TTL is applied in both stream creation paths:

  • Publish path (Stream.Add / XADD) — after the write succeeds
  • Sink path (newSink / XGROUP CREATE ... MKSTREAM) — after the group/stream exists

Docs: added a short “Stream retention (TTL)” section to streaming/README.md.

Why

Many production uses want automatic retention for per-run/per-session streams to avoid leaked keys accumulating indefinitely. Pulse already caps stream length via MaxLen, but length trimming alone doesn’t guarantee eventual cleanup of inactive streams.

How

  • Stream options now carry TTL + TTLSliding.
  • Stream.Add and newSink call a shared internal helper applyTTL:
    • Absolute TTL uses ExpireNX (set only if no TTL exists).
    • Sliding TTL uses Expire (refreshes TTL).
  • NewStream fails fast on negative TTL (ttl < 0).

Behavioral notes

  • TTL starts when the Redis stream key is first created, which can be:
    • first publish (XADD), or
    • sink creation (MKSTREAM) even before the first event.
  • Default remains no TTL unless an option is set.

Test evidence

  • go test ./streaming/options -run TestStreamOptions
  • go test ./streaming -run TestOptions

TTL behavior tests live in streaming/streams_test.go and require a Redis instance (e.g. localhost:6379).

@raphael raphael enabled auto-merge (squash) January 18, 2026 18:46
@raphael raphael merged commit 7743d61 into main Jan 18, 2026
5 checks passed
@raphael raphael deleted the feat/streaming-stream-ttl branch January 18, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant