MODULE 04 · HIGH-FREQUENCY REAL-TIME & TRANSACTIONAL SAFETY

Stock Trading App

Decoupling 500+ ticks/sec WebSocket bursts from a 60fps UI via ring-buffer conflation, enforcing zero-duplicate idempotent order execution, and guarding against stale-quote trades.

100ms Conflation Window UUIDv4 Idempotency Stale-Quote Lock (<3s) Zero-Dependency Contracts
Act I · Scope, Constraints & Device Budgets

Trading Safety Guarantees & Frame Envelope

Step 1 of 5
Functional & Safety Scope
Core Requirements
  • Watchlist Live Ticks & Level 2 Depth: Ingest high-frequency bid/ask streams for 50+ tickers without janking main-thread scrolling or chart rendering.
  • Market & Limit Order Single-Execution Guarantee: Pessimistic local ledger commit + deterministic Idempotency-Key prevents double-buys on network timeouts.
  • Stale-Price Circuit Breaker: Automatically dim prices and lock Buy/Sell submission if quote freshness exceeds 3,000 ms or a sequence gap is detected.
  • Atomic Portfolio P&L Sync: Reconcile authoritative server fills into the Local SSOT ledger before updating cash buying power and open positions.
Non-Functional & Device Budgets
4 Hard Limits
UI Frame Budget
16.6 ms

Strict 60fps lock even during 500 ticks/sec market-open volatility bursts.

Tick Conflation
100 ms

Fixed sampling window capping UI repaints at 10 atomic snapshots/sec.

Duplicate Orders
0.00%

Zero double-execution tolerance across retries, 504 timeouts, or app kills.

Stale Lockout
< 3.0 s

Watchdog lockout threshold disabling order entry when quotes go dark.

Act II · API, Wire & Local Storage Contract

Tick Stream Frames, Idempotent Order API & SQLite WAL

Step 2 of 5
Wire & Idempotent Order API
WS + REST
Local SSOT Schema (SQLite WAL)
Ledger Integrity
Act III · Interactive 5-Node Topology, Telemetry HUD & Chaos Switchboard

Tick Conflation, Idempotent Execution & Stale-Quote Simulator

FEED LIVE · 60FPS
Step 1 / 6
5-Node Topology · Tick Burst & Conflation
WS INGRESS
candlestick_chart UI Viewport 60fps Watchlist & Ticket 60FPS · 6.2ms compress Tick Conflator 100ms Sampler · Watchdog 10 SNAPSHOTS/S database Local SSOT DB Order Ledger & Quotes WAL · IDLE router Stream & REST GW WS + Idempotent HTTP WS LIVE · IO THREAD dns Exchange Engine Matching & Idempotency SEQ #88412 · LIVE
TICK BURST

Loading simulator...

Invariant active
Event Telemetry Console
Live Telemetry HUD
Real-Time
UI Frame Time
6.2 ms
60fps locked (<16.6ms)
Ingress → UI Rate
120/s → 10/s
100ms conflation window
Quote Freshness
LIVE (42ms)
Order entry enabled
Order Ledger State
IDLE
0.00% duplicate risk
Chaos Switchboard
0 Active
Act IV · Zero-Dependency Kotlin Contracts & Platform Mapping

Library-Agnostic Core & Android / iOS Equivalents

Auto-Synced to Simulator
Platform Mapping Strip (Android & iOS Equivalents) 6 Agnostic Primitives
Agnostic Primitive Android Equivalent iOS Equivalent
High-Frequency Conflator Kotlin Flow (sample / conflate) + StateFlow Combine (throttle) / AsyncAlgorithms
Persistent Order Ledger (SSOT) Room / SQLDelight (SQLite WAL) Core Data / SwiftData / GRDB
Full-Duplex Tick Stream OkHttp WebSocket / Ktor URLSessionWebSocketTask
Idempotent Order Client Custom Interceptor + UUIDv4 Header URLRequest + UUID Header
Trade Confirmation Security Android BiometricPrompt + Keystore LocalAuthentication + Secure Enclave
App Lifecycle Stream Pause ProcessLifecycleOwner UIApplication ScenePhase / NotificationCenter
Act V · Architectural Tradeoffs & Production Observability SLOs

Engineering Tradeoffs & Reliability Targets

Step 5 of 5
Tradeoff 01 60fps Safe

Latest-Value Conflation vs. Full Tick Queue

Latest-Value Conflation

Overwrites per-symbol slot in O(1) and emits 1 immutable snapshot every 100ms—memory stays strictly O(symbols).

Full Tick Queue

Backlogs 500 ticks/sec during market open, delaying displayed prices by seconds and starving the UI thread.

Defense: Traders only care about the freshest bid/ask. Dropping intermediate ticks between 100ms paints protects both latency and frame time.

Tradeoff 02 Idempotent

REST for Orders vs. WebSocket for Orders

REST + Idempotency-Key

Explicit request/response status codes, standardized Idempotency-Key caching, and independent retry semantics.

WebSocket Orders

Shares congestion with noisy market-data ticks and requires custom correlation IDs and timeout state machines.

Defense: Separate the data plane (WebSocket tick bursts) from the transactional control plane (idempotent REST POST /v1/orders).

Tradeoff 03 Financial Safety

Pessimistic Order Lock vs. Optimistic UI

Pessimistic Ledger Lock

Persists SUBMITTING locally and waits for authoritative exchange confirmation before mutating cash balance.

Optimistic Fill UI

Showing a fake fill before matching engine execution causes phantom buying power and dangerous rollback confusion.

Defense: Unlike social likes or chat messages, financial fills cannot be assumed locally—show a clear SUBMITTING state backed by SQLite WAL until confirmed.

Tradeoff 04 Fast Resync

Snapshot + Delta vs. Full Book Resend

Snapshot + Delta

Fetch one atomic REST snapshot on connect or sequence gap, then apply tiny incremental WebSocket deltas.

Full Book Resend

Broadcasting the entire order book on every tick wastes cellular bandwidth and triggers constant JSON parsing GC.

Defense: Sequence numbers (seq) detect any missed delta immediately, falling back to a sub-600ms REST snapshot hydrate.

Production Observability SLOs
Financial-Grade Targets
Duplicate Order Rate 0.00%
P99 UI Frame Time < 14.0 ms
Stale Quote Lockout < 3.0 s
Gap Resync Latency < 600 ms