🖥️
Sunil Notebook
Interview Preparation
  • 📒Notebook
    • What is this about ?
  • System Design
    • 💡Key Concepts
      • 🌐Scalability
      • 🌐Latency Vs Throughput
      • 🌐Databases
      • 🌐CAP Theorem
      • 🌐ACID Transactions
      • 🌐Rate limiting
      • 🌐API Design
      • 🌐Strong Vs eventual consistency
      • 🌐Distributed tracing
      • 🌐Synchronous Vs asynchronous Communication
      • 🌐Batch Processing Vs Stream Processing
      • 🌐Fault Tolerance
    • 💎Building Blocks
      • 🔹Message
      • 🔹Cache
      • 🔹Load Balancer Vs API Gateway
    • 🖥️Introduction to system design
    • ⏱️Step By Step Guide
    • ♨️Emerging Technologies in System Design
    • ☑️System design component checklist
      • 🔷Azure
      • 🔶AWS
      • ♦️Google Cloud
    • 🧊LinkedIn feed Design
    • 🏏Scalable Emoji Broadcasting System - Hotstar
    • 💲UPI Payment System Design
    • 📈Stock Broker System Design - Groww
    • 🧑‍🤝‍🧑Designing Instagram's Collaborative Content Creation - Close Friends Only
    • 🌳Vending Machines - Over the air Systems
    • Reference Links
  • DSA
    • Topics
      • Introduction
      • Algorithm analysis
        • Asymptotic Notation
        • Memory
      • Sorting
        • Selection Sort
        • Insertion Sort
        • Merge Sort
        • Quick Sort
        • Quick'3 Sort
        • Shell Sort
        • Shuffle sort
        • Heap Sort
        • Arrays.sort()
        • Key Points
        • Problems
          • Reorder Log files
      • Stacks and Queues
        • Stack Implementations
        • Queue Implementations
        • Priority Queues
        • Problems
          • Dijkstra's two-stack algorithm
      • Binary Search Tree
        • Left Leaning Red Black Tree
          • Java Implementations
        • 2-3 Tree
          • Search Operation - 2-3 Tree
          • Insert Operation - 2-3 Tree
        • Geometric Applications of BST
      • B-Tree
      • Graphs
        • Undirected Graphs
        • Directed Graphs
        • Topological Sort
      • Union Find
        • Dynamic Connectivity
        • Quick Find - Eager Approach
        • Quick Find - Lazy Approach
        • Defects
        • Weighted Quick Union
        • Quick Union + path comparison
        • Amortized Analysis
      • Convex Hull
      • Binary Heaps and Priority Queue
      • Hash Table vs Binary Search Trees
  • Concurrency and Multithreading
    • Introduction
    • Visibility Problem
    • Interview Questions
    • References
      • System design
  • Design Patterns
    • ℹ️Introduction
    • 💠Classification of patterns
    • 1️⃣Structural Design Patterns
      • Adapter Design Pattern
      • Bridge Design Pattern
      • Composite Design Pattern
      • Decorator Design Pattern
      • Facade Design Pattern
      • Flyweight Design Pattern
      • Private Class Data Design Pattern
      • Proxy Design Pattern
    • 2️⃣Behavioral Design Patterns
      • Chain Of Responsibility
      • Command Design Pattern
      • Interpreter Design Pattern
      • Iterator Design Pattern
      • Mediator Design Pattern
      • Memento Design Pattern
      • Null Object Design Pattern
      • Observer Design Pattern
      • State Design Pattern
      • Strategy Design Pattern
      • Template Design Pattern
    • 3️⃣Creational Design Patterns
      • Abstract Factory Design Pattern
      • Builder Design Pattern
      • Factory Method Design Pattern
      • Object Pool Design Pattern
      • Prototype Design Pattern
      • Singleton Design Pattern
    • Java Pass by Value or Pass by Reference
  • Designing Data-Intensive Applications - O'Reilly
    • Read Me
    • 1️⃣Reliable, Scalable, and Maintainable Applications
      • Reliability
      • Scalability
      • Maintainability
      • References
    • 2️⃣Data Models and Query Languages
      • Read me
      • References
    • Miscellaneous
  • Preparation Manual
    • Disclaimer
    • What is it all about?
    • About a bunch of links
    • Before you start preparing
    • Algorithms and Coding
    • Concurrency and Multithreading
    • Programming Language and Fundementals
    • Best Practices and Experience
  • Web Applications
    • Typescript Guidelines
  • Research Papers
    • Research Papers
      • Real-Time Data Infrastructure at Uber
      • Scaling Memcache at Facebook
  • Interview Questions
    • Important links for preparation
    • Google Interview Questions
      • L4
        • Phone Interview Questions
      • L3
        • Interview Questions
      • Phone Screen Questions
  • Miscellaneous
    • 90 Days Preparation Schedule
    • My Preparation for Tech Giants
    • Top Product Based Companies
  • Links
    • Github
    • LinkedIn
Powered by GitBook
On this page
  • Introduction
  • Functional requirements
  • Non Functional requirements
  • Architecture Diagram
  • List of Microservices
  • User Distribution
  • Envelope Calculations
  • Market Service
  • Order Service and Order Matching Service
  • Portfolio Service
  • Trade History Service and Trade Execution Service
  • Price Update Service
  • Real-time Data Processing Service
  • Total Servers
  • Component Design
  • References

Was this helpful?

Edit on GitHub
  1. System Design

Stock Broker System Design - Groww

PreviousUPI Payment System DesignNextDesigning Instagram's Collaborative Content Creation - Close Friends Only

Last updated 10 months ago

Was this helpful?

Introduction

lets first try to understand what stock broker is ?

Stock brokers are authorized members of the stock exchanges (i.e. BSE, NSE).

They execute trades on behalf of the customers at the exchange. There are 100's of registered stock brokers in India offering stock trading to retail customers. At high-level, the most famous stock brokers in India are categories in 2 types:

  1. Full-services brokers

  2. Discount Stock Brokers

Full-service brokers offer a wide range of services including stock trading, research and advisory and local support. Discount stock brokers are online brokers specialized only in stock and commodity trading services. They do not offer add-ons like research and advisory, PMS, wealth management and branch support.

The most popular stock brokers in the full-service category include ICICI Direct, Sharekhan, HDFC Securities, Sharekhan, Angel One and Axis Bank. The most famous stock brokers in discount broker category include Zerodha, 5paisa, Groww and upstox.

We can't read system design for all the brokers but lets consider one by more number of clients .

Stock Broker
Number of Clients ( In Millions )

Groww

9.19

Zerodha

7.22

Angel One

5.98

Upstox

2.4

Functional requirements

  • Price Changes are frequent in fractions of seconds.

  • Buy / Sell Order placement

  • Portfolio Management

  • Trade history & Reporting

  • Notification & alerts

  • Order Matching Engine

Non Functional requirements

  • High availability and reliability.

  • Low latency for real-time transactions.

  • Scalability to handle high volumes of trade requests.

  • Security to protect user data and transactions.

  • Compliance with financial regulations.

Architecture Diagram

List of Microservices

Considering the system like groww, it is good to have each microservices for single major functionality. There will be huge data flow in and out. Failures in one service do not necessarily impact others, improving system reliability. Having decoupling between the service dependencies, allows more flexibility and resilient architecture.

Below are the list of services required considering the above functionality. I am dividing the functionality into three categories. ( Core, Infra, Optional )

Core: These are the microservices specific to the grow as a company or product.

Microservice
Description

Market Service

Provides real-time market data to users

Order Service

Handles the creation, modification, and cancellation of buy/sell orders

Portfolio Service

Manages users’ portfolios and investments

Trade History Service

Stores and retrieves historical trade data

Trade Execution Service

Executes buy/sell orders by communicating with stock exchanges like NSE / BSE

Order Matching Engine Service

Matches buy and sell orders within the system. This is required for internal order processing and execution efficiency.

Price Update Service

Handles frequent price updates and broadcasts them to users

Real-time Data Processing Service

Processes real-time data streams for market data and order execution

Infra: These are the services required to run any kind of product. These services will be helpful when company / startup need for diversion from the existing path.

Microservice
Description

Identity Service

Manages user profiles, authentication, and authorization

Logging and Monitoring Service

Logs system activities and monitors system health

Payment Service

Manages financial transactions and settlements

API Gateway Service

Provides a unified entry point for client applications

Optional: These services are good to have for a system. Cover these services only if time permits in the interview.

Microservice
Description

Audit, Compliance Service

Ensures the system complies with regulatory requirements

Customer Support Service

Manages customer support requests and issues

User Preferences Service

Stores and manages user preferences and settings

Fraud Detection Service

Detects and prevents fraudulent activities

User Distribution

Let's assume different percentages of active users for each microservice, considering typical usage patterns.

  1. Order Service: 30% of active clients.

  2. Portfolio Service: 20% of active clients.

  3. Trade History Service: 10% of active clients.

  4. Trade Execution Service: 10% of active clients.

  5. Order Matching Engine: Tied to order service, hence 30% of active clients.

  6. Price Update Service: 10% of active clients, with updates for 5 companies each.

  7. Market Data Service: 100% of companies (7,800).

Envelope Calculations

As of March 2024, Companies registered are as follows

BSE: 5421

NSE: 2379

Total companies are around: 7800

Note: There can be companies listed in both. We are not considering this corner case.

Assumptions

Description
Value

Total Users ( Groww )

9.19 million

Registered Companies ( NSE and BSE )

7800

Trading hours in a day

9:15 AM to 3:30 PM (22,500 seconds)

Trading days in the month

22

API Call Average Request/Response Size

2 KB including headers

Average Request Processing Time

50 ms

Max Requests per Server

1000

Max Websockets per Server

20,000

Market Service

Assumptions

Description
Value

Company Updates Per second

5 updates per company per second

Data per Update

0.1 KB (100 bytes)

Total Companies

7,800

Websocket Update Required ?

No

Storage Calculation

Total Updates per Trading Day: 7800 * 22500 = 175,500,000 Updates / day

Data per Trading Day: 175,500,000 * 0.5 KB = 87.75 GB / day

Total data per month (Assuming 22 days of trading): 16.73 GB * 22 = 1930.5 GB / Month

Servers Calculation

Market Data Ingestion

Total Updates per Second: 7,800 companies * 5 updates/sec = 39,000 updates/sec

Number of API Servers: 39,000 / 1000 = 39 servers


Order Service and Order Matching Service

Assumptions

Description
Value

Orders per User per Day

2 orders

Data per Order

2 KB

Active Users

30% of 919,000 = 275,700

Storage Calculation

Total Orders per Trading Day: 9.19 m * 2 = 18.38 million orders per day

Data per Trading Day: 18.38 * 2 = 36.76 GB / day

Total Data per month: 18.38 * 22 = 808.72 GB / month

Server Calculation

API Requests per second = 275,700 / 0.05 = 5,514,000 requests/sec

Number of API Servers = 5,514,000 / 1000 = 5514 servers

Number of Websocket Servers = 275,700 ​/ 20,000 = 13.785 = 14 ( Approx)


Portfolio Service

Assumptions

Description
Value

Updates per User per Day

5

Data per update

1 KB

Active Users

20% of 919,000 = 183,800

Storage Calculation

Total portfolio updates per Trading Day: 9.19 m * 5= 45.94 million updates / day

Data per Trading Day: 45.95 m * 1 KB = 45.95 GB / day

Total data per month (Assuming 22 days of trading): 45.95 GB * 22 = 1,010.9 GB / Month

Server Calculation

API Requests per second = 183,800 / 0.05 = 3,676,000 requests/sec

Number of API Servers = 3,676,000 / 1000 = 3676 servers


Trade History Service and Trade Execution Service

Assumptions

Description
Value

Trades per User per Day

2

Data per Trade

2 KB

Active Users

10% of 919,000 = 91,900

Storage Calculations:

Total trades per Trading Day: 9.19 m * 2 = 18.38 million updates / day

Data per Trading Day: 18.38m * 2 KB = 36.76 GB / day

Total data per month (Assuming 22 days of trading): 36.76 GB * 22 = 808.72 GB / Month

Server Calculations:

API Requests per second = 91,900 / 0.05 = 1,838,000 requests/sec

Number of API Servers = 1,838,000 / 1000 = 1838 servers

Number Of Websocket Servers = 1,838,000 / 20,000 = 91.9 = 92 Servers


Price Update Service

Assumptions

Description
Value

Price Updates per second

1000

Data per Update

0.1 KB

Number of price updates per user

5

Number of active Users

10% of 919,000 = 91,900

Storage Calculations

Total updates per Trading Day: 1000 * 22,500 = 22,500,000 updates / day

Data per Trading Day: 22,500,000 * 0.1 KB = 22,500 KB / day = 2.25 GB / day

Total data per month (Assuming 22 days of trading): 2.25 GB * 22 = 49.5 GB / Month

Server Calculations

Total Price Updates: 91,900 * 5 = 459,500 updates/sec

API Requests per second = 459,500 / 0.05 = 9,190,000 requests/sec

Number of API Servers = 9,190,000 / 1000 = 9190 servers

Number of Websocker servers = 9,190,000 / 20,000 = 459.5 = 460 ( Approx)

Real-time Data Processing Service

Storage Calculations

Market Data Updates: 39,000 updates/sec

Updates Stored per Second: 1% of 39,000 updates/sec = 390 updates/sec

Total Data Size per Second: 390 * 0.01 = 3.9 KB/sec

Total Data Size per Day: 3.90 KB/sec * 22,500 = 87.75 MB /day

Total Data Size per Month: 87.75 * 22 = 1.93 GB /month

Server Calculations

  • Market Data Ingestion:

    • Active Companies: 7,800

    • Market Data Updates: Assume 5 updates per second per company

    • Total Updates per Second: 7800 * 5 = 39,000 updates/sec

  • Data Aggregation and Transformation:

    • Assume each update requires 1 additional processing step

    • Total Processing Steps per Second: 39,000

  • Event Detection:

    • Assume 10% of updates trigger events

    • Event Detection Steps per Second: 0.1 * 39,000 = 3,900

  • Streaming Updates:

    • Assume each update is sent to 30% of active users (30% of 919,000)

    • Active Users Receiving Updates: 0.3 * 919,000 = 275,700

    • Updates per User per Second: 2 (assume each user receives updates for 2 companies)

    • Total Streaming Updates per Second: 275,700 * 2 = 551,400 updates/sec

  • Data Storage:

    • Assume 1% of updates are stored for historical analysis

    • Data Storage Steps per Second: 0.01 * 39,000 = 390

Total Updates:

39,000 + 39,000 + 3,900 + 390 = 82,290 updates/sec

Number of API servers: 82,290 / 1000 = 82.2 = 83 ( Approx )

Number of Websocket Servers: 551,400 / 20, 000 = 28 Servers

Total Servers

Total Storage Required = 6227.71 GB / Month

Total API servers: 27, 692 per second

Total Websocket servers: 608 per second

Component Design

References

https://www.chittorgarh.com/report/top_20_share_brokers_in_india_by_clients_at_nse/1/
https://www.statista.com/statistics/1056499/india-leading-equity-brokerage-firms-by-active-customers/
https://www.nseindia.com/regulations/listing-compliance/nse-market-capitalisation-all-companies
https://blog.shoonya.com/how-many-companies-are-listed-in-nse-and-bse/#:~:text=There%20are%20a%20total%20of%205%2C309%20listed%20companies,DVRs%2C%20REITs%2C%20InVITs%2C%20ETFs%2C%20and%20Partly%20Paid%20Shares.
https://cloud.google.com/customers/groww#:~:text=Groww%3A%20Empowering%20investors%20with%20a,platform%20powered%20by%20Google%20Clou
https://stackshare.io/groww/groww
https://tech.groww.in/
📈
Envelope Calculation
Page cover image
Groww System Design
Stock Broker System design
Groww System design components