GCP Networking Fundamentals and API Gateway Design
This post covers two topics: manually creating VPC, Subnet, Firewall Rules, and Service Account on GCP, and API Gateway design concepts along with common interview questions.
GCP Networking
VPC: Global vs Regional
GCP VPC is global, Azure VNet is regional.
|
|
GCP’s advantage is that within the same VPC, VMs in different regions can communicate directly over the internal network without Peering.
VPC Subdivision and Address Ranges
Subnets are regional. When creating a Subnet, you specify the region and IP range (CIDR).
|
|
/24 means the last 8 bits are variable = 2^8 = 256 IPs (254 usable).
| CIDR | Usable IPs | Use case |
|---|---|---|
| /8 | 16,777,214 | Large private network |
| /16 | 65,534 | Medium VPC |
| /24 | 254 | Typical subnet |
| /28 | 14 | Small subnet (GCP minimum) |
| /32 | 1 | Single IP (firewall rule) |
/0 = all IPs. 0.0.0.0/0 means “any source”.
|
|
Firewall Rules
GCP Firewall Rules are attached at the VPC level, not the Subnet level (Azure NSG can be attached at Subnet or NIC).
Lower priority numbers mean higher priority. Default is 1000, highest is 0, lowest is 65535. GCP has an implied deny-all rule (priority 65535) โ any traffic that does not match any rule is blocked.
|
|
|
|
Production practice is --source-ranges=YOUR_IP/32, only allowing your own IP.
Service Account vs Managed Identity
A Service Account is an identity for programs or services, not for people.
| GCP Service Account | Azure Managed Identity | |
|---|---|---|
| Creation | Manually created, manually assigned | System-assigned auto-created with resource |
| Credentials | JSON key downloadable (but not recommended) | No key at all, Azure manages token lifecycle |
| Lifecycle | Exists independently, manually managed | System-assigned deleted when resource is deleted |
Azure’s design blocks the key path entirely, making it a more secure default. GCP’s SA key download is too convenient, making it a common source of security vulnerabilities.
The recommended replacement is Workload Identity Federation (WIF): let GitHub Actions or GKE Pods exchange OIDC tokens for GCP permissions. The entire flow requires no SA key.
|
|
SA + WIF = equivalent security to Managed Identity, but requires proactive setup.
|
|
Unified Request Front Door
Unified Entry Point
API Gateway is the unified entry point for all requests. It handles cross-cutting concerns so backend services only need to focus on business logic.
| Handled at Gateway | Backend services don’t need to |
|---|---|
| TLS termination | No need to handle HTTPS |
| Auth (JWT / API key) | No need for each service to validate tokens separately |
| Rate limiting | No need to block brute-force requests themselves |
| Caching | No need for each service to implement its own cache |
| Routing | Unified management of path โ service mapping |
TLS Offloading at the Edge
TLS handshake has CPU overhead. Let the Gateway handle SSL, while backend and Gateway communicate over HTTP on the internal network.
|
|
Benefits:
- Certificates only need to be managed at the Gateway, renewed in one place
- Backend services save the CPU cost of encryption/decryption
Gateway and backends are within the same VPC, so internal HTTP is usually acceptable. For stricter requirements (finance, healthcare), mTLS can be added to encrypt between backends as well.
Reply Store-and-Resend at the Perimeter
APIs with high read-to-write ratios and infrequently changing responses are suitable for caching at the Gateway layer.
|
|
Personalized responses (GET /cart, GET /orders) are not suitable for caching because each user’s data is different.
Dedicated Frontend Gateways
The more different clients’ API needs diverge, the more compromises appear when using a single Gateway for all clients. BFF (Backend for Frontend) pattern: maintain a separate Gateway for each client type.
|
|
Web BFF can do heavy data aggregation, Mobile BFF returns only compact fields, Public BFF enforces strict versioning. Backend microservices remain unchanged.
Suitable when: there are multiple clearly different client types, and each client team has the capacity to maintain its own BFF. For small teams or when client differences are minimal, a single unified Gateway is sufficient.
GraphQL offers an alternative: clients decide which fields they want, and a single endpoint serves all clients. The tradeoff is schema design complexity and the N+1 query problem.
Multi-Instance Topology and Resilience
|
|
Gateway is stateless โ routing rules are stored in config files or a DB. Any instance can handle any request, and horizontal scaling is natural.
Rate limiting is stateful: counters are stored in Redis, shared across all instances.
|
|
Without shared Redis, a user could hit three instances with 100 requests each, bypassing the 100 requests/minute limit.
Typical Interview Scenarios
Q: Is the Gateway a SPOF?
No. Deploy multiple instances behind a Load Balancer. Gateway is stateless โ any instance can handle any request โ so horizontal scaling is straightforward.
Q: How do you share rate limit state across instances?
Store rate limit counters in Redis. All Gateway instances read and write to the same Redis, so the limit is enforced globally regardless of which instance handles the request.
Q: What’s the difference between a Gateway and a Load Balancer?
LB distributes traffic at L4/L7. Gateway handles application-level concerns โ auth, rate limiting, routing logic. LB sits in front of the Gateway for HA; Gateway sits in front of your services for business logic.
Deterministic Routing Without Shared State
How do multiple LB instances make routing decisions without sharing state?
Round Robin needs a counter (“which one did I give last time”). Multiple instances each maintain their own counter, leading to unbalanced distribution.
Consistent Hashing uses a deterministic algorithm: the same input always produces the same output, without depending on any external state.
|
|
Any LB instance receiving the same IP computes the same result. No communication needed, no shared state required.
| Algorithm | Needs shared state | Reason |
|---|---|---|
| Round Robin | Yes | requires counter |
| Least Connections | Yes | requires connection count per backend |
| Random | No | stateless by nature |
| Consistent Hashing | No | deterministic function, same input = same output |