An API Gateway is a server that acts as a single entry point for all client requests to multiple backend services. It handles routing, authentication, rate limiting, and sometimes response aggregation.
Click to reveal answer
beginner
Name two main responsibilities of an API Gateway.
1. Routing client requests to the correct backend service. 2. Handling security like authentication and authorization.
Click to reveal answer
intermediate
Why use an API Gateway instead of direct client-to-service communication?
It simplifies client interactions by providing one endpoint, improves security by centralizing access control, and can improve performance by caching and aggregating responses.
Click to reveal answer
intermediate
How does an API Gateway help with scalability?
It can balance load across multiple backend services and apply rate limiting to protect services from overload, helping the system scale smoothly.
Click to reveal answer
advanced
What is a common challenge when using an API Gateway?
It can become a single point of failure or a bottleneck if not designed or scaled properly.
Click to reveal answer
What role does an API Gateway play in a microservices architecture?
AActs as a single entry point for client requests
BStores all user data
CReplaces backend services
DManages database transactions
✗ Incorrect
An API Gateway acts as a single entry point that routes client requests to the appropriate backend services.
Which of the following is NOT a typical function of an API Gateway?
ADirect database management
BAuthentication and authorization
CRequest routing
DRate limiting
✗ Incorrect
API Gateways do not manage databases directly; they route requests and handle security and traffic control.
How does an API Gateway improve security?
ABy encrypting backend databases
BBy replacing firewalls
CBy storing user passwords
DBy centralizing authentication and authorization
✗ Incorrect
API Gateways centralize security by managing authentication and authorization for all incoming requests.
What is a potential risk of using an API Gateway?
AIt increases client complexity
BIt can slow down the database
CIt can become a single point of failure
DIt eliminates the need for backend services
✗ Incorrect
If not properly designed or scaled, the API Gateway can become a single point of failure.
Which feature helps an API Gateway protect backend services from too many requests?
ACaching
BRate limiting
CLoad balancing
DData encryption
✗ Incorrect
Rate limiting controls how many requests a client can make, protecting backend services from overload.
Explain the role and benefits of an API Gateway in a microservices system.
Think about how clients communicate with many services and how the gateway helps.
You got /5 concepts.
Describe common challenges when implementing an API Gateway and how to address them.
Consider what happens if the gateway goes down or gets overloaded.
You got /5 concepts.
Practice
(1/5)
1. What is the primary role of an API gateway in a system using Express?
easy
A. It directly serves static files to users.
B. It stores all the data for the application.
C. It acts as a single entry point to route requests to multiple backend services.
D. It replaces the database in the backend.
Solution
Step 1: Understand the role of API gateway
An API gateway acts as a single entry point that routes client requests to appropriate backend services.
Step 2: Eliminate incorrect roles
Storing data, replacing databases, or serving static files are not responsibilities of an API gateway.
Final Answer:
It acts as a single entry point to route requests to multiple backend services. -> Option C
Quick Check:
API gateway = single entry point [OK]
Hint: API gateway routes requests centrally [OK]
Common Mistakes:
Thinking API gateway stores data
Confusing API gateway with database
Assuming it serves static files directly
2. Which Express middleware is commonly used to forward requests in an API gateway setup?
easy
A. http-proxy-middleware
B. express.static()
C. body-parser
D. cors
Solution
Step 1: Identify middleware for proxying requests
The http-proxy-middleware package is used to forward requests from the gateway to backend services.
Step 2: Differentiate from other middleware
express.static() serves static files, body-parser parses request bodies, and cors manages cross-origin requests, none forward requests.
Final Answer:
http-proxy-middleware -> Option A
Quick Check:
Proxy middleware = http-proxy-middleware [OK]
Hint: Proxy requests with http-proxy-middleware [OK]
Common Mistakes:
Using express.static() to proxy requests
Confusing body-parser with proxying
Assuming cors forwards requests
3. Given this Express API gateway code snippet, what will be the output when a client requests /api/users?
A. Missing changeOrigin: true causes backend to reject requests
B. The target URL is incorrect and unreachable
C. The middleware is not mounted on the correct path
D. Express app is not listening on port 3000
Solution
Step 1: Check proxy middleware options
Without changeOrigin: true, the Host header sent to backend remains the gateway's, which may cause backend to reject requests.
Step 2: Verify other parts
The target URL and path are correct, and app listens on port 3000, so those are not errors.
Final Answer:
Missing changeOrigin: true causes backend to reject requests -> Option A
Quick Check:
changeOrigin fixes Host header issues [OK]
Hint: Add changeOrigin: true to fix backend rejection [OK]
Common Mistakes:
Ignoring changeOrigin option
Assuming wrong target URL without checking
Thinking app.listen is missing
5. You want to design an Express API gateway that routes requests to multiple backend services and also logs each request's method and URL. Which approach best fits this requirement?
hard
A. Use only express.static() to serve all backend services and log requests.
B. Use http-proxy-middleware for routing and add a custom middleware before it to log requests.
C. Implement routing logic inside each backend service and skip the gateway.
D. Use body-parser middleware to route and log requests.
Solution
Step 1: Identify routing and logging needs
The API gateway must route requests to multiple backends and log method and URL of each request.
Step 2: Choose appropriate middleware
http-proxy-middleware handles routing, and a custom Express middleware can log requests before proxying.
Step 3: Eliminate incorrect options
express.static() serves static files only, backend routing skips gateway, and body-parser parses bodies but does not route or log.
Final Answer:
Use http-proxy-middleware for routing and add a custom middleware before it to log requests. -> Option B