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: What types of orders should the system support? Just basic market and limit orders, or more complex types like stop-loss or fill-or-kill?
Interviewer: For this design, let's stick to Market and Limit orders for both buying and selling.
Candidate: Should the system handle user accounts, including cash balances and stock portfolios?
Interviewer: Yes, each user must have an account that tracks their cash balance and the quantity of each stock they own. The system must ensure that a user has sufficient funds to buy or enough stock to sell.
Candidate: Should we support multiple stocks being traded simultaneously, or focus on a single stock for simplicity?
Interviewer: Multiple stocks should be supported.
Candidate: How should the order matching engine work? Should it follow a price-time priority (first come, first served at the same price), or is simple price priority sufficient?
Interviewer: Let's keep the matching logic simple. The engine should match the highest bid (buy order) with the lowest ask (sell order) as long as the bid is greater than or equal to the ask. We can ignore the time priority aspect for now.
Candidate: Do we need to notify users of events like when a trade executes or when a stock's price changes?
Interviewer: A user should be notified about status changes to their own orders (e.g., OPEN -> FILLED). It would also be great if users could "subscribe" to specific stocks and get notified when their prices change.
After gathering the details, we can summarize the key system requirements.