---
# System prepended metadata

title: Design Twitter.

---

# Design Twitter.
start: 9:55

Twitter is an online social networking service where users post and read short 140-character messages called "tweets." Registered users can post and read tweets

- communication de hieu nen focus on cai gi
- Lead cau chuyen, noi ve nen co nhung chuc nang gi, API nao => input, output => DB
- DB: noi ve table truoc, sau do moi chon loai DB nao => ly do
- ve Diagram, noi ra duoc nhung component chinh => ly do tai sao lai chon => communication giua cac service
- interviewer se pick 1 service va focus on.


**Thieu <- order cua tweets:
Nen dan dat theo user-case:
List up function: <- Tao con thieu gi khong, may co bo sung gi khong.
Scale cua he thong: User?**

Chuc nang chinh:

2 chuc nang chinh:
- Luu tweet vao DB
- Doc tweet tu DB

Cau hoi:
- Co the doc dc tweets cua nhung ai
=> Doc dc twt tu nguoi minh da follow

=> co them chuc nang la
- Follow
- List up nhung nguoi follow
- Dang ki nguoi dung => ko quan tam phan nay



tweeting:
- API tweet: cho phep nguoi dung tao tweet toi da 140 characters va luu vao DB voi userId, tamstamp, contents
- API read tweet: 
 list up followers => get followers id 
 => list ra all tweets dc viet boi followers & myseflt => order by timestamp

- API Follow
 luu userId cua minh va following vao table (follow )
- API list up followers
 doc tu bang follow ra voi userid la your id
 
** API: input / output**
 


Thiet ke DB:

**DB nay thi read nhieu, hay write nhieu? consistency cua no? 
**
- users: userId
- tweet: userId, content, timestamp
- follow: userId, followingId
- tweet_for_user <- cached

SQL: Cache: Redis

zset(List[])
key: <- block_memory

key int : +1
List: append(elements)

Tweets? 


select followingId from follow where userId = $1 // your id

program => get array following ids

array.push(yourId)

select * from tweet where userId in ({array})


**Chon DB:**
  PostgresSQL
  - truy xuat nguoc dc voi list follow
  - toc do doc nhanh (not sure)
  - familiar


1M users follow:
 - danh index
 - setup master slave => tang server slave
 
 # high level design: 
 
 Diagram:
     Microservice: function, comnication : sync/async/ (Rest_api , Message_queue)
     
     Main Service:
         Insert Tweet into DB
        
         Event: Insert tweet to MQ : user_id, tweet_id, timestamp
         
         1 : timestamp1
         2: > timestamp2
         
        MQ: Async
            Kafka: 
        
    TimelineSevice:
        
        Event(user_id, tweet_id, timestamp)
        
        Get Follower of user: GRPC(user_id) > list of follower
    
        Insert Redis: New Tweet: 1M key insert
        
    
    Follower service:
        List follower of user?
    
     

- front end:
   - consumer => auto load lai tweet moi
   - 140 characters limit
- backend:

  - store tweet
  - read tweet
  - follow user
  
  
  ca 3 functions vao 1 serve?
  LB => n nodes
  
  
  van de ton tai nguyen do nhu cau cua moi modules khac nhau + he thong la stateless => chon cach chia tung modules ra cac server khac nhau
  
  Q: multi AZ 
  A: => cloudfront
 
 => cau hoi order 
 
 Q: loat 100 newest tweet
 
 pure query: 
 
 select * from tweet where userId in (array) order by timestamp desc limit 100
 
 
 problem: 
 - where in => ko indexing?
 - scan all records

 => huong giai quyet:
 - tweet_for_user
 - moi lan tweet: luu lai userId, tweetId, timestamp
 => ko kha thi voi viec write
 
 
 
 
 
 
 
 
 
  
  
  
  
  
  
  
  
  
  





 
