0
0
GCPcloud~10 mins

TCP/UDP Load Balancer (Layer 4) in GCP - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - TCP/UDP Load Balancer (Layer 4)
Client sends TCP/UDP request
Load Balancer receives request
Check backend service health
Select healthy backend instance
Forward request to backend instance
Backend instance processes request
Response sent back through Load Balancer
Client receives response
The load balancer receives TCP/UDP requests from clients, checks backend health, forwards requests to healthy backends, and returns responses to clients.
Execution Sample
GCP
resource "google_compute_forwarding_rule" "tcp_udp_lb" {
  name       = "tcp-udp-lb-rule"
  load_balancing_scheme = "EXTERNAL"
  port_range = "80-81"
  target     = google_compute_target_pool.tp.id
  ip_protocol = "TCP"
}

resource "google_compute_target_pool" "tp" {
  name = "target-pool"
  instances = [google_compute_instance.instance1.id]
}
Defines a TCP load balancer forwarding rule that listens on ports 80-81 and forwards traffic to a target pool of backend instances.
Process Table
StepActionInputLoad Balancer DecisionBackend Response
1Client sends TCP requestClient IP: 10.0.0.1, Port: 80Receive request on port 80No response yet
2Load Balancer checks backend healthTarget pool instances health statusInstance1 is healthyNo response yet
3Load Balancer selects backendHealthy instances: Instance1Select Instance1No response yet
4Forward request to backendRequest forwarded to Instance1ForwardedProcessing request
5Backend processes requestRequest received by Instance1N/AResponse generated
6Response sent backResponse from Instance1Forward response to clientClient receives response
7End of request cycleNo new requestsIdle, waiting for next requestNo response
💡 Request cycle ends after response is sent back to client.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6Final
Client RequestNoneReceivedReceivedForwardedForwardedNone
Backend HealthUnknownHealthyHealthyHealthyHealthyHealthy
Selected BackendNoneNoneInstance1Instance1Instance1None
ResponseNoneNoneNoneProcessingSentNone
Key Moments - 3 Insights
Why does the load balancer check backend health before forwarding?
To avoid sending requests to unhealthy instances, ensuring reliable service. See execution_table step 2 where health is checked before selection.
What happens if no backend instances are healthy?
The load balancer cannot forward requests, so clients may get errors or timeouts. This is implied by the health check step in execution_table step 2.
Does the load balancer modify the TCP/UDP payload?
No, it only forwards packets at Layer 4 without changing payload content, as shown by forwarding actions in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the load balancer select the backend instance?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Load Balancer Decision' column in execution_table row for step 3.
According to variable_tracker, what is the state of 'Response' after step 4?
ANone
BProcessing
CSent
DForwarded
💡 Hint
Look at the 'Response' row and the column 'After Step 4' in variable_tracker.
If the backend health was unhealthy at step 2, what would change in the execution_table?
ALoad balancer would select a different backend at step 3
BRequest would be forwarded anyway at step 4
CLoad balancer would not forward the request at step 4
DClient would receive response immediately at step 6
💡 Hint
Refer to key_moments about backend health and execution_table step 2 and 4.
Concept Snapshot
TCP/UDP Load Balancer (Layer 4) forwards client requests based on IP and port.
It checks backend instance health before forwarding.
Does not modify packet content, only routes traffic.
Commonly used for TCP/UDP protocols on specified ports.
In GCP, uses forwarding rules and target pools.
Ensures high availability by selecting healthy backends.
Full Transcript
A TCP/UDP Load Balancer works at Layer 4 of the network. It receives client requests on specific ports and forwards them to backend instances. Before forwarding, it checks if backend instances are healthy to avoid sending traffic to unhealthy servers. The load balancer does not change the content of the packets; it simply routes them. After the backend processes the request, the response is sent back through the load balancer to the client. This process ensures reliable and efficient distribution of network traffic. In Google Cloud Platform, this is configured using forwarding rules that listen on ports and target pools that group backend instances.