Explained step by step
Functional: 1-on-1 chat, groups of up to 100 people, and sharing files like photos and videos. Non-functional: high availability with minimal latency, and it must scale. Extended: sent, delivered and read receipts; showing last seen; and push notifications. It's like designing a postal service: delivering letters is the basics, the delivery receipt is an extra.
With 50 million daily active users (DAU), each sending 10 messages to 4 people, that's 2 billion messages per day: about 24,000 per second. If 5% carry files, that's 100 million files per day. At 100 bytes per message and 100 KB per file: about 200 GB plus 10 TB per day, 10.2 TB in total. Over 10 years, about 38 PB, and you need about 120 MB per second of bandwidth. It's like counting how many letters pass through a country's mail in 1 day.
Tables: users (name, phone), messages (type, content, delivery times, and the chat or group), chats (a private conversation between 2), users_chats, groups and users_groups. The users_chats and users_groups tables exist because the relationship is many-to-many: 1 user has many chats and 1 chat has several users. The model is quite relational, but putting everything in 1 single database isn't wise: it's split among services, each owning its table, using PostgreSQL or Cassandra. It's like a school: students, subjects and a list saying which student takes which subject.
Microservices are used to scale and decouple, each owning its data. User service: HTTP, authentication and user data. Chat service: WebSockets for messages, with a cache of active connections to know who's online. Notification service: sends push. Presence service: stores last seen. Media service: uploads photos and videos. Between them, gRPC performs better than REST, and you need service discovery or a service mesh. It's like a post office with windows: each one handles 1 kind of task.
To send messages in real time there are 2 models. Pull: the client asks every so often whether there are new messages, for example with long polling; it doesn't scale, because most responses come back empty and waste resources. Push: the client opens a long-lived connection and the server pushes data as soon as it arrives, with WebSockets or SSE. For the lowest latency, push with WebSockets fits best. It's like waiting for a package: either you ask at the post office every hour (pull), or the mail carrier rings your bell (push).
For last seen, the client sends a heartbeat every so often to say it's still alive, and the presence service stores the time in a cache like Redis: the key is the user, the value is the time. Another, lazier way: record the user's last action, and if it passes a threshold, like 30 seconds without doing anything, show them offline with that time. It's like a «back in 5 minutes» sign: you know when they were last there.
When a message arrives, the system checks whether the recipient is active. If not, the chat service puts an event in a message queue, with data like the phone's platform. The notification service consumes it and sends it to Firebase Cloud Messaging (FCM) or Apple Push Notification Service (APNS), depending on Android or iOS. A queue is used because it delivers at least 1 time and nearly in order. It isn't a pub/sub case, because each platform handles its notifications its own way. It's like leaving a note in the mailbox when nobody answers the bell.
Most of the space, about 10 TB per day, goes to files. The media service stores them in object storage, like Amazon S3, Azure Blob Storage or Google Cloud Storage, and they're served from a CDN to cut latency and cost. Fun fact: WhatsApp deletes the file from its servers once the user has downloaded it. Also, compressing photos and videos saves a lot of space. It's like a luggage storage room: it keeps the suitcase until you pick it up.