Phase 2 section 7 (the other half, alongside sdk/go/ration and sdk/rust/ration-client already pushed in c5297e4/8215fb8): a hand-rolled internal/httpapi on services/gateway translating JSON <-> the gRPC source of truth (cards, deposits, bridge funding, activity), documented as a convenience facade over gRPC, not a parallel implementation. Also folds in the remaining phase-2 doc updates (ARCHITECTURE/RUNBOOK/THREAT-MODEL/ AIRWALLEX-SETUP/SUBMISSION, ADR 0003/0004) and docker-compose/Makefile/ .env.example wiring from the resumed phase-2 workflow's final agents. Verified: go build/vet/test clean across all 10 services + both new SDKs, `make build && make test` clean end to end (C WAL, Rust ledger-core + the new Rust SDK, every Go module including sdk/go/ration). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
8.5 KiB
ADR 0004: Service decomposition boundaries (gateway splits into 4)
Status
Accepted and executed (docs/SCALE-EXPANSION.md section 1):
services/auth, services/tenant-registry, services/card-issuance
and services/webhook-ingestion exist as their own Go modules, each its
own gRPC server, and services/gateway is now the thin BFF described in
the Decision section below — internal/card, internal/bridge,
internal/tenant, internal/airwallex and internal/httpserver no
longer exist under services/gateway; their logic moved verbatim into
the four new services (see each new service's own doc comments for the
mechanical-extraction details, and services/card-issuance/internal/bridge/service_test.go,
carried over unchanged from the old services/gateway/internal/bridge/service_test.go,
for proof the business logic itself didn't change). gateway.proto is
byte-for-byte unchanged, so services/agent, services/tui and
services/cli needed no changes. This ADR's original boundary rationale
below is kept as-written (it described the boundaries correctly; only
this Status section was forward-looking at the time it was accepted).
Context
services/gateway today is one deployable Go binary
(cmd/gateway/main.go) whose internal/ already has real seams, because
its packages were written with single responsibilities from the start:
internal/tenant— tenant CRUD, "delegates persistence to ledger-core" (ARCHITECTURE.md).internal/card+internal/bridge— card issuance and the rationing/bridge-funding math (bridge.Service.RationBridgeFunding, the highest-value business logic in the whole system perservices/gateway/internal/bridge/service.go's own doc comment).internal/airwallex(webhook signature verification) +internal/httpserver(the/webhooks/airwallexHTTP listener) — receiving and verifying Airwallex webhook deliveries, turning them intoMarkDepositReceivedcalls.internal/grpcserver— theGatewaygRPC service implementation (gateway.proto) that the TUI and CLI actually dial.internal/activity— the fan-out event bus feedingStreamActivity.internal/ledgerclient,internal/config,internal/idgen,internal/ratelimit— cross-cutting support used by more than one of the above.
The problem one binary creates at scale: card issuance (expected to be the system's highest-traffic, horizontally-scaled path — "give it its own horizontal scaling story," section 1) is deployed, restarted, and rate-limited as one unit with tenant CRUD (low traffic, rarely changes) and webhook ingestion (bursty, externally triggered, needs to be safe to retry independently of everything else).
Decision
Split along the package boundaries that already exist, one new Go
module (its own go.mod, its own gRPC server) per bullet, with the
internal proto surfaces this split requires added to proto/ alongside
the existing common.proto, ledger.proto, gateway.proto,
audit.proto:
services/auth(newproto/auth.proto) — tenant API-key issuance/verification, JWT minting for the TUI/CLI, and mTLS client cert issuance for service-to-service calls. This is new code, not an extraction — today nothing inservices/gatewayissues per-service mTLS certs;infra/dev-ca/andpkg/mtls(section 8) generate and wire TLS credentials, but a dedicated issuance service is what lets a compromised downstream service's cert be revoked without redeploying every other service.services/tenant-registry(newproto/tenant_registry.proto) — a direct extraction ofinternal/tenant/service.go. Deliberately a "thin service, but a real separate deployable/scalable unit, not a package" (section 1): it owns nothing ledger-core doesn't already own — every write still proxies toledger-core'sLedgerCore.CreateTenant/GetTenant/ListTenantsRPCs, exactly asinternal/tenantdoes today. It is split out anyway so tenant CRUD's (low, bursty-on-onboarding) traffic and deploy cadence never forces a redeploy of card-issuance.services/card-issuance(newproto/card_issuance.proto) — a direct extraction ofinternal/card+internal/bridge: Airwallex issuing calls and the rationing math. Named "the highest-value service" in the spec becausebridge.Service.RationBridgeFundingis where real money decisions happen; its extraction target is explicitly "stateless, idempotent, safe to run N replicas behind a queue" — already true of the current code (RationBridgeFunding's own doc comment: "correctness for a single tenant under concurrent calls ultimately rests on ledger-core's idempotent, reserve-floor-checked ApplyEntry ..., not on any lock in this package"), so this split changes deployment topology, not business logic.services/webhook-ingestion(newproto/webhook_ingestion.proto) — a direct extraction ofinternal/airwallex/webhook.go(signature verification) +internal/httpserver/webhooks.go(the HTTP listener), publishing normalized events onto a simple internal event bus (the Go channel businternal/activity/stream.goalready implements) rather than callingMarkDepositReceivedinline — isolating webhook delivery spikes from the services actually indexing them. The spec is explicit that this internal bus is a placeholder: "document where a real deployment would swap in SQS/Kafka" (noted here; the swap itself is future work, not done).services/gatewaybecomes a thin BFF/aggregator implementinggateway.protoby fanning out gRPC calls to the four services above — this is what the TUI and CLI keep talking to, with the external surface (and thereforeinternal/ratelimit, per section 8's "in front of every public-facing RPC") staying here, not duplicated into each internal service.internal/grpcserver/server.gois the part of today's codebase that survives essentially unchanged in shape, just rewired to call four gRPC clients instead of four in-process packages.
internal/ledgerclient and internal/idgen are shared support, not
owned by one service: each extracted service gets its own copy of the
thin ledgerclient wrapper (matching the existing pattern where
services/agent already has its own independent
internal/ledgerclient, not a shared import of gateway's) and idgen,
since the spec calls for separate go.mods per service and this project
does not yet have a shared internal Go module for cross-service
utilities.
Alternatives considered
- Split along a different axis (e.g. by data entity — "cards
service," "deposits service" — rather than by capability). Rejected:
bridge.Servicealready spans cards, deposits, and ledger entries in a single rationing decision; splitting by entity would force that one decision to make three cross-service calls instead of one, adding latency and partial-failure surface to the highest-value code path for no benefit. - Extract card-issuance only, leave the rest in gateway. Rejected: it solves card-issuance's scaling story but still couples webhook-ingestion's bursty, externally-triggered traffic to tenant-registry's deploy cadence, and auth (new capability) would have nowhere principled to live.
- Keep one monolith, scale it horizontally as-is. Rejected per the spec's framing — "true service decomposition" (not just more replicas of the same binary) is explicitly what distinguishes this phase from "hackathon scaffolding," and a single binary cannot give card-issuance an independent scaling policy from tenant-registry's.
Consequences
- Sections 1 and 7 (SDKs/REST facade, ADR 0003) touch the same gRPC
surface and are explicitly called out in
SCALE-EXPANSION.md's execution notes as not safe to run fully in parallel with each other — the facade'sinternal/httpapimust be written against whatevergateway.protolooks like after the BFF rewiring, not against the pre-split monolith's implicit internal calls. - Every new internal RPC this split introduces (
auth.proto,tenant_registry.proto,card_issuance.proto,webhook_ingestion.proto) is internal-only;gateway.protoremains the one public aggregate surface external callers (TUI, CLI, the future REST facade) ever see — unchanged externally, per the spec. RUNBOOK.md's diagnosis paths for card/bridge/webhook issues now point atservices/card-issuance/internal/<package>andservices/webhook-ingestion/internal/<package>respectively, not atservices/gateway/internal/<package>— updated alongside this split.