# Building a Farmers-Market Marketplace That Survives Saturday Morning

A farmers-market marketplace looks simple: vendors list produce, customers place orders, and someone coordinates pickup. In practice, inventory changes by the hour, connectivity at market stalls is unreliable, and products rarely fit standard retail units. A grower may sell tomatoes by the basket, basil by the bunch, and eggs by the dozen, so the catalog must treat units, availability windows, and pickup locations as first-class data.

## Reserving Inventory Safely

The hardest backend problem is preventing overselling when several customers order the last few items simultaneously. We store inventory in PostgreSQL and reserve stock inside a transaction using a conditional update. Reservations expire after ten minutes, allowing abandoned baskets to return stock without requiring vendors to intervene.

```sql
UPDATE inventory
SET available = available - $1, reserved = reserved + $1
WHERE listing_id = $2 AND available >= $1
RETURNING available;
```

## Designing for Unreliable Connections

Vendor tools use an offline-capable web app backed by IndexedDB. Price changes, sold-out actions, and new listings are written to a local queue, displayed immediately, and synchronized when connectivity returns. Every mutation includes an idempotency key, so retrying a request cannot decrement inventory twice or create duplicate listings.

## Modeling Pickup and Fulfillment

Orders are grouped by market, vendor, and pickup window rather than shipped independently. The checkout service validates that all basket items share a compatible market session, then creates vendor-specific fulfillment records under one customer payment. This structure lets each stall mark its portion ready while the customer still sees a single order and receipt.

## Operating on Market Day

We monitor reservation failures, synchronization lag, payment errors, and the number of orders awaiting pickup. Structured logs include market and vendor identifiers but exclude customer contact details. The result is a system tuned less for endless warehouse inventory and more for the messy reality of seasonal supply, intermittent networks, and a two-hour rush on Saturday morning.