Architecture

Build a chat messenger

Requirements:

  1. User should be able to chat with anyone online or send message to offline 
  2. Scalable to millions of users
  3. Sending a message to offline user 
  4. Scrolling of the old messages

Architecture Overview

Design Tenets

We would be deploying the application in the following microservices since microservices are decoupled from each other and can be deployed, updated and scale individually. We have following microservices :

  1. Authentication service
  1. Chat service
  2. Blob storage service

An aggregated proxy service can be added to forward the traffic to the right backend service by looking at the hash of the receiver.This operation can be performed by the load balancer also. The server check if the sender is online by looking at the current connections. If the receiver is available, the message is pushed to receiver else it is stored in redis. The message is sent to Hbase also for the backup of the data (and support of feature like seeing old records) and running of analytics jobs (It can be a stream job which can ingest data from kafka and use Apache storm/spark to process the data and store in the hbase).

Authentication service’s primary responsibility is to perform user authentication with ldap or any other authentication mechanism. Blob storage service is used to store the files/images which are transferred during the chat. The chat message will only have the url of the blob storage which is already uploaded to blob storage. 

MQTT protocol for the clients

App clients can use MQTT protocol for communications. For the web clients, websockets or XMPP can be used.

Database Schema

Redis

Key – ReceiverId

Structure – List

FieldType 
SenderIdtext
Messagetext

Hbase

Key – SenderId+ReceiverId

FieldType 
Messagetext

Alternate designs

  1. Using Proxy service instead of load balancer L7 switching
  2. XMPP instead of MQTT or websockets
  3. Using Cassandra for caching as well as persistent of the messages

Suggested Readings

  1. Facebook using MQTT
  2. XMPP vs Websockets

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button