Search⌘ K
AI Features

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.

Functional and nonfunctional requirements of the Messenger API
Functional and nonfunctional requirements of the Messenger API

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:

  1. Client to API gateway

  2. API gateway to backend services

Provide your answer in the widget below.

API design for Facebook Messenger
API design for Facebook Messenger

Architectural style for Messenger API

Design overview and decisions

With these requirements established, the Messenger API design involves multiple cooperating services. Incoming requests flow through three main services:

  1. Asset service: Handles media and documents sent via chat.

  2. User data services: Manages user profiles and associated metadata.

  3. 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 working architecture of Messenger's services
The working architecture of Messenger's services

The following table describes the purpose of each component in the Messenger system:

Components and Services Details

Component or Service

Details

User data service

  • User relevant data is accessed or stored via this service in the relevant database

Asset service

  • Responsible for storing/retrieving media files in the blob storage

Messages service

  • Facilitates retrieval of messages on new/alternative devices


Messaging queue

  • Serves as buffer space for temporarily holding users’ messages
  • Enables asynchronous communication between chat service and persistence storage
  • Decouples services aiding system scalability

Zookeeper

  • A service discovery that keeps a check on the health of various servers

WebSocket manager

  • Keeps mapping of active/online users and the ports assigned to them

Chat server

  • Facilitates message delivery between sender and receiver

Presence server

  • Responsible for managing users' online status

API gateway

  • Authenticates and authorizes a user request
  • Performs request throttling and caches responses to frequently made API calls. For example, data related to specific conversations or user profiles can be cached to reduce the load on the backend servers

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 WebSocket connectionThe WebSocket protocol is used for real-time two-way communication. The connection is kept open, and whenever a message is available, it is communicated to the other party. is created between the sender and a chat server. The WebSocket manager updates the user-to-port mapping. ZooKeeper monitors the health of chat, presence, and WebSocket manager servers and maintains a registry of live servers. The same process ...