Backend and payments
RyuStoreFront
Status today: Live at ryustorefront.com on AWS, with Stripe in test mode only (no card is charged). Earlier AWS sessions on 2026-11-15 and 2026-11-23 were measured and then destroyed; no number below comes from the current deployment. Order history and the admin API are still being built.
The problem
Online stores fail in the same few places. A payment arrives twice, or late, or after the cart expired. Two shoppers take the last pair. A background job dies halfway through. Most store demos show a catalogue and a cart and skip all of that.
I wanted a small shoe store where those cases are designed, tested and written down. The scope is deliberately narrow: Stripe test mode only, flat $8 shipping, one currency, no tax engine, no multi-vendor features. Nothing here ships a real shoe.
What I built
What exists, and what does not. Nothing in the second column is claimed as done.
Built and tested
- Catalogue API with Postgres full-text search, filters, sort and pagination
- Sign-in with server-side sessions and CSRF tokens; a guest cart that merges on sign-in
- Checkout with atomic stock reservation, an idempotency key and an expiring hold
- Stripe Checkout (hosted page), a signed webhook, refunds when the money cannot be honoured
- Order state machine, and a transactional outbox with a background worker for email, cart clearing and expiry
- Next.js storefront wired to the API, with 142 demo products and 188 licensed photos served as AVIF and WebP
- Terraform for a persistent foundation stage and a destroyable per-session stage that runs the API, worker and frontend
Not built yet
- Order history and the admin API (the admin screens exist only as designs on fixtures)
- Login rate limiting, password reset, email verification
- A load test and a measured cloud cost
- Photos on the cart and order pages (their APIs return none yet)
System architecture
This diagram scrolls sideways on a narrow screen.
It is a modular monolith: packages for auth, cart, catalogue, checkout, inventory, orders, outbox, payments, notifications and a worker, with a Spring Modulith test that fails the build if a module reaches into another's internals. One jar runs as the API or, with a different profile, as the worker. PostgreSQL is the only store, so there is no cache or queue to keep consistent with it.
This diagram scrolls sideways on a narrow screen.
Every status change goes through one function that locks the order row, checks this table and records history. Asking for the status an order already has is a no-op, which is exactly what a replayed webhook looks like.
Decisions and what each cost
| Decision | Choice | Tradeoff accepted |
|---|---|---|
| Stock reservation | Atomic UPDATE ... WHERE stock >= n plus a reservation row with an expiry | A worker must release expired holds. No row locks to reason about, and a CHECK (stock >= 0) backstops it. |
| Background work | An outbox table in Postgres, drained by the worker (SQS was the original plan) | At-least-once delivery, so handlers must be idempotent. One event per transaction: fine for a shop, not a stream. |
| Authentication | Server-side sessions in the database, CSRF token on every write | The Next.js client must fetch a token first and forward cookies; an in-app proxy keeps them first-party. |
| Packaging | One jar, two profiles, two ECS services | A looser dependency graph than separate modules. Split only if it hurts. |
| Cloud networking | Fargate tasks in public subnets, no NAT gateway | Saves the NAT charge (not measured). The security group is then the only barrier, so I do not call it a private network. |
The three hardest problems
1. A payment webhook that must be safe to receive twice, late, or out of order
Stripe retries and reorders events, and a customer can pay after the order has expired. The order becomes paid only through the signed webhook, which does this:
- Verify the signature over the raw request bytes with a five-minute tolerance. A re-serialised body would not match. Bad, missing or stale signatures get a 400.
- In one transaction, insert the event ID with
ON CONFLICT DO NOTHING. If nothing was inserted it is a repeat: answer 200 and stop. - Lock the order row, check that the amount and currency match, move it to paid, record the payment, and commit the stock hold.
- If anything throws, roll back everything, including the event ID, and answer 500 so Stripe retries.
The hard cases are the ones where the money cannot be honoured: a payment on an order that already expired, a wrong amount, or a stock hold that vanished. Those record the payment, queue a refund through the outbox, and issue it with a Stripe idempotency key. The order stays cancelled; I did not add a cancelled-to-refunded move, because a paid order is never cancelled.
Tradeoff: the refund runs after commit, so for a short window (about one worker poll) the shopper is charged and not yet refunded.
2. Not overselling, without a deadlock
Reservation is one conditional UPDATE ... WHERE stock >= n inside the checkout transaction, so a failure anywhere leaves no order and no hold. A per-customer advisory lock serialises one customer's checkouts and removes a duplicate-key race on the idempotency key.
While adding the state machine I found that checkout's "cancel the old pending order" step released stock before cancelling, while a webhook paying the same order locked in the opposite order. That is a possible deadlock. I fixed it by fixing one lock order everywhere: the order row, then its reservations, then variant rows, and by cancelling before releasing stock.
3. A worker that can be killed at any moment
Events are written in the same transaction as the change that caused them, and the publish call refuses to run outside a transaction, so an event exists if and only if its change committed. The worker claims one event at a time with FOR UPDATE SKIP LOCKED, runs its handlers and marks it processed in one transaction. Kill the process and nothing commits, so the event is simply delivered again.
A recorded limit: a kill is not counted as an attempt, so an event that crashes the whole JVM every time would retry forever. Nothing does that today; alarms on the age of the oldest pending event are the planned guard.
Evidence
Every number here has a row in the project's docs/metrics.md, and only rows marked Run (the output was seen) or User-reported (I ran it and recorded the result) are quoted. Each line says whether it was measured locally or on AWS. I ran none of these tests again while writing this page.
Correctness under failure (local)
The same Stripe event delivered 3 times in a row, and 6 times at the same moment, leaves 1 paid order, 1 payment row, 1 order.paid event and 1 confirmation-email record.
Local: one JVM, Postgres 18.7 in Testcontainers, signatures from an independent HMAC-SHA256 (StripeWebhookTests, 19 tests) [B2].
Expired-then-completed is refunded once and the order stays cancelled. Completed-then-expired stays paid.
Local: same test class and run as the duplicate test, with a fake payment gateway. Refunds were not exercised against real Stripe [B2].
50 threads each reserving 1 unit against 10 in stock produced exactly 10 reservations and 0 stock left, in each of 20 consecutive rounds. At checkout level, 20 buyers for 5 units produced exactly 5 orders in each of 3 rounds.
Local: one JVM, threads released by a latch, connection pool of 60, Postgres 18.7 in Testcontainers on one VM. Not a traffic claim [B3, B4].
A separate worker JVM killed with SIGKILL mid-job: its writes vanish, the event stays pending with 0 attempts, and a restarted worker completes it exactly once. Separately, 4 competing workers handled 60 events, each exactly once.
Local: two child JVMs against the test database, test time 51.8 s (WorkerKillTests); OutboxProcessorTests with a 5 ms handler, not a throughput claim [B5, B7].
All 36 status pairs (6 statuses × 6) are tested. 10 concurrent identical transitions apply once, and a payment racing a cancellation has one winner in each of 20 rounds.
Local: unit table plus the real service and Postgres (OrderStatusTests, OrderTransitionTests) [B6].
Full backend suite: 245 tests, 0 failures, 0 errors, 0 skipped, in 28 classes.
Local: dev VM, Java 25, Postgres 18.7 and Mailpit in Testcontainers, 6 min 32 s [B1c].
On AWS
A Terraform session stage of 44 resources ran the API, the worker and the frontend on AWS. One real purchase in Stripe test mode was paid through the webhook, and the worker processed the order.paid event. An unsigned webhook call was rejected.
AWS us-west-2: Fargate (API 0.5 vCPU / 1 GB), ALB over HTTPS, RDS Postgres, Stripe test mode, card 4242. The shop’s order history page is still a design fixture [C11, C14].
The live browser checks passed 19 of 19 against the deployed shop, in two runs (1.8 and 1.7 min). An earlier run had 1 failure, a test race that was fixed in the test.
AWS: Playwright 1.65.1, Chromium, real DNS, TLS, ALB host routing and CloudFront photos [C13].
The confirmation email failed 6 times against the SES sandbox (gaps growing from about 5 s to about 80 s), then sent once the recipient was verified.
AWS: worker on Fargate, SES sandbox. That the recipient was verified in between is assumed, not confirmed. Not the same as the worker-kill test [C15].
The first session ended with a 31-resource destroy plan and an empty state afterwards, then a 13-check audit came back all ok. The second session’s 44 resources were destroyed in two runs, followed by a clean audit.
AWS: Terraform 1.17.2. The first “Destroy complete” line was not captured; the second destroy was split by expired credentials. Session cost was not measured [C2, C3, C11].
The API started in 55.2 s on a fresh database (including migrations and seeding), and in 50.4, 58.8 and 59.4 s in the second session. The worker took 121.9 s.
AWS: Fargate 0.5 vCPU / 1 GB, x86_64. Separate single runs, not averages [C4, C12].
Queries, frontend and accessibility (local)
At 50,000 products and 750,680 variants, after adding a maintained minimum price per product: count 518 ms to 22 ms, category sorted by price 227 ms to 20 ms, deep page 28 ms to 5.1 ms.
Local Postgres 18.7 in Docker, warm cache, single runs of EXPLAIN (the ledger marks them for re-measurement). Not a load test [D1, D2, D3].
After the redesign, an axe scan (WCAG 2.0/2.1 A and AA rules) found 0 violations on 119 pages at 1440 and 390 px. The quality gates passed 19 of 19, including 12 routes at 320 px with 0 px sideways overflow.
Local: mock API, production build, Playwright 1.65.1 with @axe-core/playwright 4.14.0, Chromium only. Axe covers only the automatable share of WCAG [F10, F9c].
The demo catalogue has 142 products, 1,113 variants and 188 licensed photos. The 960 px AVIF a card loads is a median 12.7 % of its original’s bytes (mean 32.6 KB against a 270.7 KB mean original).
Local: Compose Postgres and SeaweedFS; sharp 0.35.5, AVIF q50. Bytes on disk, not transferred [D6b, F5].
After the redesign, Lighthouse accessibility was 100 on home, listing, product and sign-in. Switching to subset fonts cut font bytes from 138.0 KB to 51.8 KB (home on mobile: 345.7 KB to 260.6 KB). Real photos made the mobile listing’s largest paint slower; loading the first two cards eagerly brought it from 4,459 ms to 2,702 ms, still later than with placeholders.
Local: production build on the dev VM, Lighthouse 13.6.0 with simulated slow 4G and 4× CPU, medians of 3 or 5 runs. Modelled, not field data [F3b, F8, F11b].
Not measured yet
No API latency or throughput under load, no webhook replay against the cloud endpoint, no real per-session cost and no rebuild-from-zero time exist yet.
Listed as Not run in the project’s ledger [P2, C8, C9, C10].
Screenshots
Each image says what it was captured from. The brands and products are invented; the photos are licensed (CC0 or public domain) and credited on the store's credits page.




Tradeoffs and limits
- The site is live, but every AWS number on this page comes from the earlier, destroyed sessions. The shop's order history page is still a design fixture, so the cloud purchase was confirmed from Stripe and the worker log, not from the shop.
- No load test and no measured cost per session. I make no claim about scale, throughput or latency under load. Lighthouse figures are local and simulated.
- Real photos made the listing's largest paint slower than the placeholders, even after the fix.
- Tasks have public IP addresses; the security group is the only barrier. The database connection encrypts but does not verify the server certificate.
- The database on AWS was Postgres 18.6 while tests ran on 18.7; the application's tests were not run against RDS.
- Sign-in has no rate limiting, password reset or email verification.
- Accessibility was checked with axe in Chromium only, on a mock API; no screen-reader walkthrough yet.
- All test results are my own records. Nobody else has reviewed them.
What I would improve next
All of this is planned, not done.
- Finish order history and the admin API, then wire the admin screens.
- Hardening: login rate limits, security headers, a dependency scan, and a load test whose setup is written down.
- Measure a session's real cost, replay webhooks against the cloud endpoint, and time a rebuild from zero.
- An alarm on the age of the oldest pending outbox event, and a metric for dead events.
Links
- Repository: github.com/jabrogena5100/RyuStoreFront (public). Its
docs/metrics.mdholds every number with its setup, date and status, and the decision log is indocs/decisions.md. - Live: ryustorefront.com (Stripe test mode: card 4242 4242 4242 4242, no card is charged)
- All projects