Karyo
Documentation/ Decision records

ADR 0004: One PostgreSQL database and one `karyo` schema serve every module

Karyo 2.1.1rendered at build time
On this page

Status: Accepted

Context

Warehouse operations need ACID transactions. A reservation, a transfer or an adjustment that half-happens leaves double picks or stock that exists only on paper, and several of those operations touch more than one module at once.

The domain is relational, and its hottest queries are join-heavy and ordered: stock selection joins stock units, unit loads, locations and goods owners and orders them first-in-first-out; location finding filters and ranks locations by capacity and strategy. Some columns hold JSON, such as the outbox payload. The inventory journal is an append-only record that grows with every stock change.

The backend is one process (ADR 0001) with one Hibernate persistence unit.

Decision

  • PostgreSQL is Karyo's only database. The Compose stack runs PostgreSQL 16 (infrastructure/docker/docker-compose.prod.yml, services.postgresql.image).
  • There is one datasource and one persistence unit, whose default schema is karyo (services/karyo-app/src/main/resources/application.yaml, quarkus.datasource, quarkus.hibernate-orm). Flyway creates and owns that schema (ADR 0005).
  • Every module's tables live in karyo. Modules are separated by table ownership and by id-only references (ADR 0007), not by schema or database.
  • The schema uses standard PostgreSQL features directly - JSONB where a column holds JSON, partial indexes (V1__create_outbox.sql, outbox_events) - and installs no extension.
  • The inventory journal is declared range-partitioned on created, with one default partition and no other (V104__create_inventory_journals.sql, inventory_journals, inventory_journals_default).

Consequences

  • One backup and one restore cover the whole installation, and a change that spans modules is one ACID transaction.
  • Reporting can join other modules' tables directly instead of maintaining copies (ADR 0011).
  • Coupling through the schema is invisible to the build. Code that reads another module's tables with native SQL - reporting's KPI views, and the forecasting, slotting and simulation engines - fails at runtime, not at compile time, when those tables change.
  • Separation between goods owners is enforced in application code, not in the database: the schema carries no row-level security policies (ADR 0014).
  • One connection pool of at most twenty connections serves every module and every scheduler (application.yaml, quarkus.hibernate-orm.database.default-schema).
  • The tables of the engine modules exist in every installation, created by migrations everyone runs, empty until a capability acts on them (ADR 0005).
  • The journal is partitioned so that it can be split by month, with recent months cheap to query and old months dropped rather than deleted. No monthly partitions exist and nothing creates them, so every journal row lands in inventory_journals_default. Adding a range partition later means first moving the rows the default partition already holds for that range, because PostgreSQL will not attach a partition while the default partition holds rows that belong to it.
  • Karyo can move only to another PostgreSQL: native SQL, JSONB and partial indexes are used directly.

Alternatives considered

  • A database or schema per module. Rejected. There would be no joins across modules; cross-module operations would become distributed transactions needing sagas; reference data would be copied and kept in step; and there would be more databases to run, back up and restore. Its benefits - schema evolution and scaling per module - have no use for one application released as a whole. The discipline it demanded, no foreign keys between modules, is kept (ADR 0007).
  • MySQL. Rejected: weaker window functions and common table expressions for the selection and location-finding queries, and a JSON type less capable than JSONB.
  • MongoDB. Rejected: the domain is relational - stock selection joins stock units, unit loads, locations and owners - its multi-document transactions are limited, and Flyway does not support it.
  • CockroachDB. Rejected: distributed SQL that a single installation does not need, with a minimum of around 2 GB of memory per node.

Evidence

  • infrastructure/docker/docker-compose.prod.yml, services.postgresql.image - the database container
  • services/karyo-app/src/main/resources/application.yaml, quarkus.datasource, quarkus.hibernate-orm, quarkus.flyway - one datasource, one persistence unit, one schema, Flyway owning it
  • services/karyo-app/src/main/resources/db/migration/common/V1__create_outbox.sql, outbox_events - JSONB and a partial index
  • services/karyo-app/src/main/resources/db/migration/inventory/V104__create_inventory_journals.sql, inventory_journals, inventory_journals_default - the partitioned journal and its only partition
  • Data and persistence
  • ADR 0001 - one process, one datasource
  • ADR 0005 - how the schema is created and changed
  • ADR 0007 - how modules share a schema without sharing keys
  • ADR 0010 - the journal the partitioning is for
  • ADR 0011 - reads across module tables
  • ADR 0014 - where owner separation is enforced instead