Api Gateway hard
Scaling WebSocket connections at the Gateway layer is challenging because a TCP connection is 'Stateful' (bound to a specific server instance). How do you route a message to a user who is connected to Gateway A from a service that talks to Gateway B?
- A By using IP Broadcast to send the message to every Gateway instance in the VPC.
- B Through a Pub/Sub Backplane (e.g., Redis or Kafka): All Gateways subscribe to a message bus. When a message for 'User 1' arrives at any Gateway, it is published to the bus, and only the instance holding User 1's socket picks it up.
- C By forcing the user's browser to connect to every Gateway instance simultaneously for redundancy.
- D By moving the physical TCP socket from Gateway A to Gateway B using the 'BGP Hijacking' technique.
Answer: B. Through a Pub/Sub Backplane (e.g., Redis or Kafka): All Gateways subscribe to a message bus. When a message for 'User 1' arrives at any Gateway, it is published to the bus, and only the instance holding User 1's socket picks it up.
Since a WebSocket is a persistent pipe to one machine, you cannot 'load balance' individual messages easily. The Backplane creates a 'Virtual Network' where any backend service can send a message to any user, regardless of which particular Gateway instance the user is physically tethered to.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- Your Gateway communicates with backends via HTTP/1.1 connections. A slow backend service causes 'Head-of-Line Blocking'. How does switching to HTTP/2 as the Gateway-to-Backend transport mitigate this?
- What is the primary architectural role of an API Gateway in a microservices environment?
- Why is 'Stateless Token Validation' (e.g., using JWTs) significantly more scalable at the Gateway level than 'Stateful Validation' (e.g., using Opaque/Reference Tokens)?
- Your Gateway is configured to cache any URL ending in `.css` or `.jpg`. An attacker tricks a victim into visiting `https://api.com/v1/profile/info/style.css`. How can this lead to an 'Account Takeover'?
- An API is receiving a sudden surge of 10,000 requests per second from a single IP address. How does an API Gateway preserve the stability of the backend services?
- How can an API Gateway help prevent 'Double Charging' in a payment system when a mobile client retries a failed request due to a poor connection?