--- title: 'Reference: ValkeyStreamsPubSub | PubSub' description: 'ValkeyStreamsPubSub is a PubSub and LeaseProvider implementation backed by Valkey Streams through Valkey GLIDE.' packages: - '@mastra/core' - '@mastra/valkey-streams' --- # ValkeyStreamsPubSub `ValkeyStreamsPubSub` is a [`PubSub`](/reference/pubsub/base) and [`LeaseProvider`](/reference/pubsub/lease-provider) implementation backed by Valkey Streams through [Valkey GLIDE](https://github.com/valkey-io/valkey-glide). It provides persistent cross-process delivery, consumer groups, redelivery, topic cleanup, and distributed leasing. Use it for Valkey deployments. Use [`RedisStreamsPubSub`](/reference/pubsub/redis-streams) for Redis deployments that use the official `redis` client. Both integrations implement the same Mastra behavior, with independent tests running against Valkey and Redis. ## Installation ```bash npm2yarn npm install @mastra/valkey-streams ``` ## Usage example ```typescript title="src/mastra/index.ts" import { Mastra } from '@mastra/core' import { ValkeyStreamsPubSub } from '@mastra/valkey-streams' export const mastra = new Mastra({ pubsub: new ValkeyStreamsPubSub({ url: 'valkey://localhost:6379', }), }) ``` ## Constructor parameters ## Delivery behavior `ValkeyStreamsPubSub` supports pull delivery and doesn't support numeric replay offsets. Grouped subscribers compete for events through a shared consumer group. Subscribers without a group receive fan-out delivery through private consumer groups. Use `startFrom: "latest"` to skip retained entries when a group is first created. The default, `"earliest"`, reads retained entries first. When a subscriber calls `nack`, the event is republished with an incremented `deliveryAttempt` and the original is acknowledged. Once an event reaches `maxDeliveryAttempts`, it's dropped instead of redelivered. Separately, each subscription periodically reclaims events that an earlier consumer in the group read but never acknowledged, controlled by `reclaimIntervalMs` and `reclaimIdleMs`. A subscription never reclaims an event its own handler is still processing, so a slow handler isn't invoked twice for the same event. If a handler hangs, a different consumer in the group reclaims the event once it has been idle for `reclaimIdleMs`. In a single-consumer group there's no sibling to do that, so set `inFlightTimeoutMs` to have the subscription nack the event itself after that long. ## Server configuration Set `maxmemory-policy` to `noeviction`. Consumer groups, unacknowledged entries, and lease keys are live state, and any evicting policy deletes them when the server reaches `maxmemory`, dropping events and leases. When `maxmemory` is set and the policy isn't `noeviction`, `ValkeyStreamsPubSub` logs a warning once after it first connects. To reclaim streams created without a TTL, follow the [Redis Streams runbook](/reference/pubsub/redis-streams#reclaim-streams-created-without-a-ttl) with `valkey-cli`. ## Cleanup and shutdown `clearTopic(topic)` deletes a topic stream and its consumer groups; Mastra's run lifecycles call it when a run ends, and a stream that never reaches the call is reclaimed by its sliding idle TTL instead. `flush()` waits for in-flight publishes, and `close()` stops subscriptions and closes GLIDE connections. ```typescript await pubsub.clearTopic('workflow.events.run-123') await pubsub.flush() await pubsub.close() ```