Capstone: Replayable Event-Driven Payments
Track
Architecture-only
Brief
A payments company is splitting authorization, ledger posting, and balance inquiries into independently deployable services after repeated release contention. The broker guarantees at-least-once delivery, so a consumer may see the same event several times or receive retries after a timeout. Finance requires an append-only history that can rebuild balance views for audits without rerunning charges. Design the service and data flows so commands, events, and read projections have clear ownership and failure behavior.
Constraints
- The existing platform provides Kubernetes, PostgreSQL, and a durable Kafka-compatible broker; no distributed transaction coordinator is available.
- Authorization requests need an initial response within 400 ms at p95, while balance projections may lag by up to five seconds.
- Every consumer must tolerate duplicate delivery and must not claim that the broker provides exactly-once processing.
- Ledger entries are immutable, and every audit replay must produce a trace tied to the original payment ID and event sequence.
- Card data stays in a PCI-scoped token vault; events carry tokens or references, never card numbers.
- Three teams can operate separate deployment pipelines, dashboards, and on-call runbooks.
Required Patterns
- Microservices — WHY: authorization, ledger, and inquiry have separate scaling and release needs, and each service can exclusively own its database and runtime.
- Event-Driven Architecture — WHY: durable payment events decouple the initial authorization response from ledger and projection work while making retries and asynchronous recovery explicit.
- CQRS + Event Sourcing — WHY: the write side can append authoritative payment events while optimized balance projections serve reads and can be rebuilt from ordered history.
Solution Sketch
Illustrative pseudocode and configuration — not runnable.
service authorization: owns auth_db; deploys independently
service ledger: owns ledger_db; consumes payment events
service inquiry: owns inquiry_db; serves read models only
Authorization.authorize(command):
require valid vaultToken and command.idempotencyKey
transaction auth_db:
decision = authorizeLocallyOrThroughVaultAdapter(command)
aggregate.append(PaymentAuthorized or PaymentDeclined,
paymentId, sequence, decision, commandMetadata)
outbox.insert(unpublishedAggregateEvents) # same local transaction
return decision + paymentId # does not wait for projections
OutboxRelay:
publish event with key = paymentId and stable eventId
mark published only after broker acknowledgement; retry safely
LedgerConsumer.on(event):
transaction ledger_db:
return if inbox.contains(consumer = "ledger", eventId)
inbox.insert(unique(consumer, eventId))
appendImmutableEntry(paymentId, event.sequence, amount, eventId)
acknowledge only after commit
InquiryProjection.on(event):
transaction inquiry_db:
return if inbox.contains(consumer = "balance-v3", eventId)
require next sequence for paymentId or park gap for retry
applyBalance(event); inbox.insert(eventId); recordProjectionLag()
AuditReplay(paymentId, targetProjection = "balance-audit-v1"):
read immutable events in sequence order
build a new projection; issue no authorization, refund, or payout commands
compare result with ledger and retain replay ID, source range, and timestamp
Rubric
- Service ownership: Passes if authorization, ledger, and inquiry each own their database and communicate only through APIs or versioned events, with no shared tables.
- Atomic publication: Passes if aggregate history and outbox records commit in one authorization transaction and relay retries cannot lose an accepted decision.
- Idempotent consumption: Passes if each consumer uses a durable unique
(consumer, eventId)inbox record in the same transaction as its side effects. - Ordering and recovery: Passes if events are partitioned by payment ID, sequence gaps are detected, and failed events have bounded retries plus a visible dead-letter or parking path.
- CQRS separation: Passes if inquiries read only projections, commands use authoritative write state, and API responses expose projection lag or stale status.
- Replay safety: Passes if replay reads the immutable sequence into a new projection and cannot resend financial commands or mutate original ledger entries.
- Compliance and operations: Passes if event schemas exclude card data and each service has its own pipeline, lag metrics, and runbook.