Requirements:
- Buy and sell financial products via exchange and see their existing positions
- user should be able to see real time value of their asset and total asset
- minimise number of server listning to the exchange
NFR: single area high scalable high available low latency
Capacity Estimates:
- 100M total user
- 10 % of 100M i.e 10M daily active users for trade
- 1 user does 5 trade a day
- 50M trade per day(6 hours a day)= 2K write per sec
- 30% of 100M i.e (30M * 6*3600) = 6K read
- 100 different instruments(stock/options ) in portfolio
- maintain single active connection per client
maintain user connection list to fetch user session connection.
API design stockTicker - NSE/NYCE placeOrder(customerId, stockTicker/exchangeCode, type, quantity, placedAt) fetchStockDetail(String stockId) fetchCustomerPortfolio(customerId) setTrigger(customerId, exchangeCode, stockTicker, quantity, threshold, condition)
DB designs:
Customer: id, name
Stock:
id,
exchange-BSE/NSE,
ticker,
priceCustomerStockOrders-
orderId,
customerId,
stockId,
currentPrice,
buy/sell,
status(PLACED/FAILED/STATUS),
quantity,
placedAt
price
Triggers:
customerId,
stockId,
time,
quantity,
condition,
threshold,
price
CustomerPortfolio:
id
customerId,
List<CustomerStock>
StockPriceHistory{
stockId,
price,
timeInmiliSec
}“
Heigh throughput Exchange systems:
exchange keeps tracks of bids and ask ex:
Bids table:
| name | amount | unit |
|---|---|---|
| kitty | $100 | 5 |
| mity | $90 | 10 |
| Ask Table: |
| name | amount | quantity |
|---|---|---|
| citadel | $120 | 100 |
| melvin | $130 | 20 |
| bids will be success full when bidPrice>= askPrice |
Matching Machine Service: instead of TCP connection for publising latest pricing and order details, exchange uses UDP multicasting.