# Building a Reliable Farmers-Market Marketplace

A farmers-market marketplace has deceptively complex inventory requirements. Unlike conventional e-commerce, vendors may harvest produce in the morning, sell it at a physical stall, and accept online orders from the same limited stock. Our platform models inventory as market-specific batches, including harvest date, available quantity, unit type, and pickup window. This prevents a vendor’s Saturday tomato inventory from being accidentally offered at a Wednesday market.

## Preventing Overselling

Each purchase reserves inventory inside a database transaction. The update succeeds only when the remaining quantity is sufficient, making the database—not an application cache—the final authority. Reservations expire after ten minutes if checkout is abandoned, while completed payments convert them into confirmed allocations. Idempotency keys ensure payment retries cannot create duplicate orders.

```sql
UPDATE inventory_batches
SET available_units = available_units - :quantity
WHERE id = :batch_id
  AND available_units >= :quantity;
```

Physical sales remain the hardest synchronization problem. Vendors use a lightweight mobile interface to record stall transactions, and the API immediately reduces the corresponding batch. When connectivity is poor, the client stores sales locally and replays them in order. We retain a small configurable stock buffer for offline vendors so delayed updates are less likely to oversell their produce.

## Pickup and Fulfillment

Orders are grouped by market, vendor, and pickup window rather than shipped individually. Before the market opens, vendors receive a packing list with product quantities and customer order codes. Customers present a short code at pickup, and scanning it marks every vendor handoff independently, allowing mixed-vendor orders to be fulfilled without forcing shoppers through multiple checkout flows.

## Operating in Production

We monitor reservation failures, inventory reconciliation differences, payment-to-confirmation latency, and unclaimed pickups. Structured audit events record every stock adjustment with its source—online order, stall sale, expiration, or manual correction. That history has proven essential when helping vendors reconcile a busy market day, and it gives the marketplace a practical path from informal spreadsheets to dependable local commerce.