Skip to the content.

Stock Exchange

Requirements and User stories

Functional requirements

Non functional requirements

Calculation

Reference

Data model

type Account struct {
	AccountID string
	Balance float32 // what is the available balance
	// contains account related information, like payment methods, external bank account info, etc.
}
type Order struct {
	AccountID string  // record who placed the order
	OrderID   string  // auto-generated id to record the order
	PlaceTime string
	Symbol    string  // APPL
	Type      string  // buy or sell
	Price     float32
	Quantity  int
	Expires   string  // if the order is not executed, when it expires
	Status    string  // record the order status: acknowledged, filled, rejected, canceled
	...
}
type OrderHistory struct {
	AccountID string
	Orders []Order
}
// This could be used to show the holdings of an account
type Position struct {
	AccountID string
	Symbol string
	Quantity int
	CostBasisPerShare float32
}

APIs

Client side RESTful API

Architecture

architecture

Match engine

More details on match engine could be found from this blog and this blog.

match-engine-overview

This kind of sorting mechanism is to make sure there are high possibilities order could match.

How to build an order book

Heap(PriorityQueue)

DDL and Binary Search Tree

Above diagram is based on this article.

Message Bus vs RPC for delivering order between services

https://doc.akka.io/docs/akka-grpc/current/whygrpc.html#grpc-vs-message-bus

gRPC vs Message Bus
- While built on an efficient non-blocking implementation, gRPC is still ‘synchronous’ in the sense that it requires both
‘sides’ of the communication to be available at the same time. When using a (persistent) message bus, only the producer
and the bus must be up, the consumer does not need to be available, leading to a higher degree of decoupling.
- While gRPC supports bidirectional streaming for each request, when using a message bus the streams are decoupled.

More articles to read:

How to guarantee latency

How order status gets updated on execution

Failure handling

Match engine(in-memory data) failure

TBA

Service failure

Scaling

References