# PayRail lab architecture

Source: [PAKS Stripe Payment Idempotency](https://paks.bayareala8s.com/docs/real-world-scenarios/stripe-payment-idempotency)

## What this lab is (and is not)

| Production (interview whiteboard) | This student lab |
|-----------------------------------|------------------|
| CloudFront → WAF → ALB → ECS Fargate | HTTP API → Lambda |
| Aurora PostgreSQL `idempotency_keys` | DynamoDB `pk = tenant#key` |
| Secrets Manager + `api.stripe.com` | In-account **Stripe mock table** (no live secret) |
| SQS webhooks + EventBridge hourly recon | SQS + EventBridge **every minute** so the drill is visible |

The **state machine** is what you defend in the interview. The AWS services are sized for a $2 afternoon.

![PayRail AWS lab](/sprint-content-sd/diagrams/payrail-aws-lab.svg)

## Charge path

1. Client sends `Idempotency-Key` (UUID per logical checkout, reused on retry).
2. Charge Lambda hashes the body.
3. Conditional `PutItem` claims `processing`. Loser reads the winner.
4. If `completed` — return cached JSON. **No second intent.**
5. If `processing` — **409 in flight**.
6. Call the mock gateway (same key → same `payment_intent_id`).
7. Write `orders` row. Mark key `completed` with the response.
8. Enqueue `payment_intent.succeeded` to SQS.

## Timeout zone

If the process dies after step 3 and before 7, the client sees **504 / UNKNOWN**. Retry the **same key**. Do not generate a new UUID. The sweeper looks up the mock intent and completes or fails the row.

## Why DynamoDB is an honest lab choice

PayRail peak **1,200/s** is well inside DynamoDB. Aurora is the right production default when you want SQL reporting on the ledger. Say that out loud. Do not pretend this lab is your 5k QPS multi-region payment platform.

## Security

- KMS on DynamoDB and SQS
- Least-privilege IAM on named tables and the queue
- No public buckets, no `sk_live_` in source
- 24h TTL on idempotency keys (Stripe default window)
