An Online Stock Exchange is a digital platform where investors can trade stocks and other financial instruments electronically. It serves as a marketplace that brings together buyers and sellers, facilitates order matching, and ensures smooth execution of trades in real time.
Modern stock exchanges also provide:
In this chapter, we will explore the low-level design of an online stock exchange system in detail.
Lets start by clarifying the requirements:
Before starting the design, it's important to ask thoughtful questions to uncover hidden assumptions and better define the scope of the system.
Here is an example of how a conversation between the candidate and the interviewer might unfold:
Candidate: Should the system support only limit orders, or do we also need to handle market orders and stop-loss orders?
Interviewer: Let’s start with limit and market orders. We can skip stop-loss and more advanced order types for now.
Candidate: Should we support multiple stocks being traded simultaneously, or focus on a single stock for simplicity?
Interviewer: Multiple stocks should be supported. Each stock will have its own order book.
Candidate: How should the order matching engine behave? Should it follow price-time priority?
Interviewer: Yes, price-time priority is the standard and should be followed for matching buy and sell orders.
Candidate: Do we need to support partial order fulfillment if a match is not fully available?
Interviewer: Yes, if an order can be partially matched, it should be fulfilled partially and the remaining quantity should stay in the order book.
Candidate: Should we track real-time price updates and publish a trade feed for matched orders?
Interviewer: Yes, the system should publish trade events with relevant details for each match—symbol, price, quantity, timestamp.
Candidate: Do users need to have account balances or portfolios managed by the system?
Interviewer: Not for this version. We can assume a simulation environment where users place orders without account balance verification.
After gathering the details, we can summarize the key system requirements.