Messaging Application

Messaging Application Feature Requirements

Messaging Application Comminucation Protocol

For data exchange, we may look into following protocols:

Data exchange between servers

ChatApp
Once we are authenticated in our messaging application,

User Server Heartbeat Time
User A N2 2018-07-16T19:20:30+01:0
User Server Heartbeat Time
User A N2 2018-07-16T19:20:30+01:0
User A N3 2018-07-16T19:21:30+01:0

Architecture

ChatApp
User A logs in, pulls its friends from User Table, which stores friendship between users (identified by uniquely generated ConversationID) along with their respective encryption keys (which will be encrypted with another master key stored somewhere else)

User 1 User 2 ConversationID Encryption Key
User A User B 5 ABfsf23er3r
User A User C 6 ABjhgj2hr3g

When both users are online

User A sends message to User B
Redis table has recent heartbeat entries indicating both are online

User Server Heartbeat Time
User A N2 2018-07-16T11:20:30+01:0
User B N3 2018-07-16T11:21:30+01:0
Conversation_ID Time Text FromUser URL
5 t1 (Uniquely generated) {hi} User A  

Message sent to offline user

User A sends message to User C
Redis table doesn’t have a recent heartbeat entry for user C indicating it is offline

User Server Heartbeat Time
User A N2 2018-07-16T11:20:30+01:0
User C N3 2018-07-16T09:21:30+01:0
Conversation_ID Time Text FromUser URL
6 t2 (Uniquely generated) {YO} User A  
toUser fromUser time
User C User A t1 (time of earliest unread message)

READ TABLE

fromUser toUser time
User A User C t1

Architecture II

In this method system stores messages (if required) till the message is delivered to the recipient. We have a Conversation Queue in place of Conversation Table.

ChatApp

Message Transfer

If User B is online

If User B is offline

READ STATUS

Multimedia Exchange

ChatApp

Conversation_ID Time Text FromUser URL
5 t4 (Uniquely generated)   User A url1

How to optimise Database

Conversation Table

Conversation_ID Time Text FromUser URL
5 t1 (Uniquely generated) {hi} User A  
5 t2 (Uniquely generated) {YO} User A  
5 t3 (Uniquely generated) {sSP} User A  
5 t4 (Uniquely generated) {wru?} User A  

Once messages are inserted in Conversation Table we don’t update them and hence table keeps on growing. To optimise the storage we can take last few days worth of data, convert it into a blob and store it into Blob storage (Which is way more cost efficient) instead of table. Suppose in our current table, we will store data of t1, t2, t3 in the blob and replace it with Url

Conversation_ID Time Text FromUser URL
5 t1 (Uniquely generated)   User A url1
5 t4 (Uniquely generated) {wru?} User A  

Group Chat

References

System Design : Design messaging/chat service like Facebook Messenger or Whatsapp
System Design: Messenger service like Whatsapp or WeChat - Interview Question

SUBSCRIBE TO NEW CONTENT

Tutorials on System Design, BlockChain, Design Patterns and more sent out once a week.

Loading comments...