Problem: Concurrent checkout races and noisy demand
Small-shop owners track stock on paper and reorder on guesswork. DukaPulse is a full-stack retail POS and telemetry system with role-based access control, race-proof inventory checkouts, and demand forecasts that earn their compute by beating moving baselines.
The Oversell Hazard at the Register
In neighborhood dukas, shelf inventory is finite and working margins are slim. When two clerks or mobile payment webhooks claim the final unit concurrently, application-layer checks permit phantom sales. DukaPulse enforces inventory safety directly at the database engine: checkouts run an atomic conditional decrement (UPDATE inventory SET current_stock = current_stock - :qty WHERE current_stock >= :qty RETURNING) paired with an engine-level CHECK (current_stock >= 0), while restock shipments take pessimistic row locks (SELECT ... FOR UPDATE). Verified in CI across 16 concurrent threads executing 10,000 checkout attempts against a single unit (tests/test_integration_engine.py#L160-L226): exactly 1 purchase succeeded, 9,999 returned 409 InsufficientStockError, and final stock remained zero without going negative.
02/ENGINEERING METHODOLOGY
Approach: Atomic SQL guards, TimescaleDB rollups, and ONNX search
FastAPI, TimescaleDB, and PostgreSQL on the backend with a kiosk POS and Streamlit analytics dashboard. Sales decrement atomically at the engine level with CHECK (current_stock >= 0) to eliminate oversell races, while restock uses SELECT FOR UPDATE row locks. Time-series events write to a 7-day chunk hypertable with continuous aggregates refreshing at 30-minute and 1-hour cadences on Timescale Cloud. Catalog search runs 384-dimensional FastEmbed ONNX embeddings with Swahili vernacular expansion.
Application-Layer Check
Race Prone
Vulnerable to concurrent debit hazards
Atomic Decrement + CHECK
0 Oversells
10,000 CI race: 1 success, 9,999 × 409 (tests/test_integration_engine.py)
03/MEASURABLE OUTCOMES
Results: Zero oversells in 10,000-attempt CI race and honest forecasts
30 tests (25 unit + 5 real-engine integration) · 10,000 concurrent checkout attempts · 0 oversells · 60 days of history
Concurrency verified in CI: 16 threads racing 10,000 checkouts against 1 unit yield exactly 1 success, 9,999 × 409 InsufficientStockError, and zero oversells (tests/test_integration_engine.py#L160-L226)
Forecasting tournament: Seasonal-Naive won 14/14 products (1.012% to 5.433% baseline MAPE vs 2.533% to 16.58% Holt Linear); ships the baseline by design (docs/receipts/forecast_mape.json)
Semantic retrieval: ONNX all-MiniLM-L6-v2 matches vernacular queries (unga ya ugali 0.8611, sabuni ya kuoga 0.6119, mafuta ya kupika 0.7743) and out-of-map English terms (soap 0.5005, rice 0.5541, tea 0.4965)
Auth0 RS256 token verification with RBAC staff margin redaction and restock 403; dev mode uses an isolated mock-token signer gated by DEV_MODE_MOCK_AUTH (secure by default, demo only)
Rather than deploying complex machine learning models that overfit on sparse small-shop transactions, DukaPulse benchmarks demand models directly against a seasonal-naive baseline. Across all 14 catalog products over 60 days of history, Seasonal-Naive won 14 of 14 tournament matchups against Holt Linear Exponential Smoothing (baseline MAPE 1.012% to 5.433% versus Holt 2.533% to 16.58%; verified in docs/receipts/forecast_mape.json). DukaPulse ships the baseline by design. For search, FastEmbed ONNX all-MiniLM-L6-v2 embeddings (384 dimensions) map vernacular queries via synonym enrichment (unga ya ugali: 0.8611 Pembe, 0.8039 Jogoo; sabuni ya kuoga: 0.6119 Geisha; mafuta ya kupika: 0.7743 Fresh Fri) while raw dense matching handles out-of-map English terms (soap: 0.5005, rice: 0.5541, tea: 0.4965).
Engine-Level Conditional Decrement & CI Stress Receipt
UPDATE inventory
SET current_stock = current_stock - :qty
WHERE sku_id = :sku_id AND current_stock >= :qty
RETURNING current_stock; -- paired with CHECK (current_stock >= 0)
-- 16 threads x 10,000 concurrent attempts on stock = 1:
-- HTTP 200: 1 | HTTP 409: 9,999 | Final stock: 0 (never negative)
Built for MLH Global Hack Week: Builders Week (November 2026). Live Kiosk POS: https://dukapulse-kiosk.onrender.com/kiosk · Analytics Dashboard: https://dukapulse.streamlit.app.
EVIDENCE/PRODUCTION SCREENSHOTS & ARTIFACTS
Product & verification captures
Screenshots and schema diagrams exported from the live app or public repository. Select any frame to inspect at full resolution.
200 vs. 409 Race Proof
Competing checkouts against the last unit on shelf: one atomic decrement commits (200), the rival hits InsufficientStockError (409).
Swahili ONNX Semantic Search
384-dim all-MiniLM-L6-v2 embeddings map vernacular terms like unga ya ugali (0.8611) cleanly to stocked SKUs.
Lipa na M-PESA Checkout
Atomic stock decrement paired with itemized sales receipt creation in a single database transaction.
Auth0 RBAC Margin Redaction
Cashier tokens mask wholesale unit margins in API payloads and reject restock mutations with HTTP 403.
Owner Stock & Margin Ledger
Authenticated owners unlock cost margins and row-locked (SELECT FOR UPDATE) restock shipments.
TimescaleDB Telemetry & Forecasts
7-day hypertable chunks and 30m/1h continuous aggregates feeding the Seasonal-Naive demand tournament.
04/SYSTEM ARCHITECTURE & STACK
Technology Stack
FastAPI
TimescaleDB
PostgreSQL
pgvector
Docker
Streamlit
Auth0
pytest
Verify the implementation
Inspect the source code, tests, and CI configuration on GitHub.