Skip to content

Python · Redis Streams

Redis Streams
that flow.

Publish and consume with consumer groups, retries, a dead-letter queue and middleware. A few lines of plain Python, and your messages find their way downhill.

pip install rillkit

Two files, one stream

Publish on one side. Consume on the other.

No schemas to register and no consumer loop to write. Name a stream and a group, decorate a function, call run.

publish.py
python
from rillkit import Broker

broker = Broker.from_env()

broker.publish("orders", {"id": 1, "total": 42.5})
worker.py
python
from rillkit import Broker, Stream

broker = Broker.from_env()

@broker.subscribe(Stream("orders", group="workers"))
def handle(order: dict):
    print("charging", order["id"])

broker.run()

What you get

The boring parts, handled.

Redis Streams give you the channel. rillkit digs the rest of the bed.

  • Consumer groups done right

    Groups are created for you, every worker gets its own consumer name, and entries stuck with a crashed worker are claimed back once they sit idle.

  • At-least-once, with retries and backoff

    A failing message comes back after an exponential backoff, up to max_retries. Nothing vanishes quietly.

  • Dead-letter queue with context

    When retries run out, or you reject it, the entry moves to a DLQ stream along with the error, the original id, the group and the delivery count. replay_dlq() sends it back.

  • Ack policies

    Decide what happens when a handler finishes. The default retries on error. nack() retries, reject() skips straight to the DLQ.

    • NACK_ON_ERROR
    • ACK_ALWAYS
    • ACK_FIRST
    • MANUAL
  • Middleware hooks

    Wrap receiving, handling, publishing and cleanup. Idempotent ships in the box to skip duplicate deliveries.

  • Startup and shutdown hooks

    on_startup, after_startup, on_shutdown and after_shutdown, as plain decorators.

  • Plain dicts, Pydantic if you like

    Handlers receive decoded JSON as a dict. Annotate the parameter with a Pydantic model and it is validated for you.

  • Graceful shutdown and heartbeat

    SIGINT and SIGTERM let in-flight messages finish. Live workers publish a heartbeat you can read with active_workers().

  • Interoperable wire format

    One JSON string in the data field of an ordinary stream entry. Anything that speaks Redis can read it or write it.

How it flows

Every message ends up somewhere.

Downhill from publish to handler, then one of three outcomes. None of them is "lost".

Publishbroker.publish()StreamXADD ordersGroupworkersHandleryour functionAckXACK, doneDLQorders-dlqRetryafter backoffredelivered after backoffPublishbroker.publish()StreamXADD ordersGroupworkersHandleryour functionAckXACKDLQorders-dlqRetrybackoff
  • Handler returnsThe entry is acked and leaves the pending list.
  • Handler raisesIt is nacked and delivered again after a growing backoff. Call reject() to skip retries.
  • Retries run out, or you rejectIt moves to the DLQ with the error, original id and delivery count.

Let the messages find their way.

Install it, point it at Redis, and have a worker running before your coffee cools.

pip install rillkit

Released under the MIT License.