DEV Community

Cover image for What Happens When You Send a WhatsApp Message?
Sreya Satheesh
Sreya Satheesh

Posted on

What Happens When You Send a WhatsApp Message?

We use WhatsApp every day.

Send a message.
See the ticks.
Get a reply.

It is easy to think of messaging as something that happens directly between two phones.

But there is a whole system between them.

Where does the message go after you tap Send?

How does WhatsApp find the person you're messaging?

What happens if they are offline?

How does one tick become two?

What happens when the same message is received twice?

And what changes when millions of people are sending messages at the same time?

Those questions became What's Up?

Start with one message

I wanted to build a different kind of system design visualizer.

Instead of starting with a large architecture diagram, I started with a WhatsApp chat.

Eva sends Sam:

Hey 👋

The message leaves Eva's phone.

From there, the app lets you follow what happens inside a messaging system.

The message reaches a server.

Sam's connection may be sitting on another server.

The system needs to find it.

If Sam is offline, the message needs to be stored until he comes back.

When he reconnects, the message can be delivered.

Then there is another question:

How does Eva know it was delivered?

And how does she know Sam actually read it?

The familiar ticks on WhatsApp represent a much larger system underneath.

What if something goes wrong?

A message can be sent again.

An acknowledgement can be lost.

A server can fail.

A connection can disappear.

If a request is retried, the system has to deal with the possibility that the same message has arrived more than once.

That is where things like message IDs and deduplication become important.

The app lets you break parts of the system and see what changes when they are missing.

Instead of only reading that a component is needed, you can see the problem it was solving.

What about photos and videos?

A WhatsApp message isn't always text.

Photos and videos are much larger.

So the system needs another path for media.

Object storage can hold the actual file.

A CDN can help deliver it.

The message itself can carry the information needed to find that media.

One chat can therefore involve several different services, depending on what is being sent.

What changes in a group?

A message to Sam is one thing.

A message to a group is different.

One message may need to reach many people.

Now the system has to deal with group membership, fan-out, queues and delivery at a larger scale.

The same small chat bubble creates a different problem once there are many recipients.

Where is everyone?

WhatsApp also shows things like online status and last seen.

That creates another question for the system:

How does it know whether Sam is online?

A presence system needs to keep track of changing connections without turning every small status change into an expensive operation.

Again, something that looks like a tiny piece of the WhatsApp interface has a system behind it.

Then there is scale

Two people sending messages is easy to picture.

WhatsApp has billions of users, so the system cannot depend on one server or one database.

Connections need to be distributed.

Messages need to be routed.

Data needs to be partitioned and replicated.

Queues can separate pieces of work.

Services need to keep working when individual servers fail.

The architecture grows from the questions.

And security?

A messaging system also has to answer a much more important question:

Who can read the message?

The design needs to account for encryption and, for WhatsApp, end-to-end encryption as part of the security model.

That adds another boundary to the system.

The message still needs to travel through the infrastructure.

The infrastructure should not simply become a place where the message can be read.

That's what I wanted to show

What's Up? is a visual walkthrough of how a WhatsApp-style messaging system can be designed.

It starts with one message and gradually introduces the pieces needed to make messaging work:

  • WebSockets for persistent connections
  • connection management
  • message routing
  • offline storage
  • delivery and read acknowledgements
  • retries and deduplication
  • media storage and CDNs
  • group messaging
  • queues
  • presence
  • databases
  • partitioning and replication
  • scaling
  • encryption

There isn't one giant diagram waiting at the beginning.

The architecture appears as the questions appear.

You start with:

Hey 👋

And by the end, you can see the system that had to exist for those two words to reach the other side.

What's Up?

That's the question behind the name too.

What's up after you press Send?

Quite a bit.

👉 Try What's Up?

Top comments (0)