Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 38

Balancing Act: Latency vs. Throughput

This lesson is built with resources from youtube.com, interviewcoder.co and 1 more site.

Hello! Welcome to the sixth lesson in our "System Design Interview Framework" module.

In our last lesson, we explored the foundational trade-offs between consistency and availability using the CAP and PACELC theorems. We learned how to drive a nuanced discussion, linking these abstract concepts to concrete choices in database technology and replication strategies.

Today, we shift our focus to another critical axis of system performance. The learning outcome for this lesson is to drive trade-off discussions around system performance (latency vs. throughput). For a Staff or Senior Staff candidate, demonstrating mastery of this trade-off is essential. It shows you can design systems that are not only functional but also efficient, cost-effective, and deliver the right user experience under load.

We will move beyond simple definitions to explore the architectural patterns that allow you to deliberately prioritize one metric over the other, and how to articulate these complex decisions in an interview.

1. Defining the Core Metrics: Latency and Throughput

Before we can discuss trade-offs, we must be precise about what we are trading off. In a system design interview, using clear, industry-standard language is a key signal of experience.

  • Latency: The time it takes for the system to process a single request. It's the measure of how fast your system feels to a user.
    • Staff+ Nuance: Don't just talk about "average latency." Speak in terms of percentiles. For example, p99 latency is the latency experienced by 99% of requests. Optimizing for p99 (tail latency) is often more critical for user experience than optimizing the average.
  • Throughput: The number of requests the system can handle in a given time period. It's the measure of your system's capacity. This is typically expressed as Queries Per Second (QPS) or Transactions Per Second (TPS).

This article provides excellent guidance on the language to use in an interview.

Engineer's Guide to System Design Interview Preparation

This guide from Interview Coder is a great resource for understanding the specific language interviewers expect. It reinforces the importance of using precise terms to frame your design decisions.

Please read the sections titled 'Common Trade-Offs and the Language You Should Use' and 'Anchor Every Choice to Users and Constraints'. Focus on how terms like 'Latency', 'Throughput', 'p99 latency', and 'TPS' are used to justify design choices by connecting them to user outcomes and SLOs (Service Level Objectives).

The Fundamental Relationship

Latency and throughput are often in tension. Optimizing for one can negatively impact the other. Imagine a single, very wide highway lane. A single car can travel very fast (low latency). Now imagine splitting that highway into five narrower lanes. The total number of cars that can pass per hour is much higher (high throughput), but each car might travel slightly slower due to more congestion.

Latency vs. Throughput Trade-offs Graph
This graph illustrates the typical trade-off space. Real-time systems prioritize low latency, while big data systems prioritize high throughput. The 'Ideal' point is what we strive for, but it's often unattainable due to physical and cost constraints.

As system load increases, this relationship becomes even more apparent.

Throughput vs. Latency Across Load Levels
This graph shows a common system behavior. As load increases, throughput initially rises. However, after a certain point (the system's peak capacity), adding more load causes components to become overwhelmed. Latency skyrockets, and actual throughput may even decrease as the system thrashes.

Understanding this "knee" in the performance curve is critical. A key goal of scalable design is to push that knee as far to the right as possible.


2. Identifying System Archetypes: Read-Heavy vs. Write-Heavy

The first step in driving a trade-off discussion is to characterize the workload. Is the system primarily handling reads or writes? This initial classification will guide your entire performance strategy.

the system design interview (intermediate - advanced) anthony explains #556

This video by Anthony Sottile (anthonywritescode) provides a very practical way to frame this initial step of a system design interview.

Watch the segment from 05:00 to 06:25. Pay attention to how he categorizes systems into 'read-heavy' (like a social media feed) and 'write-heavy' (like a voting or ticketing platform) and how this dictates the design approach.

  • Read-Heavy Systems: The primary goal is often to minimize latency. Users expect fast-loading content. Examples: News feeds, e-commerce product pages, search results.
  • Write-Heavy Systems: The primary goal is often to maximize throughput. The system must be able to ingest a high volume of data without dropping requests. Examples: Ad click aggregation, logging services, IoT sensor data ingestion.

Once you've made this distinction (and confirmed it with your interviewer), you can propose specific architectural patterns tailored to that workload.


3. Architectural Patterns for Performance Trade-offs

This is where you demonstrate your skills as a system architect. You need to propose specific components and explain how they help you achieve your desired performance characteristics by making a deliberate trade-off.

Optimizing for Throughput (Handling Write-Heavy Loads)

When you need to handle a high volume of writes, the goal is to decouple the client from the expensive work of processing and storing the data. This improves throughput at the cost of slightly increased end-to-end latency for any single write.

Pattern 1: Asynchronous Processing with Message Queues

Instead of writing directly to a database, the client writes a message to a queue (like Kafka, RabbitMQ, or AWS SQS). This is a very fast operation. Worker processes then consume messages from the queue at their own pace and perform the slower database writes.

the system design interview (intermediate - advanced) anthony explains #556

Let's return to the video from anthonywritescode, where he explains exactly how to apply this pattern.

Watch the segments from 18:14 to 19:02 and 24:59 to 26:11. Notice how he introduces a message queue to offload a 'heavy write workflow' and uses aggregation to reduce the number of database writes, thereby increasing throughput.

How to phrase this trade-off:

"For our ad click analytics system, which is extremely write-heavy, I would introduce a message queue like Kafka. The client-facing service will just publish a small message for each click, which is a very low-latency operation for the client. This allows us to achieve massive write throughput. The trade-off is that the click data will be processed asynchronously, so there will be a few seconds of end-to-end latency before it appears in our analytics dashboard, which is acceptable for this use case."

Pattern 2: Batching and Compression

To further increase throughput, especially when using a system like Kafka, you can configure the producer to batch messages and compress them before sending them over the network.

Kafka System Design Deep Dive w/ a Ex-Meta Staff Engineer

For a Staff-level deep dive, let's look at a concrete example within Kafka. This video from Hello Interview goes into specific producer-side optimizations.

Watch the clip from 39:11 to 40:44. Focus on the two specific techniques mentioned for increasing throughput: batching messages (to reduce the number of requests) and compressing messages (to reduce data size). This gives you very specific levers to discuss in an interview.

Optimizing for Latency (Handling Read-Heavy Loads)

When you need to deliver data to users as quickly as possible, the goal is to reduce the amount of work done at request time and shorten the distance the data has to travel.

Pattern 1: Caching

This is the most fundamental latency optimization. Store frequently accessed data in a fast, in-memory cache like Redis or Memcached. This avoids slow, expensive queries to the primary database.

Pattern 2: Data Replication & Geo-Distribution

For a global user base, network latency is a major factor. By placing read replicas of your data in different geographic regions, you can serve users from a location closer to them. This directly relates to the PACELC discussion from our previous lesson, where you might choose lower Latency over strong Consistency during normal operation (Else).

the system design interview (intermediate - advanced) anthony explains #556

Let's look at how caching and replication are used to handle hot read paths and global distribution.

Watch the segments from 19:02 to 19:48 and 20:33 to 22:24. Observe how caching is introduced for a 'hot read path' and how multi-region replication is proposed specifically for 'read-heavy systems' to reduce latency for global users.

How to phrase this trade-off:

"To ensure a low-latency experience for users reading their news feed, I'll introduce a caching layer using Redis. We'll store the pre-computed feed for active users. This adds complexity around cache invalidation, but the trade-off is a significantly lower p99 read latency. For our global users, I'll use read replicas in EU and APAC regions, asynchronously replicating from our primary in US-East. This means users in those regions might see data that is a few seconds stale (eventual consistency), but their read latency will be dramatically lower."


4. Driving the Discussion: A Framework for Senior Candidates

A Staff+ engineer doesn't just present a single solution; they guide the conversation by surfacing trade-offs proactively.

This article provides an excellent mental model for this process.

System Design Tradeoffs: How to Think and Explain in Interviews

This article from the System Design Handbook is invaluable for understanding the mindset of a senior engineer when discussing trade-offs.

Read the sections 'Performance vs scalability tradeoffs' and 'Latency vs correctness tradeoffs'. Focus on the distinction between performance and scalability, and how choices that improve one can impact the other. The table contrasting local state vs. stateless services is a perfect example of a trade-off discussion.

Here's how you can apply this in an interview:

  1. Clarify Performance SLOs: Start by asking clarifying questions. "What are the performance goals? Are we optimizing for p99 latency for reads, or for peak write throughput? What are the target numbers?"
  2. Characterize the Workload: Explicitly state whether you believe the core problem is read-heavy or write-heavy, and get buy-in from the interviewer.
  3. Propose a Pattern and State the Trade-off: When you introduce a component, immediately state the trade-off it implies. "I'm adding a message queue here. This improves our write throughput and resilience, but the trade-off is we now have an eventually consistent system."
  4. Mention Alternatives: Briefly mention an alternative and why you chose against it. "We could process these writes synchronously to get lower end-to-end latency, but that would cap our throughput at what the database can handle and risk cascading failures under load."

Conclusion

In today's lesson, we moved from the theoretical (CAP theorem) to the tangible world of system performance. You've learned how to define and differentiate latency and throughput, and how to use specific architectural patterns to optimize for one over the other. Most importantly, you now have a framework for driving these discussions at a level expected of a Staff+ engineer.

Key Takeaways:

  • Be Precise: Use specific terms like p99 latency and QPS. Anchor your performance goals to SLOs.
  • Characterize First: Classify the system as read-heavy or write-heavy to guide your design.
  • Master the Patterns:
    • To boost throughput, use message queues and batching.
    • To reduce latency, use caching, replication, and geo-distribution.
  • Articulate the Trade-off: For every pattern you introduce, explicitly state what you are gaining (e.g., throughput) and what you are giving up (e.g., adding latency, increasing complexity).

Preview of the Next Lesson:
Our performance choices directly impact another crucial set of constraints. In the next lesson, we will broaden our scope to drive trade-off discussions around operational concerns (cost vs. scalability vs. reliability). We'll explore how the decisions we made today about queues, caches, and replicas affect the budget, the operational burden, and the overall robustness of our system.