Slide 1

Slide 1 text

How to rebuild a websocket service by golang and redis LY @MaiCoin 2020.08.25

Slide 2

Slide 2 text

BACKGROUND

Slide 3

Slide 3 text

CRYPTOCURRENCY EXCHANGE

Slide 4

Slide 4 text

API EVERYWHERE

Slide 5

Slide 5 text

API EVERYWHERE /depth

Slide 6

Slide 6 text

API EVERYWHERE /markets

Slide 7

Slide 7 text

API EVERYWHERE /kline

Slide 8

Slide 8 text

API EVERYWHERE /public_trades /private_orders

Slide 9

Slide 9 text

Websocket v1 • Rails em-websocket • RabbitMQ • Ticker • Push when data changed • Order Book Push order book operation • Private channel • Account only

Slide 10

Slide 10 text

Websocket v2 • Golang gorilla/websocket • Deploy with Rails • Ticker • Push when data changed Push data every 5 seconds • Order Book • Not production ready • Private channel • Not implement

Slide 11

Slide 11 text

PAIN POINTS

Slide 12

Slide 12 text

Too Many Requests Polling Every 3 seconds 6 requests 120 requests/minutes

Slide 13

Slide 13 text

Slow Response Polling 1.5 seconds CDN issues

Slide 14

Slide 14 text

Low priority New Features Bug fix Operations

Slide 15

Slide 15 text

ARCHITECTURE

Slide 16

Slide 16 text

At the Beginning

Slide 17

Slide 17 text

Final RabbitMQ Worker Redis WebSocket Server WebSocket Client

Slide 18

Slide 18 text

THE RIGHT WAY

Slide 19

Slide 19 text

Good Infra Kubernetes Helm chart Jenkins Gitflowd Grafana Kibana Machine Management CI/CD Monitor/Log

Slide 20

Slide 20 text

Reuse good Part Message Queue Codebase

Slide 21

Slide 21 text

RABBITMQ https://codedestine.com/rabbitmq-headers-exchange/ Fanout Headers

Slide 22

Slide 22 text

REDIS PUBSUB https://medium.com/@saurabh.singh0829/redis-pub-sub-implementation-f3208e4625c7 Topic Keep server simple Reduce memory Improve scalability

Slide 23

Slide 23 text

PROBLEMS

Slide 24

Slide 24 text

JSON Format [1,”b”,[…,…],true] Readability Small size {“event”:”orderbook”,”bids”:} {“e”:”orderbook”,”b”:}

Slide 25

Slide 25 text

Authentication Ruby https://www.facebook.com/yhsiang/posts/10157307118952883 AES GCM Base64 Golang cipher text / encrypted_xx yDIBC ==\nxZyyc= nonce / encrypted_xx_iv gCkjsa ==\n

Slide 26

Slide 26 text

Scalability

Slide 27

Slide 27 text

REDIS https://rtfm.co.ua/en/redis-replication-part-1-overview-replication-vs-sharding-sentinel-vs-cluster-redis-topology/ Primary-replica Sentinel Cluster

Slide 28

Slide 28 text

Kubernetes Metric server Kubernetes v1.6 Kubernetes v1.5 Client-go HPA CPU

Slide 29

Slide 29 text

LOAD TESTING https://artillery.io/ Artillery Scenarios Ramp Up

Slide 30

Slide 30 text

Achievement

Slide 31

Slide 31 text

Reduce API Calls

Slide 32

Slide 32 text

Lower latency Order book update latency 3ms Market trade update latency 291ms

Slide 33

Slide 33 text

CONCLUSION

Slide 34

Slide 34 text

RECAP Good Infra Reuse Good Part Design First

Slide 35

Slide 35 text

WE ARE HIRING AMIS Blockchain/Backend Frontend MaiCoin Backend Frontend Site Reliability [email protected] Full Stack