Stock Exchange System Design: How Orders Get Matched @KGPTalkie
Stock Exchange System Design: How Orders Get Matched  @KGPTalkie
Uploaded October 2026 | Updated November 2026, 20 hours ago
You've probably traded on the stock market. Maybe you even lost money. But how does a stock market actually work? On its busiest day, NSE handled 21.89 billion order messages and matched every one against the best price waiting on the other side. In a System Design Interview you get exactly this question: "Design Stock Exchange."

Most candidates say "store all orders in a database, query for a matching sell when a buy comes in, and update both rows". A query per order is far too slow, and two servers on one stock can sell the same shares twice.

- Fix 1: an order book per stock, in memory. Buys sorted highest price first, sells lowest first. At the same price, whoever came first goes first: price-time priority, the way NSE ranks orders too
- Fix 2: the matching rule. Best buy meets best sell, and the trade happens at the resting order's price. Sellers wait with 200 shares at ₹500 and 300 at ₹501; a buy for 400 up to ₹501 fills 200 at ₹500, 200 at ₹501, and 100 shares stay in the book
- Fix 3: one sequencer, one writer. Every order for a stock goes into one queue, in order, and one thread matches it. Spread stocks across machines, never one stock across many. NSE says its platform handles nearly 5 million messages a second
- Fix 4: log first, then match. Each order is written to a sequenced log before it's matched. Matching is deterministic, so replaying the log rebuilds the exact same book
- Fix 5: guardrails. Price bands of 2, 5, 10 or 20% reject a crazy price. If the index falls 10, 15 or 20%, a market-wide circuit breaker halts everything
- The follow-up: the primary data centre fails in the middle of the day. It has happened: on 24 February 2021, NSE halted trading from 11:40 until 3:45. SEBI's rule today: declare a disaster within 30 minutes, restore from the DR site within 45, with near-zero data loss. So replicate the log synchronously to a near site, keep a standby replaying it, switch over with the same book, and drill it every quarter

The answer that gets you selected: an in-memory book with price-time priority, trades at the resting price, one sequencer per stock, a replayable log, guardrails, and a hot standby that takes over in minutes.

Comment and tell me: has an order of yours ever filled only partly?

kgptalkie.com

#shorts #SystemDesign #SystemDesignInterview #StockExchange #NSE #OrderBook #MatchingEngine #StockMarket #BackendInterview #InterviewPrep #SoftwareEngineering #KGPTalkie
Stock Exchange System Design: How Orders Get Matched05 Gen AI Interview Preparation: Multi-Agent vs Single Agent Gen AI System Design  #ai #codingLLM Token Cost: Why the Hindi Bill Comes Out HigherClaude Usage Tracker System Design: 5-Hour + Weekly LimitsTokenization in LLM: 4 Quirks That Confuse ModelsVocabulary Size vs Context Window: Why 128K Means Two Things | LLM ConceptsQwen 3.8 on Ollama vs Muse Glimmer vs Gemma 4 - Local LLM Coding Test on OllamaI Ran Qwen 3.8 Flash Next (Qwen 4) on CPU OnlyInstagram Feed System Design: Interview Question ExplainedProduction RAG for Finance with Qwen 3.8 27B: Fully Local Ollama + Qdrant (Live)AI Chatbot Hallucination: The Bot Promised the Wrong Refund | AI System Design InterviewSentencePiece Tokenizer Explained | Tokenization Without Spaces
Laxmi Kant | KGP Talkie |

Stock Exchange System Design: How Orders Get Matched

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER