AlgoMaster Logo

Design Stock Brokerage System

Ashish

Ashish Pratap Singh

hard

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.

Online Stock Exchange

Modern stock exchanges also provide:

  • Live market data and stock price updates
  • Historical trade information
  • Tools for portfolio and order management
  • Support for various types of orders (e.g., market, limit)

In this chapter, we will explore the low-level design of an online stock exchange system in detail.

Lets start by clarifying the requirements:

1. Clarifying 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:

After gathering the details, we can summarize the key system requirements.

1.1 Functional Requirements

  • Each user must have an account with a cash balance and a portfolio of owned stocks.
  • Support trading of multiple stock symbols
  • Allow users to place limit and market orders (buy or sell)
  • Allow users to cancel pending (unmatched) orders
  • Users should be notified of status updates for their orders.
  • Users should be able to subscribe to stock price updates and receive notifications when the price changes.
  • Before placing an order, the system must validate it (e.g., check for sufficient funds for a buy order or sufficient stock quantity for a sell order).

1.2 Non-Functional Requirements

  • Concurrency: The system must be thread-safe to handle concurrent requests from multiple users placing orders simultaneously. 
  • Modularity: The system should be designed using object-oriented principles, with clear separation of concerns between components like accounts, orders, and the matching engine.
  • Extensibility: The design should be modular and easy to extend. For instance, adding new order types (e.g., Stop-Loss) or different notification channels (e.g., SMS, Email) should not require major refactoring.
  • Simplicity: The system should provide a simple, high-level interface for clients to perform common actions like placing an order, without exposing the underlying complexity of the matching engine.

2. Identifying Core Entities

Premium Content

This content is for premium members only.