Skip to main content

Welcome to a Grasp lesson

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

Lesson 88

Architectural Trade-offs: Scalability, Latency, and Cost

Recommended resources

Welcome back. In our last lesson, you meticulously deconstructed the "Design Twitter" problem, breaking it down into manageable components using a structured framework. You learned what to build: the services, the data stores, and the critical logic like timeline generation.

Today, we shift from the what to the why. This lesson is about mastering the art of a senior engineer: articulating and defending your architectural decisions. Any engineer can propose a solution; an elite engineer can explain the complex trade-offs behind their choices, grounding them in the realities of scalability, latency, and cost. This is the single most important skill to demonstrate in a system design interview.

By the end of this lesson, you will be able to confidently defend your architectural choices, moving beyond simply stating a design to justifying it with principles, data, and clear communication.

We will explore this through three core pillars:

  1. The Foundational Trade-off: Consistency vs. Availability
  2. The Pragmatic Triangle: Latency, Throughput, and Cost
  3. The Art of Communication: Defending Decisions Under Pressure

Let's begin.


1. The Foundational Trade-off: Consistency vs. Availability

(Tutor speaking)

At the heart of every large-scale distributed system lies a fundamental choice: when the system is under stress (like a network failure), do you prioritize keeping all data perfectly synchronized, or do you prioritize keeping the system responsive to users? This is the essence of the CAP theorem.

  • Consistency (C): Every read receives the most recent write or an error. All nodes see the same data at the same time.
  • Availability (A): Every request receives a (non-error) response, without the guarantee that it contains the most recent write.
  • Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.

In any real-world distributed system, network partitions are a fact of life. Therefore, the actual trade-off you must make is between Consistency and Availability.

This image provides an excellent overview of the CAP theorem and its more nuanced successor, PACELC.

CAP vs PACELC Theorem: Consistency & Partition Tolerance in Distributed Systems
This diagram explains the core tenets of the CAP and PACELC theorems. CAP dictates the trade-off between Consistency (C) and Availability (A) during a Partition (P). PACELC extends this: Else (if no partition), a system trades off between Latency (L) and Consistency (C).

As the PACELC part of the diagram shows, even when the network is healthy, there's often a trade-off between how fast you respond (Latency) and ensuring the data is perfectly up-to-date (Consistency).

To see why this is such a difficult problem, let's watch a clear explanation of the challenges of keeping data in sync across a distributed system.

Data Consistency and Tradeoffs in Distributed Systems

This video from Gaurav Sen's channel explains the fundamental reasons we use distributed systems and the consistency problems that immediately arise. It sets the stage for why the CAP theorem is so important.

Watch from 01:33 to 15:46. Focus on these key points: (01:33 - 03:43): The reasons to move from a single server to a distributed system (fault tolerance, scalability, latency). (07:15 - 09:16): How distributing data solves these problems but creates the new, difficult problem of consistency. (10:55 - 13:58): The 'Two Generals' Problem', which illustrates why guaranteeing consistency over an unreliable network is theoretically impossible without trade-offs. (13:58 - 15:46): The direct trade-off: if you want perfect consistency, you may have to sacrifice availability (by making users wait).

  • Metaphor for a 5-Year-Old: Imagine you and your friend are coloring the same picture but are in different rooms.
    • Strong Consistency (CP): Before you use a crayon, you must shout to your friend, "I'm using the blue crayon now!" and wait for them to shout back "Okay!" before you can color. No one ever uses the same crayon at the same time, but it's very slow. If your friend can't hear you (a partition), you have to stop coloring (unavailable).
    • Eventual Consistency (AP): You both just color. If you both grab for the blue crayon, one of you will get it first. Later, you'll look at the picture and see what happened. The picture gets colored much faster (available), but it might look a little messy for a while until you sort it out.

Understanding this core trade-off allows you to make the first and most critical decision in your system design: what level of consistency does this specific feature truly require?



2. The Pragmatic Triangle: Latency, Throughput, and Cost

(Tutor speaking)

While CAP and PACELC provide the theoretical framework, the day-to-day decisions of an engineer revolve around a more practical triangle: Latency, Throughput, and Cost. You can almost never optimize for all three. Improving one often degrades another.

  • Low Latency: The system feels snappy and responsive to users.
  • High Throughput: The system can handle a massive volume of requests.
  • Low Cost: The system is efficient and doesn't require expensive hardware or massive server fleets.

This diagram visualizes the classic tension between latency and throughput.

Latency vs. Throughput Trade-offs
This graph shows that systems optimized for ultra-low latency often have lower throughput, while systems built for massive throughput (big data) typically accept higher latency. Achieving both high throughput and low latency is the goal, but it is difficult and expensive.

To defend your decisions, you need to speak with authority not just about the concepts, but about the numbers. What does "low latency" even mean? 100ms? 10ms?

Latency Numbers Every Data Streaming Engineer Should Know

The article 'Latency Numbers Every Data Streaming Engineer Should Know' provides a fantastic, data-driven breakdown of what different latency targets mean in practice and what they cost in terms of system complexity and infrastructure.

Read the following sections from this article: TL;DR: Your Latency Budget Quick Reference: Study the three tables. These numbers are your new vocabulary. Memorizing the approximate latency of a cross-AZ network round trip or an SSD fsync is a hallmark of a senior engineer. What "Real-Time" Actually Means: Understand the definitions of Ultra-Low, Low, and Latency-Relaxed, and the cost implications of each. The Physics of Streaming Latency: Internalize the hardware and network floors. You can't design a system that violates the speed of light. Conclusion: Engineering Time as a Feature: Read the conclusion to grasp the high-level philosophy: latency is a feature you design for, not just a metric you hope is low.

Real-World Example: Netflix's Distributed Counter

Let's see how a real company navigates these trade-offs. Netflix needs to count billions of events, but not all counts are created equal.

How Netflix Built a Distributed Counter for Billions of User Events

This article from ByteByteGo explains how Netflix designed a flexible counting system. It's a masterclass in making deliberate trade-offs based on the specific use case.

Read the sections 'Why the Need for a Distributed Counter?', 'Counting Techniques', 'Netflix’s Hybrid Approach', and 'Key Benefits of the Hybrid Approach'. Focus on how Netflix explicitly offers different counting strategies to its internal developers, each with a different trade-off profile: Best-Effort Regional Counter: Fast, cheap, but less accurate. Why is this acceptable for A/B testing? Eventually Consistent Global Counter: Slower, more expensive, but accurate and durable. Why is this mandatory for billing metrics?

By offering different solutions, Netflix empowers its teams to choose the right point on the latency/consistency/cost spectrum for their specific need, rather than forcing a one-size-fits-all solution. This is a sophisticated architectural decision.



3. The Art of Communication: Defending Decisions Under Pressure

(Tutor speaking)

Knowing the trade-offs is half the battle. Communicating them effectively during an interview is the other, more critical half. The best way to learn this is to watch an expert do it.

We'll analyze a mock interview for designing an online auction system. This is a great example because auctions have very strict requirements for consistency (the highest bid must be accurate) and latency (bids must appear in near real-time).

OpenAI & Meta Senior/Staff System Design Mock Interview: Design Online Auction

In this mock interview from 'Hello Interview', watch how a strong candidate navigates the design, constantly verbalizing the trade-offs he's considering. Pay less attention to the final design and more to how he talks about his decisions.

Watch the following segments, focusing on the candidate's language: (01:48 - 10:08): Notice how he defines the non-functional requirements. He doesn't just say 'fast'; he says 'low latency for seeing updated bids' (< 500ms) and 'strong consistency for bids'. This sets the stage for his later decisions. (25:02 - 26:29): Listen to his discussion on database transactions. He identifies a potential consistency issue and immediately proposes a solution (transactions with a serializable isolation level), explaining why it's needed. (26:29 - 33:07): This is a masterclass in defending a choice. He considers polling ('brute force solution'), explains why it won't scale, considers and dismisses caching ('bids are changing all the time'), and finally lands on Server-Sent Events (SSE) with a PubSub system, justifying it as the best fit for the low-latency, one-directional update requirement. (38:20 - 43:51): Listen to the interviewer's feedback. He explicitly praises the candidate's process: 'the clear articulation of the problem, the handful of solutions, the weighing of the trade-offs and then the clear articulation of the solution that you're going to go with.' This is your goal.

Test your understanding!

The candidate in the auction video chose PostgreSQL, justifying it with the need for strong consistency and ACID transactions. An interviewer might push back: "PostgreSQL can be a bottleneck. Why not a scalable NoSQL database like Cassandra?" How would you defend the choice of Postgres, or alternatively, argue for Cassandra, using the language of trade-offs?

Show answer

Here's how you could structure a response, articulating the trade-offs for each choice:

Defense of PostgreSQL (Prioritizing Consistency):
"That's a great point. Choosing a database is a critical trade-off between scalability and consistency. For the core bidding process, I prioritized strong consistency. Here's my reasoning:

  • Problem: In an auction, we cannot have two winners, nor can we accept a bid that is lower than the actual current highest bid. This requires ACID guarantees, which relational databases like PostgreSQL provide out-of-the-box with serializable transactions.
  • Trade-off: The cost of this strong consistency is scalability. A single Postgres instance will eventually hit a write limit.
  • Mitigation: We can mitigate this. We can use read replicas to handle the high volume of users just viewing the auction, directing all write traffic (placing bids) to the primary. For a truly massive scale, we could shard Postgres by auction_id, but that adds significant operational complexity. I'm making a deliberate choice to accept this potential scaling challenge in exchange for guaranteed correctness in the most critical part of the system."

Argument for Cassandra (Prioritizing Scalability/Availability):
"You're right, a single relational database could be a bottleneck. If we anticipate millions of concurrent auctions with very high bid velocity, a horizontally scalable system like Cassandra might be a better fit. Here's how I'd approach that trade-off:

  • Benefit: Cassandra offers exceptional write throughput and horizontal scalability, which is perfect for handling a massive number of incoming bids. It's also highly available across multiple data centers.
  • Problem: Cassandra's default model is eventual consistency. To handle the strong consistency requirement for bids, we can't just do a simple write. We'd need to use 'Lightweight Transactions' (LWTs), which implement a Paxos-based compare-and-set operation.
  • Trade-off: Using LWTs in Cassandra provides the needed consistency but comes at a significant performance cost—it's much slower than a standard Cassandra write and adds latency. We are trading Cassandra's raw write performance for correctness.
  • Conclusion: So the decision is: Do we start with a simpler, consistent-by-default system like Postgres and scale it, or do we start with a scalable-by-default system like Cassandra and engineer consistency on top of it? Given the criticality of the bidding logic, I'd still lean towards starting with Postgres, but if the scale estimations showed it wouldn't be feasible, Cassandra with LWTs would be my next choice."


Conclusion

Today, we elevated your system design skills from describing a system to defending it. You learned that every significant architectural choice is a trade-off, and your ability to articulate that trade-off is what interviewers are looking for.

Key Takeaways:

  • Master the Vocabulary: Use terms like CAP, PACELC, consistency (strong/eventual), availability, partition tolerance, latency, and throughput correctly.
  • Ground Decisions in Numbers: Anchor your arguments with realistic latency figures. Knowing that cross-region replication adds ~80ms of latency instantly makes your arguments more credible.
  • There is No "Right" Answer: The best answer is a well-defended one. The goal is not to find a perfect solution, but to choose the best fit for the given requirements and be able to explain why other options are a poorer fit.
  • Follow the Defensibility Framework: For any major decision (e.g., choice of database, communication protocol):
    1. State the specific problem you are solving (e.g., "we need real-time bid updates").
    2. Propose 2-3 alternative solutions (e.g., "we could use polling, websockets, or SSE").
    3. Analyze the trade-offs of each in terms of scalability, latency, and cost.
    4. Select your preferred solution and justify why it best meets the specific requirements you identified at the start.

Next Lesson Preview

You have now completed the core system design modules focused on deconstruction and articulation. You're ready to put these skills to the test under pressure. However, elite interviews test both system design and algorithmic skills. In the next lesson, we will pivot back to algorithms and data structures to ensure you are sharp across the board. The topic will be: Execute a full-length mock interview focused on data structures and algorithms. Get ready for a hands-on coding challenge.