A production-grade inventory reservation system built for multi-warehouse D2C brands. This system solves the classic e-commerce race condition: preventing overselling during high-traffic checkouts without artificially depleting stock during cart abandonment.
URL: [Your Vercel URL will go here]
- Framework: Next.js 14 (App Router)
- Language: TypeScript (End-to-End Type Safety)
- Database: PostgreSQL (Supabase) + Prisma ORM
- Concurrency/Idempotency: Upstash Redis
- Validation: Zod
- UI/UX: Tailwind CSS + shadcn/ui + Sonner
Instead of a naive "check then update" logic, this system utilizes Pessimistic Locking (SELECT FOR UPDATE SKIP LOCKED) within a single database transaction.
- Result: It is mathematically impossible to oversell an item, even if thousands of requests hit the same SKU at the same millisecond.
Real-world warehouses have "shrinkage" (lost/damaged items).
- Algorithm: We implemented a Warehouse Reliability Score using Laplace Smoothing.
- Logic: If a warehouse has a history of missing items, the engine automatically calculates a Shadow Buffer (e.g., hiding 15% of stock from the public).
- Benefit: Protects the customer experience by only promising stock we are confident exists.
To handle network retries and "double-click" scenarios, we implemented a custom withIdempotency wrapper.
- Logic: Uses Redis
SET NXwith a TTL to ensure that duplicate requests with the sameIdempotency-Keydo not trigger multiple side effects.
Reservations automatically expire after 10 minutes via:
- Vercel Cron Job: Frequent background sweeps.
- Lazy Cleanup on Read: Ensuring users never see "dead" stock.
- API Validation: The
/confirmendpoint rejects tokens if the DB timestamp has passed.
-
Clone the Repo:
git clone <your-repo-url> cd allo-inventory
-
Environment Variables: Create a
.envfile with:DATABASE_URL="your_supabase_connection_string" DIRECT_URL="your_supabase_direct_string" REDIS_URL="your_upstash_redis_url" REDIS_TOKEN="your_upstash_redis_token" CRON_SECRET="your_secret_key"
-
Install & Sync:
npm install npx prisma db push npm run db:seed
-
Run:
npm run dev
- Go to
/admin/inventory. - Click "Report Error" on a warehouse.
- Observe the Trust % drop and the Shadow Buffer automatically hiding stock on the storefront.
- Click "Mark Success" to see the system "heal" itself as trust increases.