Preventing overselling in limited drops
ARTHEN. Commerce platform for my streetwear label.
- Problem
- Limited drops sell out in minutes, and payment is a bank transfer that a person checks later. Every oversold unit is a refund and an apology to someone who already paid.
- My role
- Co-founder and solo engineer. Architecture, data model, API, storefront, admin and deploys.
- Key call
- Stock and quota change only inside single conditional SQL statements, so the database, not application code, prevents overselling.
- Outcome
- Built in two weeks and live behind a holding page. In tests, 50 concurrent claims on a quota of 10, and 200 concurrent reservations on a stock of 10, each succeed exactly 10 times.
Many buyers, limited stock
ARTHEN is a streetwear label I co-founded with a close friend. He owns the brand, the clothes and the marketing. I own everything that runs on a server. The first launch is one product, sold as a limited pre-order.
Most of a drop’s demand arrives in the first minutes. Payment is PromptPay, Thailand’s bank-transfer QR standard, so a “paid” order can wait hours for someone to check the transfer slip. If two people can claim the last unit, one of them pays for something that doesn’t exist.
The second requirement was quieter. The brand plans to grow to 20–50 products, card payments, stocked inventory and customer accounts over three to five years. Launching small could not mean rebuilding later.
Constraints
- One engineer, two weeks. Anything outside the core problem used simple, well-known tools.
- Manual payments. Slip review is a person, so a reservation has to outlive the checkout.
- An edge runtime. Cloudflare Workers can’t keep a database connection between requests.
- Free tiers. Postgres runs on Neon, which suspends after five idle minutes and allows about 400 compute-hours a month.
Key decisions
Concurrency belongs to the database
- Considered
- Check the stock in TypeScript and then write it, or add an application-level lock.
- Chose
- Every quota and stock change is a single conditional UPDATE inside a SQL function. It only succeeds if the result stays within the limit.
- Because
- Postgres row locks serialize the callers, so there is no gap between the check and the write for a second buyer to slip into.
- Cost
- The most important logic lives in SQL migrations, which are harder to read and refactor than TypeScript.
The main concurrency test fires 50 concurrent claims at a quota of 10, and 200 concurrent reservations at a stock of 10. Exactly 10 succeed, every run.
The ledger is the truth; counters are a cache
- Considered
- Increment and decrement stock columns directly.
- Chose
- Every inventory change writes a movement row in the same transaction. Every order status change goes through one state-machine check and is guarded by the status it expects to move from.
- Because
- When a number looks wrong, the ledger says why. And a race that tries to move an order twice fails with an error instead of counting twice.
- Cost
- More rows per order, and a stricter write path for every future feature.
Business rules don't import frameworks
- Considered
- Pricing and order logic inside the API route handlers. It's the fastest thing to write.
- Chose
- A framework-free core package holds the money type, pricing, the order state machine, the PromptPay payload and the payment-provider interface. A test fails the build if it imports Hono, React, Next, Drizzle or Cloudflare.
- Because
- The same code prices an order on the server and formats it in the browser, and the rules are tested in milliseconds without a database. Adding card payments is one new provider with no order-logic changes.
- Cost
- More packages and indirection than a one-product launch strictly needs.
The free tier is a design constraint
- Considered
- An expiry job that checks the database every five minutes.
- Chose
- The cron reads a watermark from KV and touches Postgres only when a hold has actually expired. A closed shop answers every request from KV, without a single query.
- Because
- The naive cron alone would wake the database 288 times a day, spending most of the free compute allowance on doing nothing.
- Cost
- Two places that hold state. A daily unconditional sweep covers the case where a KV write is lost.
Text description of this diagram
Checkout
- A shopper checks out. The API Worker validates the request against the same Zod schema the storefront was built with.
- Core prices the cart in integer minor units and confirms the new order’s first state transition is legal. No database involved.
- One conditional UPDATE claims the quota and writes the ledger row in the same transaction. If the drop is sold out, zero rows change and checkout stops there.
- The PromptPay QR is generated per request and the confirmation goes out by LINE and email. The rendered message is logged so a failed send can be replayed.
Slip review
- An admin opens the transfer slip the customer uploaded and approves it.
- The state machine checks that “payment submitted → confirmed” is a legal move. Anything else throws.
- The UPDATE is guarded by the status it expects to move from. If another admin got there first, nothing changes and the second click gets an error.
- The customer is told the order is confirmed. The hold becomes a sale.
Expiring holds
- Every five minutes the cron reads one value from KV: the earliest moment any hold can expire.
- Only if that moment has passed does it touch Postgres, releasing expired holds in the same statement that selects them, so a second run finds nothing. Otherwise the database stays asleep.
What I chose not to build yet
Every item below was a real option. Each one has a trigger for when it becomes worth building.
| Not built | Why not, yet |
|---|---|
| Microservices or a queue | One Worker, one database. Post-response work uses waitUntil, schedules use cron. A queue earns its place at ~100× launch volume. |
| Redis | KV covers rate limiting; Postgres covers everything durable. |
| GraphQL | The API has exactly two clients. Typed REST with shared Zod schemas is already typed end to end. |
| Automated slip OCR | Manual review takes minutes a day at launch volume and catches what OCR would miss. The fields OCR would fill already exist. |
| Event sourcing | The inventory ledger and the admin audit log already give the audit trail that matters. |
Limiting abuse
Checking out over and over and letting the timers run is a cheap way to lock up a drop. So holds are bounded four ways: a cap on units per order, a cap on open unpaid orders per email or phone, a checkout rate limit per IP, and an optional bot challenge. The idempotent expiry job releases anything past its window. Payment pages also require a random per-order token, so guessing sequential order numbers only returns uniform 404s.
Where it stands
The platform is running in production behind its own “close the shop” holding page while the first samples are made. What can be verified today: no overselling under concurrent load in tests, a database that sleeps when nobody is shopping, and a growth path (cards, more products, stocked inventory) that only adds code. Switching a product from pre-order to stocked is a setting, not a migration.
The first drop will be the real load test.