Course Content
Data Structures & Algorithms
Full Stack Web Development
Understanding and playing with DOM (Document Object Model)
0/2
MERN project
0/2
Low Level System Design
LLD Topics
High Level System Design
Fast-Track to Full Spectrum Software Engineering
1. What is Caching?

Caching is a technique used to store frequently accessed data in a temporary storage (cache) to reduce the time it takes to retrieve it. Caches are typically faster than accessing data from a database or an external resource, so by caching data, we can improve response time and reduce the load on backend services like databases or external APIs.


How Caching Helps in Messenger:

  • Improved Performance: Caching frequently accessed data, such as user profiles, message history, or status updates, allows Messenger to serve this data quickly without having to query the database every time. For example, if a user is frequently viewing their own profile or recent chats, this data can be cached.

 

  • Reduced Database Load: By caching popular or recent data, Messenger reduces the number of database queries, which helps prevent the backend database from becoming overloaded, especially during peak usage times.

 

  • Reduced Latency: Since caches are often stored in in-memory systems (like Redis or Memcached), data retrieval is much faster than fetching from traditional databases, leading to lower latency and better user experience.


Types of Caching in Messenger:

Content Caching:

  • User data (profile information, message history, etc.) can be cached at the application level to provide instant access to commonly accessed content.

 

Message Caching:

  • Caching recent messages allows for faster message retrieval, particularly in active conversations. This cache can be maintained per user or per conversation (chat).

 

Media Caching:

  • Media files (images, videos, etc.) can be cached to avoid fetching them from storage or the server multiple times.

 

Session Caching:

  • Cached session data, like user authentication tokens, active sessions, and last-read messages, allow the system to quickly validate users and track their activity.

 

How Caching Works in Messenger:

For example, when a user opens their chat list:

 

  1. Messenger checks if the recent list of conversations and user data is available in the cache.
  2. If the data is cached, it is served directly from the cache, resulting in a fast response.
  3. If the data is not cached, the system queries the database to retrieve the data, caches it, and serves it to the user.

 

2. What is Load Balancing?

Load Balancing refers to the process of distributing incoming network traffic across multiple servers to ensure that no single server becomes overwhelmed with too many requests. This ensures high availability, reliability, and optimal resource usage in a distributed system.



How Load Balancing Helps in Messenger:

  • Distributes Traffic: Messenger has millions of users who generate lots of traffic. Load balancing ensures that no single server handles too many requests, allowing the system to scale horizontally across many servers.

 

  • Fault Tolerance: If one server becomes unavailable due to a failure, the load balancer can automatically route traffic to healthy servers, minimizing downtime and ensuring uninterrupted service for users.

 

  • Improved Performance: By evenly distributing traffic, load balancing prevents bottlenecks, improves server response times, and maintains system performance during periods of high traffic.

Types of Load Balancing in Messenger:

Round Robin Load Balancing:

  • In this basic load balancing strategy, incoming requests are distributed evenly to all available servers. The load balancer routes requests to the next server in the list, starting from the top.

 

Least Connections Load Balancing:

  • The load balancer routes traffic to the server with the least number of active connections. This ensures that servers with the least load (e.g., those handling fewer requests) get new traffic, helping maintain balance.

 

IP Hashing Load Balancing:

  • The load balancer routes traffic based on a hash of the client’s IP address. This ensures that requests from the same client are consistently sent to the same server, which is useful when the server needs to remember session information (e.g., chat history).

 

Geographic Load Balancing:

  • For global services like Messenger, geographic load balancing ensures that users are routed to the nearest data center. This minimizes latency by directing traffic to servers that are geographically closer to the user.


How Load Balancing Works in Messenger:

  1. When a user sends a message, the request is first received by the Load Balancer.
  2. The load balancer routes the request to one of the available application servers.
  3. If an application server becomes overwhelmed or unavailable, the load balancer routes the request to another healthy server.
  4. The server processes the request (e.g., sending a message, retrieving chat history) and responds to the user.

 

3. Cache & Load Balancing Working Together in Messenger

Cache and Load Balancing complement each other by improving both data retrieval speed and overall system reliability. Here’s how they work together:

 

  • Load Balancing for Cache Requests: When a request is made for cached data (e.g., a user’s message history), the load balancer ensures that the request is directed to the server with the least load, which may have the cached data.

 

  • Load Balancing for Database Requests: If data is not available in the cache and a database query is required, the load balancer ensures that database requests are routed efficiently to the least busy server.

 

  • Distributing Cache Across Multiple Servers: In a distributed caching setup, multiple application servers or cache nodes (e.g., Redis nodes) can be used. The load balancer ensures that requests for cache are directed to the appropriate server (or node), improving cache hit rates and reducing response times.

 

  • Global Load Balancing and Caching: Messenger users are spread across different geographic regions. With global load balancing, users are routed to the nearest server, which may also have cached data, ensuring both low latency and quick responses.


4. Benefits of Cache & Load Balancing in Messenger

Performance Improvements:

  • Faster Responses: Caching reduces the time it takes to retrieve data from the database, leading to faster load times for users.

 

  • Reduced Database Load: Caching frequently accessed data helps reduce the load on the backend database, ensuring it operates efficiently even during high traffic periods.

 

  • Optimized Resource Utilization: Load balancing ensures that all servers are used efficiently, preventing any one server from being overworked.

Scalability
:

  • Handling More Users: As Messenger scales to accommodate more users, both caching and load balancing help ensure the system can handle increased traffic without sacrificing performance.

 

  • Horizontal Scaling: Both cache and load balancing support the ability to scale horizontally by adding more servers or cache nodes to the system as demand grows.

Fault Tolerance & Availability
:

  • Redundancy: Load balancing ensures that even if one server or cache node fails, the system can automatically route traffic to healthy servers, ensuring continuous service.

 

  • Geographically Distributed Caches: Cache data can be replicated across different regions to ensure fast access even during regional failures or high demand.

5. Real-World Example: How Caching & Load Balancing Improve Messenger

 

Message Delivery:

  • When a user sends a message, the system first checks the cache to see if the recipient’s chat history or profile information is already stored. If so, the cached data is served directly, resulting in a fast and responsive experience.
  • If the data is not in the cache, the system queries the database for the required information. This is where load balancing ensures that the query is directed to the least busy database server, maintaining optimal system performance.

 

User Authentication:

  • When a user logs into Messenger, their session information (such as tokens and user data) is cached for fast access in subsequent requests. Load balancing ensures that authentication requests are distributed across multiple servers, making the login process faster and more resilient.

 

Media Access:

  • For large media files like photos or videos, Messenger caches media content for fast retrieval. The load balancer ensures that media requests are routed to the appropriate server or cache node closest to the user, minimizing load times.
  •  


Conclusion

Cache and Load Balancing are integral to building scalable, fast, and reliable systems like Messenger. Caching improves performance by reducing data retrieval time and offloading work from backend systems, while load balancing ensures that traffic is evenly distributed across servers, improving both reliability and resource utilization. Together, these techniques provide a seamless experience for users, whether they’re chatting with friends, sending photos, or logging in from around the world.

0% Complete
WhatsApp Icon

Hi Instagram Fam!
Get a FREE Cheat Sheet on System Design.

Hi LinkedIn Fam!
Get a FREE Cheat Sheet on System Design

Loved Our YouTube Videos? Get a FREE Cheat Sheet on System Design.