- Golang 1.25.3
- Chi - routing
- SQLC - queries
- Goose - migrations
- Docker
- Transport layer - http/grpc - cmd/api.go, cmd/main.go
- Service layer - all business logic ex: getOrders, getOrderItems
- Repository layer - postgres - data fetching - SELECT * FROM products;
- CMD folder - used for executables, CLI, etc.
- Internal folder - reserved folder that doesn't export.
docker compose up -d- start the postgres databasego run cmd/*.go- run the applicationsqlc generate- will regenerate your SQL queriesgoose -s create create_products sql- will create migrations underinternal/adapter/postgres/migrationsgoose up- apply all available migrationsgoose down- roll back a single migration from the current version
Objective:
Support partial fulfillment by introducing per-item order statuses.
- Implement an
order_item.statusfield to represent the fulfillment state of each item within an order. - Enable the following logic:
- For multi-item orders, if any item is out of stock, set its status to
backordered. - Items that are in stock should have their status set to
fulfilled.
- For multi-item orders, if any item is out of stock, set its status to
- Example:
- An order contains Item A (in stock) and Item B (out of stock):
- Item A →
fulfilled - Item B →
backordered
- Item A →
- An order contains Item A (in stock) and Item B (out of stock):
Current Challenge:
Inventory levels are updated per item immediately, which allows concurrent orders for the same product to both pass stock checks. This can result in overselling.
Recommended Solutions:
- Lock inventory rows using
SELECT FOR UPDATEwithin a transaction. - Implement a timeout (e.g., 2 seconds) to minimize the risk of long-held locks.
- Pros: Provides strong consistency and is effective for moderate traffic volumes.
- Cons: May cause contention for popular items under high load.
- Upon ordering, create a temporary reservation of inventory in Redis.
- Set a reservation expiry (e.g., 15 minutes) to auto-release stock in case of order abandonment.
- Optionally, use Redis counters for tracking "hot items" and supporting high-demand sales scenarios.
- Supplement with background jobs for clean-up or reconciliation if necessary.
- Pros: Fast, scalable, and suitable for events like flash sales.
- Cons: Additional complexity; requires operational Redis.
- Queue incoming order requests to serialize access to inventory for popular or high-traffic items.
- Ensures consistent stock updates and avoids race conditions under high concurrency.
- Pros: Handles conflict resolution at scale and provides graceful degradation.
- Cons: May introduce order processing latency.