Resilient, high-throughput event-driven e-commerce microservices platform demonstrating zero-data-loss checkout persistence via the Transactional Outbox pattern and idempotent Kafka stream processing.
StreamMart is an event-driven e-commerce microservices system built with Node.js, Apache Kafka (KRaft), PostgreSQL 16, Drizzle ORM, and Docker Compose. It demonstrates reliable asynchronous event processing using the Transactional Outbox pattern (SELECT ... FOR UPDATE SKIP LOCKED), idempotent batch consumers (eachBatch with processed_events), per-message poison-pill fallback to a Dead Letter Queue (DLQ), deterministic partition keys, and distributed event lineage (eventId, correlationId, causationId).
flowchart TD
Client["Client / Load Generator"] -->|"HTTP + JWT (Port 8085)"| Gateway["API Gateway"]
Gateway -->|"HTTP Keep-Alive POST /payment"| Payment["Payment Service"]
subgraph Storage["PostgreSQL 16"]
PaymentsTbl[("payments")]
OrdersTbl[("orders")]
OutboxTbl[("outbox_events\n(topic-scoped partial indexes)")]
ProcessedTbl[("processed_events\n(event_id, service_name PK)")]
end
subgraph Broker["Apache Kafka 3.7 (KRaft)"]
T1["Topic: payment-successful\nkey = userId"]
T2["Topic: order-successful\nkey = orderId"]
T3["Topic: email-successful\nkey = orderId"]
DLQ["Topic: payment-successful.DLQ\nkey = eventId"]
end
Payment -->|"1. Atomic Tx: payment + outbox"| PaymentsTbl
Payment -->|"1. Atomic Tx"| OutboxTbl
OutboxTbl -.->|"2. Payment Outbox Relay\nSKIP LOCKED"| T1
T1 -->|"eachBatch consumer"| Order["Order Service"]
Order -->|"Atomic Batch Tx: orders +\nprocessed_events + outbox"| OrdersTbl
Order --> ProcessedTbl
Order --> OutboxTbl
Order -->|"Fallback 1-by-1 after 3 retries\n(poison message only)"| DLQ
OutboxTbl -.->|"Order Outbox Relay\nSKIP LOCKED"| T2
T2 -->|"eachBatch consumer"| Email["Email Service"]
Email -->|"Dispatch receipt +\nAtomic Batch Tx"| ProcessedTbl
Email --> OutboxTbl
OutboxTbl -.->|"Email Outbox Relay\nSKIP LOCKED"| T3
T1 -->|"eachBatch consumer"| Analytic["Analytic Service"]
T2 -->|"eachBatch consumer"| Analytic
T3 -->|"eachBatch consumer"| Analytic
Analytic -->|"Bulk ON CONFLICT DO NOTHING"| ProcessedTbl
- Transactional Outbox (
outbox_events):payment-service,order-service, andemail-servicewrite business state and outgoing events in a single PostgreSQL transaction. Background relay workers claim rows withSELECT ... FOR UPDATE SKIP LOCKED, publish to Kafka, and markpublished = true, while topic-scoped pruners clean up published rows older thanOUTBOX_RETENTION_HOURS. - Idempotent Consumers (
processed_events):order-service,email-service, andanalytic-servicededuplicate incoming events on(event_id, service_name). Redeliveries after a crash before Kafka offset commit skip already-processed events cleanly (zero duplicate orders). - Poison-Message Fallback in
eachBatch: If a batch transaction inorder-servicefails, the transaction rolls back cleanly andorder-servicefalls back to processing that batch one message at a time in offset order. Valid messages still commit; after 3 retries, only the failing message is sent topayment-successful.DLQand the consumer continues without getting stuck. - Split Health Checks (
payment-service):GET /healthreports process liveness (200 even during a Kafka outage so checkouts keep succeeding into PostgreSQL), whileGET /relay-statusreports Kafka relay and outbox pruner health (503 when disconnected from Kafka). - Strict Secret Hygiene & Zero-Fallback Gateway:
api-gateway/config.jsenforces thatJWT_SECRETis defined and at least 32 characters long at startup; missing or short secrets fail immediately with a clear error. Both token issuance (index.js) and token verification (auth.middleware.js) share this single config module without hardcoded fallback strings.
- Centralized Validation (
api-gateway/config.js): Fails fast on startup ifJWT_SECRETis missing or shorter than 32 characters. - No Hardcoded Fallback Secrets:
docker-compose.ymlinjectsJWT_SECRET=${JWT_SECRET}without fallbacks so unconfigured container startups fail visibly instead of running on insecure defaults. - Automatic Local Secret Generation:
node scripts/start-stack.jsdetects ifJWT_SECRETis missing from the local gitignored.env, auto-generates a secure 32-byte hex secret (crypto.randomBytes(32).toString('hex')), writes it to.env, and logs notice. - Ephemeral CI Secrets: GitHub Actions workflow dynamically generates a unique masked 32-byte secret per run (
openssl rand -hex 32) for both unit tests and Docker Compose resilience tests. - Gated Demo Auth Endpoints:
POST /api/auth/tokenandPOST /api/auth/tokens/batchare disabled (returning HTTP 404) unlessDEMO_AUTH=trueandNODE_ENV !== "production".
All numbers below come from clean, reset-database, full-drain runs with raw outputs stored in benchmarks/raw/ and detailed methodology in PHASE1-BENCHMARKS.md.
Host for every benchmark row below: Single local laptop — AMD Ryzen 7 6800H (8 physical cores / 16 threads @ 3.20 GHz), 16 GB RAM, Windows 11 Home (64-bit), Docker Desktop (WSL2). All 7 Docker containers and the k6 load generator ran on the same machine.
| Configuration | Workload | Host | Accepted req/s (mean) | True Completed checkouts/s (mean) | Order Lag @ 30s (mean) | Catch-up to Lag 0 (mean) | p50 / p95 Latency (ms) |
|---|---|---|---|---|---|---|---|
baseline_consumer (1 part, order=eachMessage) |
20 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 199.06 | 71.37 | 3,984 msgs | 57.57s | 41.49 / 104.31 |
baseline_consumer (1 part, order=eachMessage) |
50 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 227.17 | 56.23 | 5,645 msgs | 96.11s | 135.36 / 350.41 |
baseline_consumer (1 part, order=eachMessage) |
100 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 285.61 | 48.42 | 7,420 msgs | 154.33s | 245.14 / 496.65 |
fix_a (6 parts, order=eachMessage, concurrency=6) |
20 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 220.04 | 152.24 | 2,142 msgs | 14.06s | 33.44 / 75.09 |
fix_a (6 parts, order=eachMessage, concurrency=6) |
50 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 273.48 | 90.74 | 5,858 msgs | 63.70s | 111.33 / 218.66 |
fix_a (6 parts, order=eachMessage, concurrency=6) |
100 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 340.17 | 87.88 | 7,912 msgs | 97.92s | 205.64 / 399.67 |
fix_b (1 part, order=eachBatch) |
20 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 253.12 | 251.17 | 0 msgs | 0.24s | 21.77 / 55.91 |
fix_b (1 part, order=eachBatch) |
50 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 372.13 | 368.92 | 0 msgs | 0.27s | 74.00 / 140.81 |
fix_b (1 part, order=eachBatch) |
100 VUs, 30s k6, 1,000 JWTs, 3 runs, full order drain | Ryzen 7 6800H / 16 GB / WSL2 | 379.49 | 375.75 | 0 msgs | 0.31s | 178.47 / 374.79 |
| Configuration | Workload | Host | Accepted req/s (mean) | Order Lag @ 30s / Catch-up | Email Lag @ 30s / Catch-up | Analytic Lag @ 30s / Catch-up (3x events) |
|---|---|---|---|---|---|---|
Before (order=eachBatch, email=eachMessage, analytic=eachMessage) |
50 VUs, 30s k6, 1,000 JWTs, 3 runs, full 3-service drain | Ryzen 7 6800H / 16 GB / WSL2 | 304.48 | 0 msgs / 0.00s | 6,242 msgs / 48.68s | 23,661 msgs / 112.34s |
Before (order=eachBatch, email=eachMessage, analytic=eachMessage) |
100 VUs, 30s k6, 1,000 JWTs, 3 runs, full 3-service drain | Ryzen 7 6800H / 16 GB / WSL2 | 274.86 | 0 msgs / 0.00s | 5,050 msgs / 44.09s | 20,480 msgs / 97.15s |
Final (order=eachBatch, email=eachBatch, analytic=eachBatch, batched relays) |
50 VUs, 30s k6, 1,000 JWTs, 3 runs, full 3-service drain | Ryzen 7 6800H / 16 GB / WSL2 | 264.53 | 0 msgs / 0.00s | 0 msgs / 0.00s | 0 msgs / 0.00s |
Final (order=eachBatch, email=eachBatch, analytic=eachBatch, batched relays) |
100 VUs, 30s k6, 1,000 JWTs, 3 runs, full 3-service drain | Ryzen 7 6800H / 16 GB / WSL2 | 319.53 | 0 msgs / 0.00s | 0 msgs / 0.00s | 0 msgs / 0.00s |
| Test Script | Workload / Failure Mode | Host | Verified Raw Counts |
|---|---|---|---|
scripts/test-poison-batch.js |
50-msg batch (49 valid + 1 userId=null DB error) + 1 follow-up msg |
Ryzen 7 6800H / 16 GB / WSL2 | 49/49 orders created, 1 message in payment-successful.DLQ, follow-up #51 processed (1/1), Kafka lag = 0 |
scripts/test-mid-batch-crash.js |
50-msg batch committed to DB, SIGKILL before offset commit, restart |
Ryzen 7 6800H / 16 GB / WSL2 | Lag before kill = 50; after restart: 50/50 orders present, 0 duplicates, 50/50 outbox published, lag = 0 |
scripts/test-outbox-outage.js |
Stop Kafka, send 500 checkouts, restart Kafka + 2 SKIP LOCKED relays |
Ryzen 7 6800H / 16 GB / WSL2 | 500/500 HTTP 200 in 2.21s (0 orders while down); after recovery: 500/500 orders in 31.1s, 0 lost, 0 duplicates |
scripts/test-order-crash-recovery.js |
Kill order-service after DB commit and before outbox publish |
Ryzen 7 6800H / 16 GB / WSL2 | 1/1 order persisted, 0 duplicates, 1/1 order-successful published, 1/1 email processed |
- Single-laptop benchmark host: All 7 Docker containers (PostgreSQL, Kafka, API Gateway, and 4 microservices) plus the k6 load generator ran on one laptop (Ryzen 7 6800H, 16 GB RAM, Docker Desktop WSL2). Numbers reflect relative behavior under shared CPU/disk contention, not isolated multi-node production capacity.
- Shared
outbox_eventstable:payment-service,order-service, andemail-serviceshare a single PostgreSQL database andoutbox_eventstable, isolated logically bytopic, per-topic partial indexes, and topic-scoped pruners. A full production microservice split would give each service its own database instance and outbox table. - 25 ms relay busy pause (
RELAY_BUSY_PAUSE_MS=25) is an observed effect: Early comparisons between 0 ms and 25 ms were confounded by environment drift and unforwarded env vars; no isolated event-loop lag or PostgreSQL lock-wait experiment proved 25 ms as a causal mechanism. Under verifiedRELAY_BATCH_SEND=true+RELAY_BUSY_PAUSE_MS=25, batched sends do not starve checkout ingress, and this setting is documented strictly as an observed working parameter. DEMO_AUTHtoken endpoint is for demos and local testing only:POST /api/auth/tokenandPOST /api/auth/tokens/batchonapi-gatewaymint unsigned-passwordless demo JWTs for local tests, benchmarks, and demos. They are explicitly gated off (HTTP 404) wheneverNODE_ENV=productionorDEMO_AUTH !== "true", and must never be enabled in production.
- API Gateway: Node.js, Express,
http-proxy-middleware(HTTP keep-alive), JWT verification - Microservices: Node.js (
payment-service,order-service,email-service,analytic-service) - Message Broker: Apache Kafka 3.7.0 (KRaft mode — no Zookeeper)
- Database & ORM: PostgreSQL 16 (
postgres:16-alpine), Drizzle ORM - Testing & CI: Node test runner, Jest, Supertest, k6, GitHub Actions (unit tests + Docker Compose resilience suite)
- Containerization: Docker & Docker Compose (
docker-compose.yml)
Clone the repository and configure your environment:
git clone https://github.com/vpn-cli/streammart.git
cd streammart
cp .env.example .envTo generate a secure 32-character hex secret:
openssl rand -hex 32Note: If you start services via
node scripts/start-stack.js, it automatically detects ifJWT_SECRETis missing in.env, generates a secure random 32-byte secret (crypto.randomBytes(32).toString('hex')), and persists it to your local gitignored.env.
Start PostgreSQL, Apache Kafka (KRaft), Kafka initializer, and all 5 services with health check dependencies:
# Build and start all containers with health checks
npm run stack:up
# (equivalent to: docker compose up -d --build --wait)
# Verify running container status & health
npm run stack:ps
# Stop the stack
npm run stack:downnpm run demo:outageSimulates a broker outage by stopping Kafka, sending 20 checkouts through the API Gateway (verifying HTTP 200 responses and safely queued PostgreSQL outbox rows while Kafka is offline), restarting Kafka, and observing every checkout completing end-to-end with 0 lost and 0 duplicates.
# Unit test suites across all 5 microservices
npm test
# Per-service unit test targets
npm run test:gateway
npm run test:payment
npm run test:order
npm run test:email
npm run test:analytic
# Live Docker Compose resilience tests (requires stack running)
node scripts/test-poison-batch.js
node scripts/test-mid-batch-crash.js
node scripts/test-order-crash-recovery.js
node scripts/test-outbox-outage.jsThe repository runs an automated two-stage pipeline on GitHub Actions (.github/workflows/ci.yml) on every push and pull request to main, master, and fix/*:
unit-and-integration-tests:- Generates a masked ephemeral
JWT_SECRET(openssl rand -hex 32). - Runs
npm testacross all 5 services (api-gateway,payment-service,order-service,email-service,analytic-service).
- Generates a masked ephemeral
docker-resilience-tests:- Deploys the complete containerized stack using
docker compose up -d --build --wait. - Executes all 4 live fault-injection and crash-recovery test harnesses:
- Poison message isolation and batch fallback (
test-poison-batch.js) - Mid-batch uncommitted offset crash recovery (
test-mid-batch-crash.js) - Order-service crash recovery between DB commit and event emit (
test-order-crash-recovery.js) - Kafka broker outage survival with concurrent
SKIP LOCKEDrelays (test-outbox-outage.js)
- Poison message isolation and batch fallback (
- Collects container logs on failure and tears down stack volumes cleanly.
- Deploys the complete containerized stack using
micro-services/
├── api-gateway/ # Reverse proxy, JWT signing & verification, keep-alive agent
│ ├── config.js # Centralized JWT_SECRET startup validation
│ ├── index.js # Routing & demo auth token minting
│ ├── auth.middleware.js # JWT bearer token verification
│ └── tests/auth.test.js # Auth & gateway security test suite
├── payment-service/ # Ingress checkout endpoint & Transactional Outbox
│ ├── index.js # HTTP endpoints (/payment, /health, /relay-status)
│ ├── worker.js # Outbox relay (SKIP LOCKED) & retention pruner
│ └── db/ # Drizzle schema (payments, outbox_events)
├── order-service/ # Idempotent batch event consumer & order creator
│ ├── worker.js # Outbox relay for order-successful events
│ ├── consumer-processor.js # eachBatch processing with poison-pill 1-by-1 fallback
│ └── db/ # Schema (orders, processed_events, outbox_events)
├── email-service/ # Receipt dispatch consumer & outbox relay
│ ├── worker.js # Outbox relay for email-successful events
│ ├── consumer-processor.js # eachBatch processing & idempotency deduplication
│ └── db/ # Schema (processed_events, outbox_events)
├── analytic-service/ # Cross-topic event collector & metrics consumer
│ ├── index.js # Multi-topic eachBatch consumer
│ └── db/ # Schema (processed_events)
├── benchmarks/ # K6 benchmarks, raw result JSONs, and latency runners
├── scripts/ # Outage demo, crash recovery harnesses, and startup scripts
├── docker-compose.yml # Multi-container orchestration (KRaft Kafka, Postgres 16)
└── .github/workflows/ci.yml # Automated CI pipeline (unit + Docker resilience suite)
