Behind the scenes of Chat Applications

Sudaraka Jayathilaka
4 min readSep 4, 2018
Source : https://www.userlike.com/en/blog/benefits-of-live-chat

There are a lot of chat applications are in the use. But only few of them standout because of the qualities they bear which attracts the users. Two of those best chat apps are considered to be Facebook Messenger and Whatsapp. Although these companies are discrete about the architecture of their applications, I managed to find bits and pieces of information from blog articles and forums. This is a composition of them and these facts can be outdated since all these applications are tend to be updated frequently.

1. Facebook Messenger

Facebook’s messenger is one of the most popular chat applications. Several years back, messenger wasn’t on it’s best shape. It’s performance and space usage was starting to lag behind. The messenger was using a pull-based model. Each time, the app was acknowledged with a lightweight push notification about the availability of new messages and then the app would pull and receive a heavy JSON response containing the conversation view. But later messenger was updated to use a push-based snapshot + delta model.

MQTT is a machine-to-machine (M2M) connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport - mqtt.org

Another interesting change that has been done in messenger infrastructure is switching from JSON to Thrift. This major change has caused a reduction in the payload size by 50%.

There has been significant improvements made in the sever side as well. Earlier each message was written to a traditional storage tier before sending messenger a trigger to read the message from the storage tier which made scaling problematic. But the process was completely replaced using an ordered queue implementation created by Facebook itself called “Iris”. One of the interesting features of Iris is the usage of pointers which facilitates message syncing between messenger and the storage tier. The following image clearly shows how message syncing happens in messenger

https://code.fb.com/wp-content/uploads/2014/10/GGRboQC1BZFuvFsCANb4tBkAAAAAbj0JAAAD.png

The above described procedure has been able to remove the dependency of message delivery on the availability and the writing speed of the storage tier in use. Iris stores the messages temporarily in it’s backing storage until all the messages synced properly.

Another interesting fact is that, the backing storage of Iris has been built using MySQL and flash.

2. Whatsapp

Whatsapp is based on LYME/LYCE stack.

Source: https://en.wikipedia.org/wiki/LYME_%28software_bundle%29#/media/File:LYME_software_bundle.svg

Within the behind the scenes of Whatsapp, one of the most fascinating and the important things is the backend written in Erlang. Erlang has a huge contribution towards making Whatsapp one of the best chat apps in the world.

Why Erlang ?

Erlang is a general-purpose, concurrent, functional programming language. Areas where Erlang is known to be useful are

> handling many connections

> routing messages given some aspects of the message

Since Erlang shows a great performance in the given scenarios most of the telecommunication related applications use Erlang in their implementations.

The initial implementation of the whatsapp server was done using Ejabberd. Which is a an open source Jabber/XMPP instant messaging server written in Erlang. The messaging protocol which used for the implementation is XMPP (eXtensible Messaging and Presence Protocol).

Another important aspect of a chat application is the database. Most of the times, database can become a performance bottleneck in real-time messaging apps. Whatsapp uses mnesia which is known to be a realtime distributed communication DBMS. One of the important aspects of mnesia is that it can be reconfigured in runtime.

Although whatsapp is considered to be fairly simple when compared to the messenger services like Facebook Messenger, It’s design isn’t a simple one. The following diagram shows a very abstract view of the Whatsapp architecture,

Source : https://codetiburon.com/app/uploads/2017/10/WhatsApp-architecture.png

--

--