Communication Service

5 minute read

This section covers proposed solutions for problems in designing communication services, i.e. chat, voice call, etc.

Message Chat service

  1. What are service requirements?
    • High consistency: for chat service, users should see updated messages on all client devices
    • Low latency
  2. What is the workflow of chat service?
     send (user_id, messages) 
     recieve (user_id, messages)
     Return: None
    • Logical flow: User A makes send API requests to application server which will send the acknowledgment to User A and store the messages in database, at the same time, It will push (or being pulled) the messages to User B who is chatting with User A. The User B will send the acknowledgment to the server
  3. What does client receive messages in chat service?
    • Client can periodically poll the chat server to check for new service messages.
    • More effective method is push model where messages are pushed from chat server to clients. We need to maintain persistent connection. Also, we can use hash table to map between user Id to connection.
  4. How do we implement push model with persistent connection?
    • HTTP polling
    • HTTP Long Polling: HTTP long polling is protocol where server holds the request from client and waits until some data becomes available. The client only sends a new long-poll request after either receiving a response or polling time-out expires.
    • HTTP persistent connection (HTTP keepalive): a single TCP connection for client send and receive multiple HTTP requests/responses. The session is closed when client is done requesting all data.
    • Server-sent event/ http streaming: Server-Sent Events utilize a regular HTTP octet streams, and therefore are limited to the browser’s connection pool limit of average 6 concurrent HTTP connections per domain in HTTP/1.1 (a workaround is to use domain sharding but can introduce performance losses; web browsers have to perform a DNS lookup on each additional domain and maintain connections to each domain, resulting in slower initial load times)
    • WebSockets: is a protocol provides a persistent connection between a client and a server that both parties can use to push data at any time. Websocket is upgraded using upgrade header after tcp handshake
  5. How to reduce load of chat server?
    • A modern server can handle about 65k connections. In order to handle high loads, we need to distribute connections to multiple servers, i.e. using hashing. If users from different servers are sending message to each other, then we need another message+ ACk from server 1 to server 2
  6. How does the messenger maintain the sequencing of the messages?
    • We can keep sequence for each user to ensure the sequence is consistent for each user on every devices
    • If a server goes down, we will have clients automatically reconnect if the connection is lost.
  7. What is storage to store messages?
    • We can use either Bigtable or Hbase as they support a very high rate of small messages updates and fetch a range of records quickly.
    • HBase is a column-oriented key-value NoSQL database that can store multiple values against one key into multiple columns. 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
  8. How does service keep track of status (online or offline) of users?
    • When a client starts the chat service, it can pull the status of the friends
    • When a user sends a message to another user that has gone offline, we can send a failure to the sender and update the status on the client. The client can either periodically poll to get the status or Whenever a user comes back online, the server can broadcast that status
  9. How to implement spelling correction module?
    • The misspelled word’s Levenshtein distance from the intended word tends to be very small (one or two edits). Hence, if we keep a hash table for all the words in the dictionary and look for all the words that have a Levenshtein distance of 2 from the text, it is likely that the intended word will be found in this set. However, it does not take into account the commonly misspelled words or the proximity of letters on a keyboard.
  10. How do we support group chat?
    • We can simple store the mapping between group Id to the list of User Ids in each group chat

Video call service

  1. What is the workflow of 1-to-1 video chat service?
    • Clients connects to the WebSockets server using a WebSocket connection and starts the process to send the video data to the server. The WebSocket server implements session management, it receives the messages sent by one client peer over one active WebSocket, and delivers them to its connected client peers over the corresponding WebSockets. The server should have a way to map between group Ids to the list of connections
  2. What is the workflow of group video chat service?
    • A new user registere a new chat session and establish Websocket connection with the WebSockets server. Other users connects via the URL of the application (if it is web-based) and enters the group name of this chat, the appropriate JavaScript connects their browsers with the WebSockets server and transfers the given group name to the service logic.
  3. What is the workflow of Live Video Streaming?
    • If the user enters a group name that has not been registered previously with the web server, the server assumes it is a new streaming event and registers the user as “streamer”, the streamer should also specify the starting time for the stream. When the streamer submits this info to the server, a WebSocket connection to the WebSockets server is established. Along with this standard signaling, the group name of the event is transferred with a message via the WebSocket to the service logic running on top of the WebSockets server. The service logic registers the specific WebSocket as a streamer WebSocket and associates it with the received group name. Whenever a client peer connects to the URL of the application and enters the group name of this multicast, the JavaScript code for the receivers runs at his browser and he is being connected to the WebSockets server via a WebSocket.?
  4. How client can stream video data from his web-camera and receive video data from a remote camera in real time?
    • An HTML5 clients requests a web-page from a web server that includes the required JavaScript for media access and streaming over WebSockets. The camera captures and image data are persistently displayed on a canvas.
  5. How do we implement web-based communication?
    • The application is available via a URL common for both streamers and receivers. Any available stream can be either a broadcast or a multicast event and identified using “group name”. Broadcast events are available to anyone; hence their group names are propagated by the web server to anyone interested to follow them, while multicast events are close ones restricted to those knowing the associated group names. A client is connected via its HTML5- and WebSockets- compatible browser to the URL of the application. The web-page requests the user to enter the “group name” of the multicast event he wishes to follow or to select one group name from the list of the available broadcast events

Leave a comment