Skip to main content

ekbatan-py

A persistence framework for event-driven systems, with an ergonomic built-in outbox pattern. The Python port of Ekbatan.

A service that publishes domain events usually writes twice - once to its database, once to its broker - and the two disagree whenever the second write fails. The transactional outbox is the established answer, normally bolted on as a separate library or assembled by hand.

This folds it into the persistence layer, with three pieces.

A model emits an event when it changes.

@dataclass(frozen=True, kw_only=True)
class Wallet(Model[UUID, WalletState]):
    owner_id: UUID
    currency: str
    balance: Decimal

    def validate(self) -> None:
        if self.balance < 0:
            raise ValidationError("balance cannot be negative")

    def deposit(self, amount: Decimal) -> "Wallet":
        if amount <= 0:
            raise ValidationError(f"deposit amount must be positive, got {amount}")
        new_balance = self.balance + amount
        return (
            self.copy()
            .with_event(WalletMoneyDepositedEvent(model_id=str(self.id), amount=amount, new_balance=new_balance))
            .balance(new_balance)
            .build()
        )

An action stages changes on a plan instead of writing them.

class WalletDepositMoneyAction(Action["WalletDepositMoneyAction.Params", Wallet]):
    def __init__(self, clock: Clock, wallet_repository: WalletRepository) -> None:
        super().__init__(clock)
        self.wallet_repository = wallet_repository

    def perform(self, principal: str | None, params: Params) -> Wallet:
        wallet = self.wallet_repository.get_by_id(params.wallet_id.value)
        return self.plan.update(wallet.deposit(params.amount))

The executor opens one transaction, writes the rows and their events, and commits.

executor.execute(principal, WalletDepositMoneyAction, WalletDepositMoneyAction.Params(wallet_id=..., amount=Decimal("50.00"), ...))

That is the whole surface for the common case. There is no transaction to open, nothing to flush, nothing to publish, and no mention of an outbox anywhere above - which is the point. State and events land together or neither does.

Install

uv add "ekbatan[postgres]"          # or [mysql], or [mariadb]

The extras are the drivers and the optional pieces, and nothing you do not ask for is installed. postgres brings psycopg 3, mysql and mariadb bring PyMySQL, migrations brings yoyo, redis brings the Redis-backed keyed lock, and codegen brings what schemagen needs to start a container at build time - a development dependency, not a runtime one:

uv add --dev "ekbatan[codegen]"

Python 3.13 or newer. SQLAlchemy 2.0 is the only unconditional dependency, alongside ekbatan-py-db-scheduler - the port of kagkarlsson/db-scheduler, which this repository also publishes because there is no upstream for it on PyPI.

The package is typed and ships py.typed, so a checker reads its annotations rather than ignoring them - which is also why the classifiers claim Typing :: Typed truthfully.

Versioning and support

Semantic versioning, and a version number shared with nothing else: this port's versions do not track the Java framework's. ekbatan-py 0.4 does not mean ekbatan 0.4. What the two do share is the on-disk contract - the outbox schema, the lock names, the scheduled_tasks table - and that is pinned by ekbatan-spec rather than by a version number. A Java service and a Python service interoperate when both pass the same spec revision, whatever they call themselves.

While the major version is 0, a minor bump may break an API. Pin accordingly: ekbatan~=0.4.0. Anything reached through ekbatan.<name> is public; anything under an underscore is not, and the _holder module is the example - it exists for the keyed lock and will change without notice.

Releases are cut from this repository to PyPI with trusted publishing - no token is stored anywhere, and every artifact carries a PEP 740 attestation. RELEASE.md is the procedure.

Built on

SQLAlchemy 2.0, used as a typed statement builder rather than as an ORM. Declarative classes describe the schema, because that is where the type information lives - select(Wallet.id, Wallet.balance) checks as Select[tuple[UUID, Decimal]] - and statements execute on a Connection. The one this framework hands you is Db, a subclass that adds rows(...) for turning rows into objects and takes nothing away. There is no Session anywhere: its identity map and autoflush are a second unit-of-work implementation, and two of those fighting over one transaction is the worst thing you could do to this design.

Table definitions are generated with python -m ekbatan.schemagen, which starts a container, runs your migrations into it and reflects the result - the counterpart to Java's jOOQ step. Postgres via psycopg 3, MySQL and MariaDB via PyMySQL. Migrations are plain SQL files run by yoyo as a library, with a database lock, checks and a history on top, so one call migrates every shard - safely, from any number of processes at once.

What is here

models and entities frozen dataclasses; a change is evolve(..., emit=...)
the plan and the executor one transaction per shard, rows then events, retry on a lost race
the outbox one table, the JSON Java writes, and a seam to replace the whole store
repositories field-name mapping, optimistic locking, soft deletion, batches as one statement
sharding shard bits in the id, per-shard commits, cross-shard refused by default
keyed locks in-process, Postgres advisory, MySQL and MariaDB GET_LOCK; reentrant, watchdogged
the local event handler fan-out and dispatch, at-least-once, capped backoff, retention
distributed jobs one slot, one execution, across the cluster
schema and migrations the framework's tables as metadata, DDL per dialect, SQL migrations across every shard
statement accounting so the cost claims are checkable rather than asserted
test support ActionSpec / ActionAssert run a unit of work against a bare plan, with no database - Java's, method for method
typed builders python -m ekbatan.codegen - the one thing Python cannot infer
reading the stream ActionEvent from an outbox row, a Debezium message, or the transforms' Avro and Protobuf messages, with Java's own schemas

Only the publishing half of event-streaming is not here, and it is not planned: Debezium and the transforms run inside Kafka Connect, in a JVM, whichever language wrote the row.

DIVERGENCE.md records where this port deliberately differs from the Java one, and docs/SHORTCOMINGS.md records where it is simply behind - what is missing, what is slower, and what has not been decided yet.

Documentation

docs/ is the index. The short version:

  • Concepts - the dual-write trap, the outbox, actions, models and entities, sagas, sharding
  • Getting started - install, the extras, your first action
  • Startup and shutdown - create ekbatan's objects (the registries, the job registry, the lock providers) in your app's startup code and close them in its shutdown code: the FastAPI lifespan, or a worker's main()
  • Wiring and with FastAPI - the complete bootstrap
  • Database - tables, repositories, transactions, keyed locks, sharding, migrations, the four dialects, schemagen
  • Events out and Jobs - in-process handlers, Debezium, cluster-exclusive background work
  • Native asyncio - ekbatan.aio: async repositories, actions and executor, for an asyncio service
  • Concurrency - threads, the GIL, asyncio, and the one way to use this wrongly
  • Observability - spans, metrics, what to alert on
  • Releasing - how a release is cut

Typed builders

wallet.copy().balance(...) works for every model with nothing generated, and refuses an unknown field at the point of the call. What it cannot do is tell a type checker that .balance(...) takes a Decimal: the setters are resolved by name at runtime, so a checker sees only __getattr__.

That is the one thing worth generating, and the only thing this generates:

uv run python -m ekbatan.codegen myapp.model --out myapp/builders.py
wallet(existing).balance("oops").build()
# error: Argument 1 to "balance" of "WalletBuilder" has incompatible type "str";
#        expected "Decimal"

A generated builder declares only the fields its model added:

class WalletBuilder(ModelBuilder[Wallet, ShardedId[Wallet], WalletState]):
    builds = Wallet

    def owner_id(self, value: UUID) -> Self: ...
    def currency(self, value: str) -> Self: ...
    def balance(self, value: Decimal) -> Self: ...

id, state, version and the timestamps are typed on the base through those parameters - the same trick Model.Builder<ID, B, M, STATE> uses in Java, and the reason a generated builder there declares three methods rather than eight. An entity gets EntityBuilder instead of ModelBuilder, so it is not offered setters for timestamps and events it does not have.

Everything else Java's @AutoBuilder emits - constructing, copying, incrementing a version - already needs no generation here, so none is done. The output is ordinary Python: commit it, read it, regenerate it when a model changes. A stale one is a type error rather than a wrong answer, because build() still calls the real constructor.

ekbatan-py-examples/wallet-service-uv-sqlite uses it, including the three-line copy() override that makes a model's own methods typed too.

Layout

src/ekbatan/          the library
db-scheduler/         its own distribution: the port of kagkarlsson/db-scheduler
tests/
  fixtures/           the domain the suite is written against - owned by the suite
  unit/               no database, ever. 250 tests, about a second
  integration/        everything that touches one. 322 tests, on any dialect
ekbatan-py-examples/                 each one built three ways: -uv-, -pip-, -poetry-
  fastapi-wallet-rest-uv-pg/          the REST service, on PostgreSQL
  fastapi-wallet-rest-uv-mysql/       the same service, on MySQL
  fastapi-wallet-rest-uv-mariadb/     the same service, on MariaDB
  fastapi-wallet-rest-uv-sharded-pg/  the same service, over two databases
  fastapi-wallet-rest-uv-async-sharded-pg/  and on native asyncio
  fastapi-wallet-rest-uv-async-pg/    the same service on native asyncio, ekbatan.aio
  fastapi-wallet-rest-uv-async-mysql/    and on MySQL
  fastapi-wallet-rest-uv-async-mariadb/  and on MariaDB
  fastapi-wallet-saga-uv-pg/          a saga: two services, choreography, compensation
  fastapi-wallet-saga-uv-async-pg/    the same saga, its steps awaited, on native asyncio
  job-worker-uv-pg/                   a distributed job, no HTTP at all
  job-worker-uv-async-pg/             the same jobs, their work awaited on a worker loop
  wallet-service-uv-sqlite/           the smallest one. No container, no web stack
  wallet-service-uv-async-sqlite/     the same, on native asyncio

One REST example per dialect and one of everything else, which is the shape of Java's matrix - there, too, only the REST project is replicated per database. Every one of them also has an async twin on ekbatan.aio, with the same tests. And each is carried once per build tool, as Java's are per Gradle and Maven: fastapi-wallet-rest-uv-pg, fastapi-wallet-rest-pip-pg, fastapi-wallet-rest-poetry-pg, the same code with its own pyproject.toml and Makefile - 42 projects, kept in step by scripts/sync-examples.py. Each is standalone: cd into one and make builds it, tests it as its installed wheel, and needs nothing else from this repository but the framework.

Three separate things with three separate jobs, and they do not import each other. The suite's domain exists to exercise every shape worth exercising, including a few no application would want; the example exists to show what good code looks like. Coupled, every change made for coverage lands in the documentation and every change made for clarity breaks the suite. tests/test_structure.py enforces the separation, including that nothing in unit/ asks for a database.

Testing

An action's business logic needs no database. ActionSpec runs one against a bare plan - Java's ActionSpec / ActionAssert, method for method:

(ActionSpec.of(WalletDepositMoneyAction(wallet_repository))
    .execute(WalletDepositMoneyAction.Params(amount=Decimal("50.00"), ...))
    .assert_updated(Wallet, lambda w: w.balance == Decimal("150.00"))
    .assert_emitted(WalletMoneyDepositedEvent, 1)
    .assert_no_additions())

Or, as plain data, with run_action:

staged = run_action(None, WalletDepositMoneyAction(wallet_repository), WalletDepositMoneyAction.Params(amount=Decimal("50.00"), ...))

assert staged.result.balance == Decimal("150.00")
assert staged.emitted(WalletMoneyDepositedEvent)[0].amount == Decimal("50.00")
assert staged.updated(Wallet) == [staged.result]

It binds the plan through the same mechanism the executor uses, so self.plan, self.clock and the principal argument behave exactly as they do in production. Nothing is written. Knowing that state and events land together is a test against a database, and no test-support machinery substitutes for it.

uv run pytest tests/unit              # no database at all, about a second
uv run pytest                         # the whole suite, on SQLite
uv run pytest --dialects=postgres     # in a container
uv run pytest --dialects=all          # every test, on all four
make examples                         # all 42 examples, each as its installed wheel
make -C ekbatan-py-examples/wallet-service-uv-sqlite run
make -C ekbatan-py-examples/wallet-service-uv-async-sqlite run

uv run mypy                           # strict, on the library and the examples
uv run ruff check .

578 tests. 264 of them are parametrised over the dialect, so --dialects=all runs 1,370.

There are no per-dialect test classes. Java needs one subclass per dialect per suite - PgRepositoryTest, MysqlRepositoryTest, MariadbRepositoryTest - because that is how JUnit parametrises a class. Here one fixture is parametrised, so a test cannot be accidentally left out of a dialect.

SQLite is the default and is not a supported production dialect. It is there because a suite needing a daemon to run at all is a suite people stop running; the claim that this framework works is a claim about Postgres, MySQL and MariaDB, and CI runs all three on every push - plus Postgres again under a non-UTC process, which is where the timestamp defects hide.

Behaviour is pinned by ekbatan-spec, which is language-independent and shared with every port.

Metadata

Release files for ekbatan 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ekbatan 0.1.2
File Size Uploaded Provenance Actions
ekbatan-0.1.2.tar.gz 6.5 MB Without provenance Details for ekbatan-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ekbatan 0.1.2
File Interpreter ABI Platform Actions
ekbatan-0.1.2-py3-none-any.whl Python 3 none any Details for ekbatan-0.1.2-py3-none-any.whl

Total release size: 6.8 MB

Release files / ekbatan-0.1.2.tar.gz

Download URL ekbatan-0.1.2.tar.gz
Size 6.5 MB
Tags Source
SHA-256 checksum
How to use checksums
e25a09a145547695e56bc16fefa37a28ab6b70c5a71d4aa624c6c7393ae0c27e
BLAKE2b-256 checksum
How to use checksums
e40077c65b64c17b4b99968ee8b27330936c0609839c56e8c829b8e1abf85f66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / ekbatan-0.1.2-py3-none-any.whl

Download URL ekbatan-0.1.2-py3-none-any.whl
Size 356.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
732297b13b93aa826220ab417eaf7a32d22e8e9270f3760cbe575d2d704bf094
BLAKE2b-256 checksum
How to use checksums
84786ba16c8f697aacfcbd89e92a788d15d34bd7b8fe8fd32193a6cdb60e9816
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for ekbatan 0.1.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page