Little's Law: WhatsApp System Design Interview Question

Imagine a world where billions of messages are sent across the globe in real time, connecting friends, families, and strangers alike. This is the magic of WhatsApp, a platform that handles staggering volumes of messages with such ease! Only geeks like you and me wonder, what could be happening behind the scenes!

In a nutshell behind this seamless experience lies a complex system design, where every queue, every server, and every millisecond counts! Today will take a look at a crucial concept which can help you in designing systems of this scale and size - this concept is called "Little's Law"



Photo by Daniel K Cheung on Unsplash



Little's Law Definition:

Little's Law is a fundamental principle in queueing theory that relates the average number of items in a system (L) to the average arrival rate of those items (λ) and the average time each item spends in the system (W). It can be expressed as the simple equation:

L = λW


Here's a breakdown of the terms:

L: This represents the average number of items currently present in the system. Think of it as the average queue length in a coffee shop, or the average number of emails waiting in your inbox.

λ: This represents the average arrival rate of items entering the system. In a coffee shop, it's the average number of customers arriving per hour. For emails, it's the average number of emails received per minute.

W: This represents the average time each item spends in the system. In a coffee shop, it's the average time a customer spends from entering to leaving. For emails, it's the average time an email spends in your inbox before being read or deleted.


Essentially, Little's Law tells us that the average number of items in a system is simply the product of the average arrival rate and the average time each item spends in the system. It's a powerful tool for understanding how queues behave and optimizing their performance.


Real-World Example of Little's Law

Imagine a bustling coffee shop. Customers arrive to order (arrival rate, λ), spend time sipping their lattes (average time in system, W), and eventually leave. At any given moment, a certain number of customers are in the shop (queue length, L). Little's Law simply connects these dots:


L = λW


This seemingly simple equation holds immense power. It reveals the fundamental relationship between how many items arrive at a system (think messages), how long they stay (processing time), and how many are present at any given time (queue length). In essence, it teaches us:

  • Higher arrival rates lead to longer queues unless we process messages faster.
  • Shorter processing times reduce queue lengths, even with high arrival rates.


System Design Example of Little's Law: WhatsApp

Imagine 50 million messages per hour arrive (λ), and users spend an average of 0.2 seconds for each message (W), this 0.2 seconds is for sending/receiving including processing on the server

Let's apply Little's Law here:

L = λW

L = 50 million messages/hour * 0.2 seconds/message = 10 million messages


This tells us that, on average, 10 million messages are in queue in the system at any given time. This queue length is crucial for WhatsApp to allocate server resources and optimize message delivery.


The 20% Surge Challenge:

But what happens during a major event, like the One Day Cricket World Cup Finals, when active users surge by 20%?

Little's Law comes to the rescue again. With the new arrival rate (λ_new = 60 million messages/hour instead of the earlier 50 million messages/hour),

Now, WhatsApp needs to adapt to maintain a smooth experience, they can do one or more of the following:

  • Horizontal scaling: Add more servers to distribute the increased load.
  • Vertical scaling: Upgrade existing servers with more processing power.
  • Queue management: Utilize efficient algorithms to prioritize and minimize wait times.


How does Little's Law Help?

It helps the WhatsApp team manage the following things better:

Estimating server capacity: By calculating the expected queue length for different user loads, WhatsApp can provision servers and anticipate in advance the resources needed before the next big surge.

Monitoring performance: Tracking and keeping a check on the actual queue length against the calculated queue length. This helps identify bottlenecks and optimize message delivery systems, they can drill down the issue quickly. This way they are well prepared because they are aware of what the system is designed for as compared to the actual loads it is currently handling!

Prioritizing resource allocation: Understanding the relationship between arrival rate, response time, and queue length allows WhatsApp to prioritize resources during traffic spikes, ensuring critical messages are delivered promptly, even for the craziest cricket fan!! 😉


Beyond Little's Law:

Although Little's Law is a very powerful tool, it's just one piece of the puzzle. To achieve the scale and performance of today's WhatsApp system design, it takes a lot more than just Little's Law, A few other wonderful concepts that help include:

Database sharding: Distributing user data across multiple servers for faster access and scalability.

Content Delivery Networks (CDNs): Caching frequently accessed content closer to users for faster delivery and reduced server load.

Load balancing: Distributing traffic across different servers to prevent overloading and ensure smooth performance.


Conclusion:

Brilliant minds at WhatsApp's team have beautifully crafted their system design and is a wonderful use case to study while you prepare for the System Design interviews.

This use case helps you understand how to anticipate user behaviours, efficiently manage queues, and constantly optimize the infrastructure.

Hope you appreciate all the wonderful insights you learned here when you hit the send button next time on WhatsApp!




Connect with me on LinkedIn for further insights and updates! You can find me on my LinkedIn Profile. Looking forward to connecting!

Kickstart your cloud computing journey with our free guide and Subscribe to our weekly newsletter for exclusive insights and updates