design a scalable social network: problems and solutions

29
Design a scalable social network Problems and solutions Châu Nguy„n Nh“t Thanh December 8, 2016 Head of Game Backend and Cloud - VNG Corp.

Upload: chau-thanh

Post on 16-Apr-2017

148 views

Category:

Internet


2 download

TRANSCRIPT

Page 1: Design a scalable social network: Problems and solutions

Design a scalable social network

Problems and solutions

Châu Nguyễn Nhật Thanh

December 8, 2016

Head of Game Backend and Cloud - VNG Corp.

Page 2: Design a scalable social network: Problems and solutions

Table of contents

1. Giới thiệu

2. Các module chính của MXH

3. Trang chủ

4. FriendList

5. Feed

6. Bonus

1

Page 3: Design a scalable social network: Problems and solutions

Giới thiệu

Page 4: Design a scalable social network: Problems and solutions

Về bản thân

Well-known as a:

• Technical Lead of ZingMe platform

• Technical Lead of Cyber Station Manager

• Co-Founder of VNG IoT LabGuest

Speaker at:

• PHPDay 2010, 2011, 2012

• Grokking Engineer

• Vietnam Web Submit

2

Page 5: Design a scalable social network: Problems and solutions

Các module chính của MXH

Page 6: Design a scalable social network: Problems and solutions

Trang chủ

Figure 1: Trang chủ facebook

3

Page 7: Design a scalable social network: Problems and solutions

Danh sách bạn bè

Figure 2: Facebook friend list

4

Page 8: Design a scalable social network: Problems and solutions

Photo

Figure 3: Facebook photo 5

Page 9: Design a scalable social network: Problems and solutions

Trang chủ

Page 10: Design a scalable social network: Problems and solutions

Thách thức

Cách làm thông thường:

• Web server kết nối đến

DB server

• Mỗi chức năng thể hiện

bằng 1 hoặc nhiều table

Với cách làm này:

• Latency cao do phải query vào nhiều bảng khác nhau

• Quá phức tạp khi thêm mới, nâng cấp tính năng

6

Page 11: Design a scalable social network: Problems and solutions

Giải pháp

Dùng cache để giảm

latency:

• Kết quả trả về từ

DB sẽ được lưu vào

cache

• Lần tiếp theo sẽ

truy cập vào cacheFigure 4: Numbers Everyone should

know,Jeff Dean, Google

7

Page 12: Design a scalable social network: Problems and solutions

Giải pháp

Để giảm độ phức tạp và dễ

dàng nâng cấp

• Tách thành những nhóm

nghiệp vụ nhỏ (service)

• Mỗi nhóm nghiệp vụ là 1

hệ thống độc lâp có

interface vào ra

• Các nhóm nghiệp vụ

tương tác nhau qua

interface này Figure 5: divide and conquer

8

Page 13: Design a scalable social network: Problems and solutions

Giải pháp

Figure 6: SNS components 9

Page 14: Design a scalable social network: Problems and solutions

FriendList

Page 15: Design a scalable social network: Problems and solutions

Thách thức

• Mỗi user có danh sách n bạn (fb=unlimited,zme=500,5K)

• Mỗi user có danh sách những người follow mình

• Thách thức

1. Kiểm tra xem 1 user nào đó có phải là bạn hay không?

2. Kiểm tra 1 user nào đó có đang follow ai không ?

3. Hôm nay là sinh nhật ai trong danh sách bạn mình ?

10

Page 16: Design a scalable social network: Problems and solutions

Giải pháp

1. Kiểm tra xem 1 user nào đó có phải là bạn hay không?

• Mỗi user có danh sách bạn là 1 tập hợp

• Thao tác tìm 1 user có phải là bạn không tương đương

với việc kiểm tra 1 phần tử có thuộc tập hợp đó không

• Redis cmd: SADD userId friendId, SISMEMBER userId

friendId

• Tìm bạn chung SINTER userIdA, userIdB, userIdC

11

Page 17: Design a scalable social network: Problems and solutions

Giải pháp

2. Kiểm tra 1 user nào đó có đang follow ai không ?

• Mỗi user có danh sách bạn là 1 bitmap (chuỗi bit 2 mũ

32 phần tử)

• Thao tác tìm 1 user có phải là bạn không tương đương

với việc kiểm tra bit tại vị trí thứ userId có bật lên không

• Redis cmd: SETBIT userId friendId 1, GETBIT userId

friendId

• Có vấn đề gì không??? BitMagic

12

Page 18: Design a scalable social network: Problems and solutions

Giải pháp

Figure 7: Follower list

13

Page 19: Design a scalable social network: Problems and solutions

Giải pháp

3. Hôm nay là sinh nhật ai trong danh sách bạn mình ?

• Mỗi ngày thứ n trong năm là 1 bitmap với vị trí thứ i

trong bitmap là userId thứ i có ngày sinh là ngày thứ n

trong năm

• Thao tác tìm trong danh sách bạn bè có ngày sinh là

ngày thứ n trong năm bằng cách lấy danh sách bạn dạng

bitmap and với bitmap ngày thứ n ở trên và lấy ra tập

các vị trí bằng 1

14

Page 20: Design a scalable social network: Problems and solutions

Giải pháp

Figure 8: Sinh nhật bạn bè

15

Page 21: Design a scalable social network: Problems and solutions

Feed

Page 22: Design a scalable social network: Problems and solutions

Thách thức

• Mỗi user có danh sách n bạn (fb=unlimited,zme=500,5K)

• Khi user thực hiện 1 hành động thì bạn bè của người đó

sẽ thấy hoạt động đó

• Thách thức

1. Kiến trúc như thế nào để đảm bảo latency?

2. Lưu trữ như thế nào để tiết kiệm storage mà vẫn đảm

bảo tốc độ truy cập?

16

Page 23: Design a scalable social network: Problems and solutions

Giải pháp

• Feed sẽ được lưu

trữ thành feedItem

và feedIdList

• Mỗi user có 1 danh

sách lưu trữ các

feedId tương ứng

với các action

(photo,blog...)

• Có 2 phương pháp

PUSH và PULL Figure 9: Chiến lược hiện thực hệ

thống news feed

17

Page 24: Design a scalable social network: Problems and solutions

Giải pháp

Figure 10: http://www.slideshare.net/mongodb/socialite-the-

open-source-status-feed18

Page 25: Design a scalable social network: Problems and solutions

Giải pháp

Figure 11: http://www.slideshare.net/mongodb/socialite-the-

open-source-status-feed19

Page 26: Design a scalable social network: Problems and solutions

Bonus

Page 27: Design a scalable social network: Problems and solutions

http://swagger.io/swagger-editor/

Figure 12: http://www.slideshare.net/pvullioud/swagger-

45508232?smtNoRedir=120

Page 28: Design a scalable social network: Problems and solutions

Hỏi đáp

21

Page 29: Design a scalable social network: Problems and solutions

The end

22