🖥️
Sunil Notebook
Interview Preparation
  • 📒Notebook
    • What is this about ?
  • System Design
    • 💡Key Concepts
      • 🌐Scalability
      • 🌐Latency Vs Throughput
      • 🌐Databases
      • 🌐CAP Theorem
      • 🌐ACID Transactions
      • 🌐Rate limiting
      • 🌐API Design
      • 🌐Strong Vs eventual consistency
      • 🌐Distributed tracing
      • 🌐Synchronous Vs asynchronous Communication
      • 🌐Batch Processing Vs Stream Processing
      • 🌐Fault Tolerance
    • 💎Building Blocks
      • 🔹Message
      • 🔹Cache
      • 🔹Load Balancer Vs API Gateway
    • 🖥️Introduction to system design
    • ⏱️Step By Step Guide
    • ♨️Emerging Technologies in System Design
    • ☑️System design component checklist
      • 🔷Azure
      • 🔶AWS
      • ♦️Google Cloud
    • 🧊LinkedIn feed Design
    • 🏏Scalable Emoji Broadcasting System - Hotstar
    • 💲UPI Payment System Design
    • 📈Stock Broker System Design - Groww
    • 🧑‍🤝‍🧑Designing Instagram's Collaborative Content Creation - Close Friends Only
    • 🌳Vending Machines - Over the air Systems
    • Reference Links
  • DSA
    • Topics
      • Introduction
      • Algorithm analysis
        • Asymptotic Notation
        • Memory
      • Sorting
        • Selection Sort
        • Insertion Sort
        • Merge Sort
        • Quick Sort
        • Quick'3 Sort
        • Shell Sort
        • Shuffle sort
        • Heap Sort
        • Arrays.sort()
        • Key Points
        • Problems
          • Reorder Log files
      • Stacks and Queues
        • Stack Implementations
        • Queue Implementations
        • Priority Queues
        • Problems
          • Dijkstra's two-stack algorithm
      • Binary Search Tree
        • Left Leaning Red Black Tree
          • Java Implementations
        • 2-3 Tree
          • Search Operation - 2-3 Tree
          • Insert Operation - 2-3 Tree
        • Geometric Applications of BST
      • B-Tree
      • Graphs
        • Undirected Graphs
        • Directed Graphs
        • Topological Sort
      • Union Find
        • Dynamic Connectivity
        • Quick Find - Eager Approach
        • Quick Find - Lazy Approach
        • Defects
        • Weighted Quick Union
        • Quick Union + path comparison
        • Amortized Analysis
      • Convex Hull
      • Binary Heaps and Priority Queue
      • Hash Table vs Binary Search Trees
  • Concurrency and Multithreading
    • Introduction
    • Visibility Problem
    • Interview Questions
    • References
      • System design
  • Design Patterns
    • ℹ️Introduction
    • 💠Classification of patterns
    • 1️⃣Structural Design Patterns
      • Adapter Design Pattern
      • Bridge Design Pattern
      • Composite Design Pattern
      • Decorator Design Pattern
      • Facade Design Pattern
      • Flyweight Design Pattern
      • Private Class Data Design Pattern
      • Proxy Design Pattern
    • 2️⃣Behavioral Design Patterns
      • Chain Of Responsibility
      • Command Design Pattern
      • Interpreter Design Pattern
      • Iterator Design Pattern
      • Mediator Design Pattern
      • Memento Design Pattern
      • Null Object Design Pattern
      • Observer Design Pattern
      • State Design Pattern
      • Strategy Design Pattern
      • Template Design Pattern
    • 3️⃣Creational Design Patterns
      • Abstract Factory Design Pattern
      • Builder Design Pattern
      • Factory Method Design Pattern
      • Object Pool Design Pattern
      • Prototype Design Pattern
      • Singleton Design Pattern
    • Java Pass by Value or Pass by Reference
  • Designing Data-Intensive Applications - O'Reilly
    • Read Me
    • 1️⃣Reliable, Scalable, and Maintainable Applications
      • Reliability
      • Scalability
      • Maintainability
      • References
    • 2️⃣Data Models and Query Languages
      • Read me
      • References
    • Miscellaneous
  • Preparation Manual
    • Disclaimer
    • What is it all about?
    • About a bunch of links
    • Before you start preparing
    • Algorithms and Coding
    • Concurrency and Multithreading
    • Programming Language and Fundementals
    • Best Practices and Experience
  • Web Applications
    • Typescript Guidelines
  • Research Papers
    • Research Papers
      • Real-Time Data Infrastructure at Uber
      • Scaling Memcache at Facebook
  • Interview Questions
    • Important links for preparation
    • Google Interview Questions
      • L4
        • Phone Interview Questions
      • L3
        • Interview Questions
      • Phone Screen Questions
  • Miscellaneous
    • 90 Days Preparation Schedule
    • My Preparation for Tech Giants
    • Top Product Based Companies
  • Links
    • Github
    • LinkedIn
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. System Design
  2. Building Blocks

Load Balancer Vs API Gateway

In the high-level overview of the system design, both these blocks look the same. But they are not. Let me tell you.

Imagine you organized the big party and had the different food counters. Assume Pizza counter is one of them. At t0, you have one pizza counter in the food arena. Over the time more audience start reaching the party, and you added the one more pizza counter to the food arena. During the peek time more people started eating pizza now you pizza counter owner can't control the crowd and keeps one person near the counters and redirects to the people to the counter having the less number of people.

Now lets see the balcony view, pizza owner what he did was he identified the increase in the load increased the number of orders and number of people staying in the queue. Now he assigned one balancer to distribute the load based on the queue size in the respective counter.

Different Techniques with respect to the pizza counter:

  1. Round Robin - The balancer sends each new guest to the next counter in a cyclic order, without considering the current queue length.

    Analogy with Pizza counter: If there are three counters, the first guest goes to Counter 1, the second to Counter 2, the third to Counter 3, and the fourth back to Counter 1.

  2. Least Connections -The balancer sends new guests to the counter with the fewest people currently in the queue.

    Analogy with Pizza counter: If Counter 1 has 3 people, Counter 2 has 2, and Counter 3 has 1, the next guest will be directed to Counter 3.

  3. Least Response Time- The balancer sends guests to the counter that is currently serving people the fastest, indicating it can handle more load quickly.

    Analogy with Pizza counter: If Counter 1 typically serves a pizza in 2 minutes, Counter 2 in 3 minutes, and Counter 3 in 1 minute, the balancer will prioritize Counter 3 for new guests.

  4. IP Hash - Each guest has a unique identifier, and the balancer always directs the same guest to the same counter based on this identifier.

    Analogy with Pizza counter: If a guest's identifier (like a name tag) hashes to Counter 2, they will always be directed to Counter 2 whenever they return for more pizza.

  5. Weighted Round Robin- Some counters are more capable (larger staff or faster ovens) and can handle more guests, so the balancer sends more guests to these counters based on their capacity.

    Analogy with Pizza counter: If Counter 1 can handle twice as many orders as Counter 2, the balancer will send two guests to Counter 1 for every one guest sent to Counter 2.

  6. Weighted Least Connections - The balancer sends guests to the counter with the fewest people, but also considers the capacity of each counter.

    Analogy with Pizza counter: If Counter 1 (higher capacity) has 5 people, Counter 2 (lower capacity) has 3 people, and Counter 3 (medium capacity) has 2 people, the balancer may still direct guests to Counter 3 based on its capacity to handle more load efficiently

Key features:

  • Traffic Distribution: Balances traffic across multiple servers.

  • Scalability: Helps scale horizontally by adding more servers.

  • High Availability: Increases availability by routing traffic to healthy servers.

  • Health Checks: Monitors the health of servers and directs traffic to healthy ones.

Operates at Transport layer and application layer ( Layer 4 and Layer 7 ) of the OSI Model

Now let's look into the api gateway.

Imagine you want to order the items required for a big party like pizza, ballons, cakes. Instead of you calling all the stores for the items. Find one person who can do all the stuff for you and they will handle everything for you. That special guy is like the api gateway. An API Gateway is a helper that takes all the requests from people (like ordering party supplies) and sends them to the right places.

Key Features:

  • Request Routing: Routes API requests to appropriate microservices.

  • Protocol Translation: Translates protocols (e.g., HTTP to WebSocket).

  • Authentication and Authorization: Enforces security policies for incoming requests.

  • Rate Limiting and Throttling: Controls the rate of requests to prevent abuse.

  • Caching: Provides caching capabilities to improve performance.

  • Analytics and Monitoring: Offers detailed logging and monitoring of API usage.

Operates primarily at Layer 7 (Application) of the OSI model.

Let me add all the differences in the table for you,

Feature
Load Balancer
API Gateway

Primary Function

Distributes traffic to multiple servers

Manages and routes API requests

Layer of Operation

Layer 4 (Transport) and Layer 7 (Application)

Layer 7 (Application)

Traffic Distribution

Yes

No

Request Routing

Basic routing

Advanced routing based on API endpoints

Protocol Translation

No

Yes

Authentication & Authorization

No

Yes

Rate Limiting & Throttling

No

Yes

Caching

Limited

Yes

Health Checks

Yes

No

SSL Termination

Yes

Yes

Session Persistence

Yes

No

Scalability

Horizontal scaling by adding more servers

Facilitates scaling by managing API traffic

High Availability

Yes

Yes

Analytics & Monitoring

Basic (server health, traffic distribution)

Detailed (API usage, performance metrics)

Complexity

Generally simpler

More complex

Use Case Context

General traffic distribution for web servers, databases, etc.

Specifically for microservices architectures

PreviousCacheNextIntroduction to system design

Last updated 9 months ago

Was this helpful?

💎
🔹
Page cover image