Market Exchange: 2. The Architecture

The exchange will be written in Go.
Other than SQL driver, I won’t be using any third party libraries, since Go has excellent http standard library, especially since this is a REST app.

This app will have two different types of request:

  1. Order request / response
  2. Live price update

Order

This request is straight forward. The bot will submit the request, and receive the response accordingly. HTTP request is absolutely perfect for this, since it is a transactional in nature and highly reliable.
However, there’s a catch. If the order has long expiry date (ie “Good til end of day”), there will be a timeout, and the number of open threads can increase exponentially on a busy trading day. That means, for that client will recieve a Webhook request with order update (if there is any).

Order information embedding

Since it’s a market exchange, information communicated between the client and exchange needs to be as small as possible, especially considering the low latency aspect of the exchange.
Here are the options I have considered:

  1. GET request with order information in URI
111XYZQ000000004200000001000000000175929186000000000001759291860000c74c6b06356b44e6b2b3a3a2b7f2d8b89e6b08ca0d114f8c8f97be2e87f699f7
  1. GET request with order information in URI byte encoded
31313158595A5130303030303030303432303030303030303130303030303030303031373539323931383630303030303030303030303137353932393138363030303063373463366230363335366234346536623262336133613262376632643862383965366230386361306431313466386338663937626532653837663639396637
  1. POST Request with JSON in request body
{"txType":1,"method":1,"ordType":1,"ticker":"XYZQ","qty:":42,"price":100,"ordDate":1759291860000,"ordUntil":1759291860000,"traderId":"c74c6b06356b44e6b2b3a3a2b7f2d8b8","ordId":"9e6b08ca0d114f8c8f97be2e87f699f7"}
  1. POST Request with []byte in request body
31313158595A5130303030303030303432303030303030303130303030303030303031373539323931383630303030303030303030303137353932393138363030303063373463366230363335366234346536623262336133613262376632643862383965366230386361306431313466386338663937626532653837663639396637

Given the order information, and using http2 with curl request.
Order information:
111XYZQ000000004200000001000000000175929186000000000001759291860000c74c6b06356b44e6b2b3a3a2b7f2d8b89e6b08ca0d114f8c8f97be2e87f699f7
Below are the order byte sizes:

  1. 301 bytes
  2. 432 bytes
  3. 435 bytes
  4. 230 bytes

Live price update for AI bots

This request is less straight forward. REST is not a good solution here, since price updates need to be live, and not transactional, one-time event. That leaves me with two options: WebSocket vs gRPC.

A case for Webhook

  1. Great for mixed client (browser and bot), which can be used for web ui as well.
  2. Low latency

Live price update for web UI

Here I will be using Webhooks, since there won’t be heavy subscriber load. The low latency is not a must.

Similar Posts