Appearance
Step-by-Step Build Order
This guide documents how to rebuild 18-flash-sale-inventory from scratch in 10 logical phases, detailing the rationale and key code snippets for each phase.
Phase 1: Database Foundation & Invariant Constraints
Goal: Create persistent schemas with strict mathematical check constraints.
- Create
src/db/database.pywith an async SQLAlchemy engine:
python
# src/db/database.py
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.orm import declarative_base
DATABASE_URL = "sqlite+aiosqlite:///./flash_sale.db"
engine = create_async_engine(DATABASE_URL, echo=False)
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
Base = declarative_base()- Define
InventoryRecordinsrc/models/inventory.pywith SQLCheckConstraint:
python
# src/models/inventory.py
__table_args__ = (
CheckConstraint("available >= 0", name="chk_inventory_available_nonneg"),
CheckConstraint("reserved >= 0", name="chk_inventory_reserved_nonneg"),
CheckConstraint("sold >= 0", name="chk_inventory_sold_nonneg"),
CheckConstraint("available + reserved + sold == total_allocated", name="chk_inventory_conservation"),
)Rationale: If the database enforces non-negativity and conservation at the kernel level, application bugs can never create phantom stock.
Phase 2: Atomic Stock Reservation Service
Goal: Implement single-statement conditional decrements and in-memory SKU locks.
- Implement
StockReservationService.reserve_stock()insrc/stock/reservation.py:
python
# src/stock/reservation.py
stmt = (
update(InventoryRecord)
.where(InventoryRecord.sku_id == sku_id)
.where(InventoryRecord.available >= quantity)
.values(
available=InventoryRecord.available - quantity,
reserved=InventoryRecord.reserved + quantity,
updated_at=datetime.now(timezone.utc)
)
)
res = await self.session.execute(stmt)
if res.rowcount == 0:
return False, "Insufficient stock", NoneRationale: Executes availability checking and deduction atomically. Eliminates read-modify-write race conditions.
Phase 3: Virtual Waiting Room & Queue Regulation
Goal: Level burst traffic before it hits checkout.
- Create
WaitingRoomManagerinsrc/flow/waiting_room.py. - Implement FIFO queueing with
join_queue()and metered batch admission withadmit_batch(). - Issue cryptographically verifiable admission tokens with 120-second lease times.
Phase 4: Two-Phase Checkout & Idempotency Engine
Goal: Ensure one idempotency key equals at most one order and charge.
- Build
CheckoutService.process_checkout()insrc/flow/checkout.py. - Check existing order by
idempotency_keyand return cached order if found. - Validate reservation token, verify user ownership, and check lease expiry (
expires_at > now). - Atomically transition reservation to
CONFIRMED, incrementsold, and persistOrderRecord.
Phase 5: Distributed Locking with Monotonic Fencing
Goal: Eliminate Martin Kleppmann stale-write hazards.
- Create
DistributedLockManagerinsrc/transactions/distributed_lock.py. - Issue monotonically increasing fencing tokens on each lock acquisition.
- Reject writes where client fencing token is less than the highest recorded token.
Phase 6: Timezone-Aware Scheduled Activation
Goal: Launch flash sales within a tight 1.0s tolerance window across DST boundaries.
- Implement
TimezoneConversionServiceinsrc/sales/activation.py. - Use Python
zoneinfoto convert wall-clock strings to UTC. - Check
is_dst_boundary_crossing()to guarantee exact scheduling.
Phase 7: Pricing, Promotions & Stacking Engine
Goal: Apply discounts while guaranteeing price floors and caps.
- Implement
PricingEngineinsrc/pricing/pricing.py. - Calculate discounts in integer minor cents (avoid floating-point drift).
- Validate
price >= price_floor_centsanddiscount <= max_discount_cap.
Phase 8: Authentication, Session Rotation & Lockout
Goal: Secure accounts with PBKDF2 hashing and exponential cool-down.
- Implement
AuthServiceinsrc/auth/service.pyusinghashlib.pbkdf2_hmac("sha256", ...). - Implement exponential lockout: $15 \times 2^{\text{lockouts}}$ minutes after 5 consecutive failed attempts.
- Support session token rotation and instant revocation.
Phase 9: Observability, Metrics & Invariant Scanners
Goal: Real-time alerts and telemetry.
- Instrument Prometheus gauges in
src/observability/metrics.py. - Implement
OversellDetectorinsrc/observability/oversell.pyto audit all SKU rows for drift. - Expose
/metricsendpoint insrc/api/app.py.
Phase 10: Storefront UI & Live Dashboard
Goal: Responsive client interface with live counters and modals.
- Build
frontend/src/index.htmlwith stock table, telemetry counters, and checkout modal. - Build
frontend/src/app.jswith auto-polling every 3s, sell-through calculators, and status badges.