[Case Simulation Workshop] Michael Evans - High performance system building

34
Michael Basic: High performance system building contact: [email protected]

Transcript of [Case Simulation Workshop] Michael Evans - High performance system building

Page 1: [Case Simulation Workshop] Michael Evans - High performance system building

Michael

Basic: High performance system building

contact: [email protected]

Page 2: [Case Simulation Workshop] Michael Evans - High performance system building

Introduction❖ Graduated: Ha Noi University of Science and

Technology - Software Engineer - 2010

❖ Got No.3, Viet Name Talent in IT field - 2011.

❖ from 2007 - 2013: work at Med-Aid as Team Leader, a partner of Prowess.

❖ From 2013 - Now: working at Cinnamon as Software Architecture Leader.

Page 3: [Case Simulation Workshop] Michael Evans - High performance system building

Main points researching❖ High performance system building

❖ Data mining

❖ Artificial Intelligent

Page 4: [Case Simulation Workshop] Michael Evans - High performance system building

How to build high performance system?

Page 5: [Case Simulation Workshop] Michael Evans - High performance system building

Normal system architecture

database

System logic

Server side

Third party

system

Client side

Problem come from here!!!

Page 6: [Case Simulation Workshop] Michael Evans - High performance system building

Real use case❖ Social network

❖ one user have: followers, followings, posts.

❖ …

Page 7: [Case Simulation Workshop] Michael Evans - High performance system building

Server side - Database

Page 8: [Case Simulation Workshop] Michael Evans - High performance system building

Database issues?

Queries So slow

- Multiple queries in one request

- Each query take time to run System run slow

Page 9: [Case Simulation Workshop] Michael Evans - High performance system building

Database solution?

Queries So slow

❖ Set Index for all important fields

❖ Optimise number of queries on each request / task

❖ Anything else?

Page 10: [Case Simulation Workshop] Michael Evans - High performance system building

Caching solution

❖ always check if needed data exist on cache or not

❖ if exist —> get from cache.

❖ if not —> query from DB and add to cache too.

Logic

CacheExist?

Query if not exist in cache

Update cache for each query

Page 11: [Case Simulation Workshop] Michael Evans - High performance system building

Caching solution❖ Redis cache: https://redis.io

❖ Memcache: https://memcached.org

❖ …

Page 12: [Case Simulation Workshop] Michael Evans - High performance system building

Example

❖ If User 1111 create new post which has ID = cccc —> system need update that post id for all his following users.

❖ How will you do?

UserId PostId2222 aaaa2222 bbbb3333 aaaa

Feeds table

UserId FollowingID1111 22221111 33331111 4444

Following table

Page 13: [Case Simulation Workshop] Michael Evans - High performance system building

Normal way❖ Step 1: Get all following user’s ids from 1111. (0.2s)

❖ Step 2: For each following users:

❖ Step 2.1: Insert new record on Feeds table for that user. (0.2s)

❖ If 1111 have 50 following —> system will take: 0.2 + 50*0.2 = 10.2s !!!! —> Too slow!!!

Page 14: [Case Simulation Workshop] Michael Evans - High performance system building

Improved way❖ Step 1: Get all following user’s ids from 1111. (0.2s)

❖ Step 2: For each following users:

❖ Step 2.1: Create feed’s record for each user. (so small time)

❖ Step 3: Insert all records in one command. (0.3s)

❖ If 1111 have 50 following —> system will take: 0.2 + 0.3 = 0.5s !!!! —>Much faster!!!

Page 15: [Case Simulation Workshop] Michael Evans - High performance system building

Apply cache?❖ Step 1: Get all following user’s ids from cache. (0.002s)

❖ Step 2: For each following users:

❖ Step 2.1: Create feed’s record for each user. (so small time)

❖ Step 3: Insert all records in one command. (0.3s)

❖ Step 4: Insert all records to cache. (0.003s)

❖ If 1111 have 50 following —> system will take: 0.002 + 0.3 + 0.003 = 0.305s !!!! —>Much faster!!!

Page 16: [Case Simulation Workshop] Michael Evans - High performance system building

Server side - Logic

Page 17: [Case Simulation Workshop] Michael Evans - High performance system building

Server side - Logic❖ POST / PUT/ DELETE

❖ GET

Third party

Data databaseLogic

SLOW

Third party

DataLogic

SLOW

database

Third party

Page 18: [Case Simulation Workshop] Michael Evans - High performance system building

POST/PUT/DELETE issues?

Tasks need to do

Database

Third Party

Request

Response time > 2s

Too much tasks need to do

Page 19: [Case Simulation Workshop] Michael Evans - High performance system building

POST/PUT/DELETE solution?Database

Third Party

Request

Response time < 500 ms

Realtime

Background

Page 20: [Case Simulation Workshop] Michael Evans - High performance system building

How background work?❖ It should be use message queue system.

❖ some message queue system: RabbitMQ, ZeroMQ, Kafka, Redis PubSub…

Main server

Message queue

Background server 1

Background server 2

Page 21: [Case Simulation Workshop] Michael Evans - High performance system building

Messaging queue technology❖ PubSub pattern (AMQP, MQTT…)

Page 22: [Case Simulation Workshop] Michael Evans - High performance system building

Can we make Realtime task run faster?

Page 23: [Case Simulation Workshop] Michael Evans - High performance system building

Caching for realtime task

Task 1

Access DB

Task 2

Update DB

….

OLD way

Task 1

Access Cache

Task 2

Update Cache

….

NEW way

Task n Task n

Page 24: [Case Simulation Workshop] Michael Evans - High performance system building

Example❖ when new post created by user 1111 —> push

notification for all following users about that.

❖ How will you do?

Page 25: [Case Simulation Workshop] Michael Evans - High performance system building

Normal way❖ User create new post

❖ Get all following user ids.

❖ for each following user:

❖ Insert new record to Feed table

❖ call push notification method to send push notification.

❖ Return result.

So take time here

Page 26: [Case Simulation Workshop] Michael Evans - High performance system building

Improved way❖ User create new post

❖ Get all following user ids.

❖ Insert all records in one command into Feed table

❖ For each following users:

❖ Add one task to message queue to run in background.

❖ Return result.

So Fast

Page 27: [Case Simulation Workshop] Michael Evans - High performance system building

GET issues?

Data queries

Database

Third Party

Request

Response time > 2s

Too much thing need to format

Format response data

Too slow

Page 28: [Case Simulation Workshop] Michael Evans - High performance system building

Solution

Data queries

Database

Third Party

Request

Response time <500ms

Format response data

Cache

Page 29: [Case Simulation Workshop] Michael Evans - High performance system building

How to make cache data optimise for GET apis?

API get list of

following users

Get following Ids

Format following

objects

Following cache

Following formatted

cache

Always have cache object for any data we need for our apis.

Page 30: [Case Simulation Workshop] Michael Evans - High performance system building

How to apply them to your project / company?

Page 31: [Case Simulation Workshop] Michael Evans - High performance system building

Modulation

Modulation

Libs

Micro services

Page 32: [Case Simulation Workshop] Michael Evans - High performance system building

Libs

Cache

DB Access

Cache Access

Repository Base

Your Repository Here

Page 33: [Case Simulation Workshop] Michael Evans - High performance system building

Micro-service

Main logic

Message queue

Background server

Background server

….