Expand description
Redis and Valkey broker for the RustStream messaging
framework, backed by the fred client.
Three Redis transports sit behind one broker. Redis Streams is the durable one, a log read
through consumer groups with acknowledgement, redelivery and crash recovery. A Redis list is a
work queue whose every entry goes to one consumer. Pub/Sub delivers to the subscribers connected
when a message is published. Four topologies serve all three: a standalone server, a cluster, a
sentinel set, and a fred Pool a service built for itself.
§A service
A handler is an async fn over the decoded payload, mounted on a broker inside the application
object, and the attribute writes the main:
use ruststream_fred::stream::prelude::*;
use serde::Deserialize;
#[derive(Deserialize)]
struct Order {
id: u64,
}
#[subscriber("orders")]
async fn handle(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "accepted");
HandlerOutcome::ack()
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
RedisBroker::standalone("redis://localhost:6379").default_group("workers"),
|b| {
b.include(handle);
},
)
}cargo run -- run starts it. RedisBroker::standalone only records the address; the runtime
connects the broker at startup and shuts it down when the service stops. A bare key in
#[subscriber] reads that stream through the broker’s default consumer group.
§Where things are
The guides take a Redis service from installation to production, in this order:
getting started, choosing a transport,
consumer groups and crash recovery, work queues on lists,
retries, delays and dead letters, fan-out with Pub/Sub,
side effects with the acknowledgement, transactions,
replay and seeking, Redis Cluster, running in production,
testing and interoperating with other clients. The
architecture pages show how Redis delivers messages and where the crate sits
between fred and the runtime, and the benchmarks what it costs.
Each topic below links to its component page on the site, which shows it in a service; the item’s own documentation keeps the detail.
§Broker
- RedisBroker:
RedisBroker, connected intoConnectedRedisBrokerand closed intoClosedRedisBroker. - Cluster and Sentinel:
RedisBroker::clusterandRedisBroker::sentinel. - from_pool:
RedisBroker::from_poolwraps afredPoolthe service built. - Pool:
RedisBroker::pool, the connections publishes and settlements share. - default_group:
RedisBroker::default_group, the group a bare key reads through. - Authentication:
RedisBroker::credentialsandRedisBroker::password. - TLS:
RedisBroker::tlswith thetls-rustls,tls-rustls-ringortls-native-tlsfeature. - sentinel_credentials:
RedisBroker::sentinel_credentialsandRedisBroker::sentinel_password(featuresentinel-auth). - credential_provider:
RedisBroker::credential_provider, rotating credentials (featurecredential-provider). - Shutdown:
ClosedRedisBroker, andRedisError::ShutDownfor a handle used afterwards. - Errors:
RedisError. - Prelude:
stream::prelude,list::prelude,pubsub::preludeand the mixed-formprelude.
§Subscriptions
- Subscriptions: the descriptors, what each settles and where its retry copy goes.
- block:
RedisSubscribeExt::block, how long one read waits on the server. - Batches: a slice handler, a native
COUNTon a stream. - Framing: the binary frame or the codec envelope of a list entry and a channel message.
§Streams
- RedisStream:
RedisStream, a stream read through a consumer group. - reclaim:
RedisStream::reclaim, the entries a dead consumer never acked. - claiming:
RedisStream::claiming, pending and fresh entries in one read (Redis 8.4). - StreamStart:
StreamStart, where a new group starts.
§Positions
- RedisGroupPosition:
RedisGroupPosition, where a consumer group resumes. - start_at: a group moved to a position on every startup.
- Seek:
RedisGroupSeeker, the group moved from a handler. - EntryId:
EntryId, a stream entry’s id.
§Lists
- Lists:
RedisList, a work queue. - reliable:
RedisList::reliable, at-least-once through a processing list. - Recovery:
RedisList::recovery_zset, the watchdog for a dead consumer’s entries. - processing:
RedisList::processing, and the hash tag a cluster needs.
§Pub/Sub
- PubSub:
RedisPubSub, one channel. - RedisPubSubPattern:
RedisPubSubPattern, every channel a glob matches. - PubSubMode:
PubSubMode, classic or sharded delivery. - buffer:
RedisPubSub::buffer, what a busy subscription holds.
§Acknowledgement
- Acknowledgement: what
ackandnackdo on each form. - retry_after: a delayed retry through the runtime’s copy.
- DelayedRetry:
DelayedRetry, a delay queue that survives a restart. - dead_letter: the retry cap and where a spent message goes.
- Counts:
DELIVERY_COUNT_HEADERandIDLE_MS_HEADERfrom the claiming modes. - out_retry: where a retry copy goes, named at the mount site.
§Context
- StreamContext:
context::StreamContextand the keys incontext::keys. - ConsumerGroup:
context::keys::ConsumerGroup. - FredPool:
context::keys::FredPool, the connection pool in a handler. - StreamBatchContext:
context::StreamBatchContext, a batch handler’s context. - PubSubContext:
context::PubSubContext, the channel a message came on.
§Pipelining
- pipeline:
pipeline::RedisPipelineSteps, a window that settles a subscription’s reads together with its handlers’ commands. - atomic: one
MULTI/EXECper delivery. - Commands:
pipeline::RedisPipeline, the commands a handler queues. - bind:
pipeline::Bindable, a slot’s publishes in the delivery’s round. - InRound:
pipeline::InRound, the reply in the delivery’s round. - Flush: when a window leaves, and what each outcome does to it.
§Publishing
- RedisPublish:
RedisPublish,XADDto a stream, and its liveRedisPublisher. - RedisDefaultPublish:
RedisDefaultPublish, each name written the way the service reads it. - RedisPubSubPublish:
RedisPubSubPublish,PUBLISHorSPUBLISH. - RedisListPublish:
RedisListPublish,LPUSHwith an optional key expiry. - partition_key:
RedisPublishSteps::partition_keyandRedisPublishOptions.
§Transactions
- Transactions:
RedisTransactionalPublish, oneMULTI/EXECblock of publishes on a stream publisher; a cluster refuses it at startup. - transaction:
RedisTransaction, a transaction that owns its buffer. - owned_transaction: the same over values.
§Tooling
- AsyncAPI: the
x-ruststream-redisextension of the generated document, with its server, channels and operations (featureasyncapi). - Testing: the in-process Redis
TestAppconnectsRedisBrokerto (featuretesting), and Live runs the same test against a server.
§Cargo features
All off by default: the crate ships the three transports and the broker with no feature on.
testing adds the in-process mode TestApp::start connects the broker through, and asyncapi
the Redis extension of the generated document. tls-rustls, tls-rustls-ring and
tls-native-tls map onto fred’s TLS backends. sentinel-auth adds credentials of their own
for the sentinels, and credential-provider rotating credentials through a callback.
Re-exports§
pub use list::RedisList;pub use list::RedisListMessage;pub use list::RedisListPublish;pub use list::RedisListPublisher;pub use list::RedisListSubscriber;pub use pipeline::RedisListAtomic;pub use pipeline::RedisListPipeline;pub use pipeline::RedisPubSubAtomic;pub use pipeline::RedisPubSubPatternAtomic;pub use pipeline::RedisPubSubPatternPipeline;pub use pipeline::RedisPubSubPipeline;pub use pipeline::RedisStreamAtomic;pub use pipeline::RedisStreamPipeline;pub use pubsub::PubSubMode;pub use pubsub::RedisPubSub;pub use pubsub::RedisPubSubMessage;pub use pubsub::RedisPubSubPattern;pub use pubsub::RedisPubSubPublish;pub use pubsub::RedisPubSubPublisher;pub use pubsub::RedisPubSubSubscriber;pub use stream::RedisStream;pub use stream::StreamStart;
Modules§
- context
- Per-delivery and per-batch context fields exposed to handlers.
- list
- Redis lists: a work queue whose every entry goes to one consumer.
- pipeline
- Consumer-side pipelining: a subscription settles in a window, and a handler queues its own Redis commands into the delivery’s segment of that window.
- prelude
- The core prelude plus everything a service mixing all three Redis forms writes.
- pubsub
- Redis Pub/Sub: delivery to the subscribers connected when a message is published.
- stream
- Redis Streams: a stream read through a consumer group, the durable form.
Structs§
- Closed
Redis Broker - The terminal witness returned by shutting down a
ConnectedRedisBroker. - Connected
Redis Broker - The typed witness that
Broker::connectsucceeded: it owns the livefredpool. - EntryId
- A Redis Streams entry id: the
<milliseconds>-<sequence>pair the server assigns to every entry. - Redis
Broker - An unconnected Redis broker: the recorded topology and its options, no I/O performed yet.
- Redis
Default Publish - The broker’s default policy, what a registration publishes through when its mount site names none: a reply, a retry copy, a dead-letter move.
- Redis
Default Publisher - The live form of
RedisDefaultPublish: writes each name the way this connection’s subscriptions read it. Cheap to clone. - Redis
Group Seeker - Moves a consumer group’s cursor.
- Redis
Message - A Redis Streams delivery, read from a consumer group via
XREADGROUPorXAUTOCLAIM. - Redis
Publish - The declaration half of the stream publisher: pure policy, constructible anywhere.
- Redis
Publish Options - The per-message settings of every publisher and transaction in this crate.
- Redis
Publisher - The live stream publisher:
RedisPublishpaired with a connection. Cheap to clone. - Redis
Subscriber - A Redis Streams subscription bound to a consumer group.
- Redis
Transaction - An owned Redis transaction, opened by
transactionon aRedisPublisher. - Redis
Transactional Publish - The stream publisher’s policy for a mount that publishes in transactions: it pairs into the same
RedisPublisherasRedisPublish, and only where the topology can run one. - TlsConfig
- TLS configuration for a client.
Enums§
- Delayed
Retry - How a subscription should handle
retry_after/nack_afterdelays. - Redis
Error - Errors surfaced by the Redis broker implementation.
- Redis
Group Position - Where a consumer group’s cursor sits, for
RedisGroupSeeker::seekand thestart_at(..)clause of#[subscriber]. - TlsConnector
- An enum for interacting with various TLS libraries and interfaces.
Constants§
- DELIVERY_
COUNT_ HEADER - Header carrying the native Redis Streams delivery count, so a handler can branch on how many attempts a message has already had.
- IDLE_
MS_ HEADER - Header carrying how long (milliseconds) the delivery had been pending before this read. Zero on
an entry a
claimingsubscription read off the tail. - PARTITION_
KEY_ HEADER - The well-known header key for per-message routing / partitioning.
Traits§
- Credential
Provider - A trait that can be used to override the credentials used in each
AUTHorHELLOcommand. - Redis
Publish Steps - The steps this crate adds to the publish builder.
- Redis
Subscribe Ext - The Redis subscription options a mount site names, chained after the core’s settings steps.