Karyo
Documentation/ Decision records

ADR 0011: Reporting reads the operational database directly

Karyo 2.1.1rendered at build time
On this page

Status: Accepted

Context

Dashboards and KPIs combine data that several modules own: picks from fulfillment, receipts and orders from orders, stock from inventory, locations from layout. A report is only useful if it agrees with what the operational screens show.

All of that data lives in one PostgreSQL database and one karyo schema (ADR 0004), so every table is reachable from one connection and nothing prevents a join. There is no message broker to feed a separate read store (ADR 0008).

Decision

  • Reporting reads operational tables directly. Current utilization and occupancy use ordinary SQL views. Daily accuracy, throughput and cycle time use read-only SQL functions from V1009. Their arguments name one Facility and its resolved time zone. Native queries consume these projections. Reporting keeps no separate read model and consumes no events to maintain one.
  • Native SQL names projections schema-qualified (karyo.kpi_accuracy_daily and so on), because Hibernate's default schema does not rewrite native queries.
  • Day buckets are Facility-local. Karyo resolves karyo.facility.time-zone through the Facility, Business Unit, Company and environment/default ladder. UTC is the default. Each request uses that zone for SQL day buckets, query bounds and year-to-date. Neither JVM nor connection timezone decides a Facility's calendar. A view cannot accept the clock as an argument, so V1009 replaces the daily views with parameterized SQL functions without storing any new operational data.
  • The reporting module also stores saved report definitions, so as a module it is not read-only; its KPI and occupancy paths are.
  • The same approach is open to any module that needs a cross-domain read. The analytics engines read the shared stock and pick tables the same way.

Consequences

  • A report is consistent with operational data the moment a transaction commits. There is no lag, no consumer to keep running and no initial-population step when a report is added.
  • A new KPI is a read-only SQL projection in a forward migration plus a scoped query.
  • The coupling is invisible to the build. A view or native query names another module's tables, columns and codes directly, with no compile-time link to the owning module, so a rename or a changed code breaks it at run time rather than at build time.
  • Report queries share the database with operational writes. A heavy report and a pick confirmation compete for the same connections and I/O.
  • Applied migrations are immutable, comments included (ADR 0005). Changing a projection is a new forward migration. Current calculation rules also live here and in the reporting module's README.
  • Throughput's units_shipped counts shipments, not quantities; the module README states the interpretation limits of each view.

Alternatives considered

  • A separate read store fed by events (CQRS). Rejected. The reason such a store is needed is that separate databases cannot be joined; with one schema that constraint does not exist, and there is no broker to feed it. It would add eventual consistency, a consumer per read model and an initial-population problem for no gain. Whether a separate read store would pay for itself at a larger scale has not been assessed.
  • Aggregating through the modules' services or REST endpoints at report time. Rejected. A report over thousands of rows would issue a call per row and per module where one SQL join answers it, and it would load the operational paths while doing so.
  • Materialised views refreshed on a schedule. Not used. Why ordinary views were preferred is not recorded.

Evidence

  • services/karyo-app/src/main/resources/db/migration/reporting/V1001__create_kpi_views.sql, kpi_accuracy_daily, kpi_throughput_daily, kpi_cycle_time_daily, kpi_utilization_current - the four KPI views
  • services/karyo-app/src/main/resources/db/migration/reporting/V1009__facility_daily_clocks.sql - daily projections with explicit Facility and clock arguments
  • services/reporting-service/karyo-reporting-core/src/main/kotlin/com/karyo/reporting/repository/KpiViewRepository.kt - schema-qualified projections and same-zone date bounds
  • karyo-reporting-core/README.md - routes, the migration chain and each view's interpretation limits
  • ADR 0004 - the single schema that makes direct reads possible
  • ADR 0005 - why a view change is a forward migration
  • ADR 0006 - the module rule that native cross-module reads step around
  • ADR 0010 - current state as the thing reports read
  • Data and persistence - the read side and the day-bucketing rule
  • Modules and boundaries - the shared schema as a coupling the module graph does not show