c_agri_trade/04_generated_documentation/04_architecture_design/hld.md

5.1 KiB
Raw Blame History

id version status last_cr
HLD-ATB 2.0.0 Reviewed — Stage 7 design baseline; runtime validation pending CR-001

High-Level Architecture Design — Agriculture Trading Backoffice

Field Value
System Agriculture Trading Backoffice (ATB)
Pattern FastAPI modular monolith, tenant isolation through PostgreSQL RLS
Technology authority tech-profile.yaml
Architecture status Stage 7 design baseline approved; runtime and production operations proof remain pending
Stage 7 focus UC-001, UC-005, UC-006, UC-007, UC-011, UC-014, UC-017, UC-019, UC-020

1. Logical Architecture

graph TD
    Browser[React + TypeScript SPA] -->|OIDC PKCE / HTTPS| IdP[Keycloak]
    Browser -->|JWT bearer / HTTPS| Edge[Ingress / TLS]
    Edge --> API[FastAPI modular monolith]
    API --> IAM[Identity validation + role policy]
    API --> Tenant[Membership/context resolver]
    API --> Domain[Receiving / Quality / Inventory / Finance]
    API --> PG[(PostgreSQL + RLS + audit + outbox)]
    Worker[Background worker] --> PG
    Worker --> Stream[Valkey Streams / cache]
    Worker --> Notify[Messaging adapter]
    API --> ObjectStore[Private document storage]
    Ops[Telemetry / alerting] <-- API
    Ops <-- Worker

The API and worker are separately scaled processes from one modular-monolith codebase. PostgreSQL is the source of truth for business state, audit records, and pending events. Valkey is an acceleration/delivery component only; loss of Valkey must not lose committed business transactions. Object storage is a candidate dependency for POD and other uploaded documents; provider and retention controls require review because the technology profile does not select a storage provider.

2. Module Boundaries

Module Owning use cases in Stage 7 scope Owns Must not own
Identity & authorization UC-001 OIDC session establishment, authorization decision, logout contract Membership lifecycle or trusted tenant selection
Tenant control plane UC-018, UC-019 Tenant lifecycle, memberships, verified active context Tenant-owned business records
Tenant isolation & audit UC-017, UC-020 RLS enforcement, cross-resource isolation, governed mutation history, support access Ordinary feature authorization policy
Receiving UC-005 Gate/weighment records, GRN posting, inward lot identity QC release decision or stock read model
Quality UC-006 Inspection evidence, QC state transitions, lot lineage Client-side-only state enforcement
Inventory UC-007 Append-only stock movements, position projection, reconciliation Direct mutation of posted ledger history
Finance UC-011 (with UC-004/008/009/012 at Stage 9) Receivables, credit lock, receipt and reversal External payment execution; none is specified in the BRD
Operational events UC-014 (with UC-003/015 at Stage 9) Reliable event delivery, alert evaluation, reporting projections Source-of-truth business state

3. Trust and Transaction Boundaries

  1. Keycloak authenticates the user through configured OIDC/PKCE. The API validates the configured JWT bearer token; algorithm, lifetime and logout revocation semantics remain explicit security-design decisions.
  2. The application resolves the active tenant through server-side membership and role checks (UC-019). A client-supplied tenant identifier or token claim alone is never trusted as authorization.
  3. Each tenant database unit of work begins a transaction, sets transaction-local tenant context from the verified resolver, then performs queries under PostgreSQL RLS. Missing context fails closed. No request-scoped context may leak through a pooled connection.
  4. A GRN, lot, stock movements, audit record and outbox event commit atomically. A QC Hold lot cannot be allocated or dispatched, even if a client bypasses the UI.
  5. Financial correction uses compensating ledger entries and immutable history; it does not rewrite a posted receipt. Event consumers are idempotent.

These are candidate architecture patterns for lead review, not newly approved product requirements.

4. Operational Constraints to Prove

  • 50 tenant firms, up to 25 named users per tenant, and 250 concurrent active users; run the accepted 5× peak-hour load scenario (BRD-NFR-012 / AMB-008).
  • Tenant reports across at least 50,000 ledger rows meet the 1-second report target (BRD-NFR-004).
  • Core financial conversions preserve 3-decimal weight and 2-decimal currency precision (BRD-NFR-001).
  • Tenant isolation fails closed for API, DB, exports, files, cache, jobs, and support paths (BRD-NFR-006/010).
  • Availability/recovery evidence must meet BRD-NFR-011. No provider or instance size is selected in this spike.

5. Design Status

This HLD is a Stage 7 candidate. The design walkthrough is recorded in architecture_spike_report.md; runtime integration, RLS, failure-injection, and load validation have not run because the repository has no product application implementation or deployment environment yet. Do not mark it Validated or Baseline until the human lead accepts the decisions and required evidence is produced.