sdi 3 Flashcards

1
Q

fb messenger - the avg msg is how large?

A

100 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

messenger high level design

A

we will need a chat server that will be the central piece. When a user wants to send a message to another user, they will connect
to the chat server and send the message to the server; the server then passes that message to the other user and also stores it in the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

pull model

A

Users periodically ask the server if there are any new messages for them. Server keeps track of undelivered msgs; when user connects to the server, server returns all the pending msgs. To minimize latency, users have to check the server quite frequently, and a lot of the time they’ll be getting an empty response. This will waste a lot of resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

push model

A

Users can keep a connection open with the server and can depend upon the server to notify them whenever there are new messages. The server does not need to keep track of the pending messages, and we will have minimum latency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can the central chat server keep track of all opened connections to redirect messages to users efficiently?

A

The server can maintain a hash table, where “key” would be the UserID and “value” would
be the connection object. So whenever the server receives a message for a user, it looks up that user in the ht.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will happen when the central chat server receives a message for a user who has gone offline?

A

If the
receiver has disconnected, the server can notify the sender about the delivery failure. If it is a
temporary disconnect, e.g., the receiver’s long-poll request just timed out, we can ask the sender to retry sending. This retry
could be embedded in the client’s logic so that users don’t have to retype the message. The server can also store the message for a while and retry sending it once the receiver reconnects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How should the central chat server process a ‘deliver message’ request?

A

upon receiving a new message:
1) Store msg in db
2) Send msg to receiver
3) Send acknowledgment to sender

The chat server will first find the server that holds the connection for the receiver and pass the message
to that server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we maintain sequencing of the messages?

A

Storing timestamps won’t ensure correct ordering of messages for clients.
we need to keep a sequence number for every message for each client. This number will determine the ordering of messages for EACH user. Both clients will see a different view of the message sequence, but this view will be consistent for them on all devices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

messenger - what storage system CAN’T we use?

A

We cannot use RDBMS like MySQL or NoSQL like MongoDB because we cannot afford to read/write
a row from the database every time a user receives/sends a message. This will not only make the basic
operations of our service run with high latency, but also create a huge load on databases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

messenger - what storage system should we use?

A
  • must support very high rate of
    small updates and also fetch a range of msgs quickly
  • solution: wide-column database solution like HBase. HBase is a column-oriented key-value NoSQL db.
  • HBase groups data together to store new data in a memory buffer and, once the buffer is full, it dumps the data to the disk.
  • HBase is also an efficient database to store variably sized data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

pagination

A

Clients should paginate while fetching
data from the server. Page size could be different for different clients, e.g., cell phones have smaller
screens, so we need a fewer number of message/conversations in the viewport.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

messenger data partitioning

A

Partitioning based on MessageID: If we store different msgs of a user on separate shards, fetching a range of msgs would be very slow, so bad idea.

Partitioning based on UserID: find shard number by “hash(UserID) % (num of shards)” and then store/retrieve the data from there. very quick to fetch chat history

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

messenger caching

A

We can cache a few recent messages (say last 15) in a few recent conversations that are visible in a
user’s viewport (say last 5). Since we decided to store all of the user’s messages on one shard, cache for
a user should entirely reside on one machine too.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will happen when a chat server fails?

A

It’s extremely hard to failover TCP connections to other servers; an easier approach can be to
have clients automatically reconnect if the connection is lost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Should we store multiple copies of user messages?

A

We cannot have only one copy of the user’s data, because if the server holding the data crashes or is down permanently, we can’t recover that data. Either we have to store multiple copies of the data on different servers or use techniques like Reed-Solomon encoding to distribute and replicate it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to implement group chat?

A

We can have separate group-chat objects in our system that can be stored on the chat servers. A group-chat object is identified by GroupChatID and will maintain list of users in the chat.
LB can direct each group chat message based on GroupChatID and the server handling that group chat can iterate through all the users of the chat to find the server handling the connection of each user to deliver the message.
In databases, we can store all the group chats in a separate table partitioned based on GroupChatID.

17
Q

how to implement push notifs?

A

Push notifications will enable our system to send messages to
offline users.
For Push notifs, each user can opt-in from their device (or a web browser). Each manufacturer maintains a set of servers that handles pushing these notifs to the user.
We need to set up a Notification server, which will take msgs for offline users and send them to the manufacturer’s push notification server, which will then send them to the user’s device.

18
Q

tweet api

A

tweet(api_dev_key, tweet_data, tweet_location, user_location, media_ids) (media ids are pics, vids, etc)

A successful post will return the URL to access that tweet. Otherwise, an appropriate HTTP error is returned.

19
Q

twitter high level design

A
  • multiple application servers to serve all r/w requests (mostly r) with LBs in front of them for traffic distributions.
  • efficient db that can store all new tweets and support a huge number of reads
  • file storage for pics and vids
20
Q

twitter db schema

A

4 DBs: users, their tweets, their favorite tweets, and people they follow.

remember tweet should include num favorites, retweets, etc

21
Q

twitter Sharding based on UserID:

A
  1. Too many queries on servers holding hot users.
  2. Over time some users can end up storing a lot of tweets or having a lot of follows compared to
    others. Maintaining a uniform distribution of growing user data is quite difficult.
22
Q

Sharding based on TweetID:

A

Our hash function will map each TweetID to a random server. To search for tweets (such as for timeline generation), we have to query all servers. A centralized server will aggregate these results.

This approach solves the problem of hot users, but we have to query
all database partitions to find tweets of a user, which can result in higher latencies.

23
Q

Sharding based on Tweet creation time:

A

advantage of fetching all the top tweets quickly and we only have to query a very small set of servers.
BUT traffic load distribution will be very uneven - while writing, all new tweets will be going to one server and the other servers will be idle. Similarly, while reading, the server
holding the newest tweets will have a very high load compared to servers holding older tweets.

24
Q

twitter ideal way to shard data

A

generate universally unique TweetIDs consisting of current epoch time + auto incrementing sequence. get shard number from this TweetID.
- We can reset our auto incrementing sequence every second. For fault tolerance and better performance, we can have 2 db servers to generate auto-incrementing keys for us, 1 for even keys and 1 for odd.
- If TweetID is 64bits (8 bytes) long, we can easily store tweets for the next 100 years w/ ms granularity.

25
Q

pros/cons of using 2-part TweetID to shard

A

we still have to query all the servers for timeline generation, but r/w will be substantially quicker.
1. Since we don’t have any secondary index (on creation time) this will reduce our write latency.
2. While reading, we don’t need to filter on creation-time as our primary key has epoch time included in it.

26
Q

twitter - fault tolerance

A

Since our system is read-heavy, we can have multiple secondary database servers for each DB partition. Secondary servers will be used for read traffic only. All writes will first go to the primary server and then will be replicated to secondary servers. This scheme will also give us fault tolerance, since whenever the primary server goes down we can failover to a secondary server.

27
Q

twitter - 2 ways of caching

A

We can use an off-the-shelf solution like Memcache that can store the whole tweet objects.

  1. cache 20% of daily read volume from each shard
  2. Let’s say 80% of users see tweets from the past 3 days only. Even if this cache data can easily fit into 1 server, we should replicate it onto multiple servers to distribute read traffic. Whenever we generate a user’s timeline, we can ask the cache servers if they have all the recent tweets for that user. If yes, we can return; else, we query the backend server.
    Our cache is a HT - ‘key’ is ‘OwnerID’; ‘value’ is a doubly LL w/ all tweets from that user from past 3 days. Insert new tweets at head; remove old tweets from tail.