oncely
Idempotency keys and a transactional outbox for async SQLAlchemy and Postgres.
Two small tools for the same problem: a network that retries.
- Idempotency keys. A client that times out sends the request again. The work runs once, and every retry gets the first answer, byte for byte.
- Transactional outbox. "Save the order, then publish an event" can do one without the other. Here the event is a row written in the same transaction as the change, and a relay publishes it afterwards.
About 400 lines, typed, no framework required. It was extracted from lobbyd, a ticketing backend, where it sits under every purchase and every payment.
Install
pip install "oncely[redis]"
The redis extra adds a Redis Streams publisher and consumer. Without it the
package needs only SQLAlchemy 2 and a Postgres driver such as asyncpg.
Idempotency keys
Attach the table to your metadata, so your own migrations create it:
from oncely import IdempotencyKeys, Refused, fingerprint
keys = IdempotencyKeys(Base.metadata)
Then wrap the work. This is a FastAPI route, but nothing here depends on it:
@router.post("/orders", status_code=201)
async def create_order(
body: NewOrder,
request: Request,
user: CurrentUser,
session: Session,
key: str = Header(alias="Idempotency-Key"),
):
async def work() -> tuple[int, str]:
seat = await claim_seat(session, body.seat_id)
if seat is None:
raise Refused(409, '{"code": "seat_taken"}')
order = await place_order(session, user, seat)
return 201, order.model_dump_json()
outcome = await keys.run(
session,
scope=str(user.id),
key=key,
fingerprint=fingerprint(request.method, request.url.path, body.model_dump_json()),
work=work,
)
return Response(outcome.body, outcome.status, media_type="application/json")
What you get:
| Situation | Result |
|---|---|
| First request | work runs. Its answer is stored in the same transaction as its writes. |
| Retry after success | The stored answer. work does not run. outcome.replayed is true. |
| Retry while the first is still running | Waits for the first to finish, then gets its answer. |
work raises Refused |
Its writes are undone, the refusal is stored, and retries get it again. |
work raises anything else |
Nothing is stored. The retry starts from scratch. |
| Same key, different request | KeyReused is raised. That is a client bug, not a retry. |
The caller owns the transaction. Commit, and the answer is permanent. Roll back, and the key is free again. There is no state in which the work happened but the key was not recorded.
scope keeps callers apart: two users who pick the same key never see each
other's responses. Run keys.prune(session, older_than=timedelta(hours=24))
on a timer to drop keys past your retry window.
Transactional outbox
from oncely import Outbox
outbox = Outbox(Base.metadata)
async with sessionmaker.begin() as session:
order.status = "paid"
await outbox.emit(session, "order.paid", {"order_id": str(order.id)})
# Committed together, or not at all.
A relay moves events out. It takes any async function as the publisher:
from oncely.streams import stream_publisher
publish = stream_publisher(redis, "shop:events")
while True:
if not await outbox.relay_once(sessionmaker, publish):
await asyncio.sleep(0.2)
- Events leave oldest first.
- Run as many relays as you like. Rows are taken with
FOR UPDATE SKIP LOCKED, so two relays never publish the same batch. - If the publisher raises, nothing is marked and the batch is offered again.
- If the process dies between publishing and committing, the batch is published twice. Delivery is at least once, so handlers must be safe to repeat.
outbox.lag_seconds(session)is the age of the oldest unpublished event. It is the one number worth an alert.
headers on emit carries context that is not part of the event's meaning,
such as a trace id, so a handler can continue the trace of the request that
caused it.
Consuming from Redis Streams
from oncely.streams import StreamConsumer
worker = StreamConsumer(redis, stream="shop:events", max_attempts=5)
async def send_receipt(event):
await mail.send(event.payload["order_id"])
worker.subscribe("order.paid", send_receipt)
while True:
await worker.run_once(block_ms=1000)
Each event goes to one worker in the group. If a handler raises, the event is
retried after retry_after_ms. If its worker dies, another takes it over.
After max_attempts it moves to shop:events:dead, so one bad message cannot
block the rest. around wraps each event's handling (a span, log context) and
on_result reports how each attempt ended (metrics).
What it does not do
- Exactly-once delivery. Nothing does, across a network. The name refers to the work behind a key, which does run once.
- Other databases. It uses Postgres features on purpose:
ON CONFLICT,SKIP LOCKED, partial indexes. - Migrations. The tables join your metadata, and Alembic's autogenerate picks them up like any other.
Tests
The suite runs against a real Postgres and Redis, including the races: twelve retries of one key at once, and two relays competing for the same events.
ONCELY_TEST_DATABASE_URL=postgresql+asyncpg://user:pass@localhost/db \
ONCELY_TEST_REDIS_URL=redis://localhost:6379/14 \
pytest
It works inside its own Postgres schema (oncely_test) and drops it when done.
License
MIT
Metadata
Release files for oncely 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| oncely-0.1.0.tar.gz | 57.2 kB | With provenance | Details for oncely-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| oncely-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for oncely-0.1.0-py3-none-any.whl |
Total release size: 70.0 kB
Release files / oncely-0.1.0.tar.gz
| Download URL | oncely-0.1.0.tar.gz |
|---|---|
| Size | 57.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fed47787eb34380594f1c395d95ad79abcad7d406db4076c13d73721f3a8b55d
|
|
BLAKE2b-256 checksum How to use checksums |
5e74ccfd9661f7d70605cdda38801107bde02b8585fbdc025bbca5e741bd7eca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logRelease files / oncely-0.1.0-py3-none-any.whl
| Download URL | oncely-0.1.0-py3-none-any.whl |
|---|---|
| Size | 12.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a31c0eac75aa51e34acfb89b564c97cb885b6ac313a29598de6c495da33289a0
|
|
BLAKE2b-256 checksum How to use checksums |
68ecdfec779648e978b7db67ab18d9171efc3a5fd172c069f58242f8089fc2b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logSecurity for oncely 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for oncely-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.