Facebook Messenger API Design
Explore the design of Facebook Messenger API by understanding its functional and non-functional requirements, service architecture, and protocols. Learn how to support real-time messaging with WebSocket and HTTP/2 for media uploads, implement message ordering, ensure security with encryption, and meet latency goals. This lesson equips you with practical strategies for building scalable, reliable instant messaging APIs.
Requirements of the Messenger API
Instant messaging applications like Messenger, WhatsApp, and Telegram connect millions of users, exchanging billions of messages daily. Designing an API for Facebook Messenger requires identifying the services it uses and how they interact to support real-time chat.
Functional requirements
Conversation: Support one-to-one and group messaging.
Sharing of media files: Enable sharing of audio, images, and videos.
Message status: Expose message states: sent, failed, delivered, and read.
Paginated chat list: Provide a paginated message history, stored locally on the client for offline viewing.
Nonfunctional requirements
Consistency: Messages must be delivered in send order, with identical history across devices.
Availability: The API must be highly available for uninterrupted service.
Security: All communication must be end-to-end encrypted.
Latency: As an instant messaging system, the API must minimize message delivery time.
Prerequisites
Before embarking on the design of the Messenger API, we need to have an in-depth understanding of the following concepts:
- API for file upload service: This chapter describes the design of API for one of the functional requirements of Messenger, which is the sharing of media files.
- Security mechanisms: We also need different security mechanisms to make the communication secure between the client and server. This chapter discusses various techniques that make communication secure.
- Event-driven protocols: The chat application is governed by different event-driven protocols that are discussed in this chapter.
We now want to design the API for Facebook Messenger. You’re tasked with deciding the architectural styles for the API between:
Client to API gateway
API gateway to backend services
Provide your answer in the widget below.
Design overview and decisions
With these requirements established, the Messenger API design involves multiple cooperating services. Incoming requests flow through three main services:
Asset service: Handles media and documents sent via chat.
User data services: Manages user profiles and associated metadata.
Real-time services: Responsible for two-way user communication (chat).
Supporting components such as ZooKeeper and messaging queues facilitate these services. The detailed responsibilities of each are listed in the table below:
The following table describes the purpose of each component in the Messenger system:
Components and Services Details
Component or Service | Details |
User data service |
|
Asset service |
|
Messages service |
|
Messaging queue |
|
Zookeeper |
|
WebSocket manager |
|
Chat server |
|
Presence server |
|
API gateway |
|
Workflow
The login process starts at the API gateway, where the request is authenticated using data from the user data service. When a sender wants to message another user, a