Skip to content

Commit c41e1aa

Browse files
committed
Add system design LB, reverse proxy, api gateway
1 parent 5631173 commit c41e1aa

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,9 @@ Here is a template I use for the system design interview:
680680
- Deep dive on any of the following component
681681
- DNS
682682
- CDN (Pull vs Push vs Hybrid)
683-
- Load Balancer (Round Robin, Weighted Round Robin, Consistent Hash, Active-Passive, Active-Active, Layer 4, Layer 7)
684-
- Reverse Proxy
683+
- Load Balancer/Reverse Proxy
684+
- LB types
685+
- LB algorithms
685686
- Application layer scaling (Microservices, Service Discovery, Service Mesh)
686687
- Database (RDBMS vs NoSQL)
687688
- RDBMS:
@@ -749,7 +750,12 @@ Network systems will eventually comes down to these components and design patter
749750
- socket, websocket, long-polling
750751
- REST, SOAP
751752
- HTTP response status codes
752-
- Load Balancer
753+
- [Load Balancer, Reverse Proxy, API Gateway](./SystemDesign/load_balancer.md)
754+
- LB types: layer 4 and layer 7
755+
- LB algorithms: least connection, least response time, least bandwidth, round robin, IP hash
756+
- Reverse Proxy
757+
- API Gateway
758+
- An example: The Architecture of Uber’s API gateway
753759
- [Cache](./SystemDesign/cache.md)
754760
- Cache Usage Pattern
755761
- Cache Aside

SystemDesign/load_balancer.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Load Balancer
2+
3+
- A load balancer distributes incoming client requests among a group of servers, in each case returning the response from the selected server to the appropriate client.
4+
- A load balancer can also enhance the user experience by reducing the number of error responses the client sees.
5+
- Session persistence (sending all requests from a particular client to the same server) is also available for some load balancers
6+
7+
## Types of Load Balancers: Layer 4 and Layer 7
8+
9+
Layer 4 load balancing:
10+
11+
- “Layer 4 load balancing” most commonly refers to a deployment where the load balancer’s IP address is the one advertised to clients for a web site or service (via DNS, for example)
12+
- Layer 4 load balancing operates at the intermediate transport layer, which deals with delivery of messages with no regard to the content of the messages.
13+
- When it receives a request and makes the load balancing decision, it also performs Network Address Translation (NAT) on the request packet, changing the recorded destination IP address from its own to that of the content server it has chosen on the internal network
14+
- Before forwarding server responses to clients, the load balancer changes the source address recorded in the packet header from the server’s IP address to its own.
15+
- Layer 4 load balancing was a popular architectural approach to traffic handling when commodity hardware was not as powerful as it is now, and the interaction between clients and application servers was much less complex.
16+
17+
Layer 7 load balancing:
18+
19+
- Layer 7 load balancing operates at the high‑level application layer, which deals with the actual content of each message.
20+
- HTTP is the predominant Layer 7 protocol for website traffic on the Internet.
21+
- It terminates the network traffic and reads the message within.
22+
- It can make a load‑balancing decision based on the content of the message (the URL or cookie, for example).
23+
- It then makes a new TCP connection to the selected upstream server (or reuses an existing one, by means of HTTP keepalives) and writes the request to the server.
24+
- Layer 7 load balancing is more CPU‑intensive than packet‑based Layer 4 load balancing
25+
26+
## Load Balancing Algorithms
27+
28+
- Least connection
29+
- Selects the server with least active connection
30+
- Weighted Least Connection
31+
- Similar to least connection but with weight
32+
- Least response time
33+
- Selects the server with least response time
34+
- Weighted Least response time
35+
- Similar to least response time but with weight
36+
- Least bandwidth
37+
- Selects the server with least bandwidth
38+
- Round robin
39+
- Checks each server one by one
40+
- Weighted round-robin
41+
- Checks each server one by one based one the weight that admin assigns on the server
42+
- IP hash
43+
- combines source and destination IP addresses of the client and server to generate a unique hash key, which is used to allocate the client to a particular server.
44+
- the client request is directed to the same server it was using previously.
45+
46+
# Reverse Proxy
47+
48+
- A reverse proxy accepts a request from a client, forwards it to a server that can fulfill it, and returns the server’s response to the client.
49+
- Increased security – No information about your backend servers is visible outside your internal network
50+
- Has security features to help protect backend servers from distributed denial-of-service (DDoS) attacks (i.e. IP address blacklisting, etc)
51+
- Increased scalability and flexibility – Because clients see only the reverse proxy’s IP address, you are free to change the configuration of your backend infrastructure.
52+
- Increased performance on response time
53+
- Compression (of server responses to reduce bandwidth)
54+
- SSL termination (Encryption)
55+
- Caching (server response for same requests)
56+
57+
# API Gateway
58+
59+
- An API Gateway is the element that coordinates and orchestrates how all the requests are processed in a Microservices architecture
60+
- An API Gateway includes an HTTP server where routes are associated with a Microservice or with a FaaS function
61+
- When an API Gateway receives a request, it looks up for the Microservice which can serve the request and delivers it to the relevant part.
62+
- Besides this pure routing task, an API gateway can also be the part that performs **authentication**, **input validation**, **load balancing** and **centralized middleware** functionality, among other tasks.
63+
- It often makes sense to deploy a reverse proxy even with just one web server or application server.
64+
- Drawbacks for API gateway are:
65+
- It creates a tight coupling between the client and the backend.
66+
- It has limited choice of communication protocols for services.
67+
- It could becomes a bottleneck for your application
68+
69+
## An example: The Architecture of Uber’s API gateway
70+
71+
Components in a request lifecycle:
72+
73+
1. **Protocol manager**: provides the ability to implement APIs that can ingest any type of relevant protocol payload
74+
2. **Middleware**: implements composable logic before the endpoint handler is invoked
75+
- Middleware implements cross-cutting concerns, such as authentication, authorization, rate limiting, circuit breaking, etc.
76+
3. **Endpoint handler**: responsible for request validation, payload transformation, and converting the endpoint request object to the client request object.
77+
4. **Client**: performs a request to a back-end service
78+
79+
80+
# Reference:
81+
82+
- [System Design: What is Load Balancing? - YouTube](https://www.youtube.com/watch?v=gMIslJN44P0&ab_channel=BeABetterDev)
83+
- [System Design — Load Balancing. Concepts about load balancers and… \| by Larry | Peng Yang | Computer Science Fundamentals | Medium](https://medium.com/must-know-computer-science/system-design-load-balancing-1c2e7675fc27)
84+
- [What Is Layer 4 Load Balancing? \| NGINX Load Balancer](https://www.nginx.com/resources/glossary/layer-4-load-balancing/)
85+
- [Benefits of Layer 7 Load Balancing \| NGINX Load Balancer](https://www.nginx.com/resources/glossary/layer-7-load-balancing/)
86+
- [Load Balancing Algorithms, Types and Techniques](https://kemptechnologies.com/load-balancer/load-balancing-algorithms-techniques/)
87+
- [What is a Proxy? \| System Design - YouTube](https://www.youtube.com/watch?v=xiUmXVcLdCw&ab_channel=BeABetterDev)
88+
- [What is a Reverse Proxy vs. Load Balancer? - NGINX](https://www.nginx.com/resources/glossary/reverse-proxy-vs-load-balancer/)
89+
- [Stupid question of the day: What is an API Gateway and what it has to do with a Serverless model? \| by Gabry Martinez | Medium](https://gabrymartinez.medium.com/stupid-question-of-the-day-what-is-an-api-gateway-and-what-it-has-to-do-with-a-serverless-model-2acee3e3eeba)
90+
- [What is API Gateway?. In microservices architecture, there… \| by Vivek Kumar Singh | System Design Blog | Medium](https://medium.com/system-design-blog/what-is-api-gateway-68a11d4ab322)
91+
- [The Architecture of Uber's API gateway \| Uber Engineering Blog](https://eng.uber.com/architecture-api-gateway/)

0 commit comments

Comments
 (0)