Tiered Stats Cache & Fast Route
in-flight deduplication for expensive pnl math
Published: 2026-06-26 | Project: Software as Glass Monorepo | Discipline: Distributed Systems & High-Throughput State
Author: Nicholas Alexander MacAskill — Founder & CTO, Flocano Labs | Canonical: https://www.nicholasmacaskill.com/dossier/memoirs-tiered-stats-cache
Two-Speed API Design
Computing baseball profit requires trade ingestion plus Gamma metadata batching through PolymarketGateway — often 10–50 seconds cold. Live MLB odds only need a single Gamma call (~1–10 seconds). Coupling them caused stats to stay blank while odds loaded.
Fast Stats Route
/memoirs/api/sports/stats returns formatted baseball volume and profit strings immediately when cache is warm:
const [volume, profit] = await Promise.all([
resolveBaseballVolumeText(poly),
resolveBaseballProfitText(poly, gateway),
]);
return NextResponse.json(
{ volume, profit },
{ headers: { 'Cache-Control': 'public, s-maxage=10, stale-while-revalidate=30' } }
);Shared Report Cache
resolveTradingReport deduplicates parallel requests via a module-level reportInFlight promise. Win rate and baseball profit read the same cached PolymarketPnLReport instead of recomputing PnL twice per poll cycle.
Win rate uses the primary-pick method: for each resolved baseball moneyline, the side with the highest gross buy USD is the pick; picks under $5 are excluded from the denominator.