user should send a new post.
user should read friends' posts
user should connect with each other
user should be able to thumb up and leave comment
user should be able to upload photo
user should be able to register and login
should be low latency,
should be resilient and fault-tolerant,
should support high-throughput traffic, should be scalability scale up
suppose the system is read heavy. write read ratio is 1:10, if we have 1million user online avg per second, and 1% of them write new post, the write qps is 10k/s, and suppose 1 user has 10 friends online and the read qps is 100k/s. suppose one host can have 1k connection, for write operation we need at least 10 hosts, for read we need 100 hosts,
suppose for each write operation, 1MB avg including photo or video
for one day it will cost storage 864TB
a. API for new user register account public boolean register(User user information) throws UserAlreadyExistsException
b. API for user to login public boolean login(username, password) throws unathenticatedException
c.API for user to post a new tweet public int postTweets(uuid userId, Tweet tweet);
d.API for user to delete an existing tweet public boolean deleteTweets(uuid userid, uuid tid);
e.API for user to read friends' tweet public List
f API for user to connect a new friend public boolean connect(uuid user1, uuid user2)
g API for user to disconnect an existing friend public boolean disconnect(uuid user1, uuid user2)
user table{
uuid userid(PK)
string username
string password
Set
string email
string phonenumber
string zipcode
long registeredTime
}
twitter table {
uuid tweetsid (PK)
uuid userid (FK)
string context
uuid photoid(FK)
uuid videoid(FK)
string comment,
long createdTime
int forwardCount,
}
file table {
uuid filed(PK)
byte[] content
long uploadTime
}
for database, we can use nosql db like MongoDB for better performance, and easier scale up. we can make a db cluster, make one db instance deal with write request, new tweets, new friends, and other db replica read instance. we can also add index for those high freq columns and we can also do sharding to scale wide our db, split db into multi subset.
for userid, tweets id we should use uuid to generate, uuid is globally unique, it is generated by timestamp and mac address, it is safe that unpredictable, it make sure it is unique in the distributed system.
when user login, we will generate jwt bearer token, and send back to user, and everytime user request our server, it will also add the bearer token and our API gateway will check the expirationdate of the token and if it expired we will send 401 responds and let user login again, if the token has an unexpired refresh token, then we will generate a new bearer token and send back to user and still let user pass.
our batch will calculate the high popular tweets on hourly basis and will put the high freq tweets into the cache, to calculatepopular tweets, we can use data structure like priorityqueue to prioritize tweets, we will evaluate multiple dimension like forwarded count, thumbs up count, reply count, etc, when user post tweets with large files like photo, video, our batch will also upload video, photos large files into s3, user don't have to wait until the batch job finish, also our batch job will run daily to clean up the deleted tweets, deleted friends.
adding caching we can reduce the read latency for popular tweets, but we also need to maintain the caching using batch jobs will cost overhead.
choosing nosql will boost the performance, but it could e more complex to run complex query and or aggregation function.
for real time message sending, friend request, we choose websocket, but websocket is expensive which cost more.
what if user delete a popular tweets, then it still remain in the caching, which will cause data inconsistency. issue, and our hourly batch won't delete it from cache until 1 hour time up
if multiple user comment on one tweets at same time, then there could e data inconsistency issue, some user don[t see other people comment,
we can improve the batch jobs, for example, make the batch job more frequency, when user delete a tweets, check both caching and db, so that to make sure the data consistency.
for multiple user comment on 1 tweets issue, we can use websocket to update the comment in real time so that user can see other people's new comment