I'm successfully creating a WebSocket connection from a GCP Cloud Run instance to an external system. I've strictly followed the official GCP documentation and everything is working as expected.
However, the documentation also recommends using a Cloud Load Balancer in front of the Cloud Run service to initiate WebSocket connections. Unfortunately, it doesn't provide a clear explanation for why this is necessary.
Could someone please clarify the specific benefits or reasons for using a Cloud Load Balancer in this scenario? Are there any potential drawbacks or performance implications to consider when using a load balancer for WebSocket connections?
Any insights or explanations would be greatly appreciated.
Cloud Run only supports HTTPS connections
, it does not support raw TCP ports
. You could use an intermediary service to handle raw TCP
and translate those requests into HTTPS
. Theoretically a load balancer
could easily translate TCP
connections to one of those.
Cloud Run just supports HTTP
, WebSockets and gRPC and not TCP
. You can have Cloud Run communicate to other GCP services in the VPC
by using a Serverless VPC Connector
or expose it with an internal IP address
behind a load balancer.
You can also use Load balancer
and Google provided Cloud Armor
to protect against the DDoS
attacks without compromising the scaling of Cloud Run
.
If the resource is a Cloud VPN gateway
with users or on-prem services
unable to access the service (despite being on the same project and/or VPC perimeter), one can use Private Service Connect or Internal Load Balancer
.
Websocket
connections can be up for a long time. Backend service timeout behaves differently for HTTP and TCP/SSL Proxy
. If you want to have very long Websocket connections
, then you should use Network Load Balancing
.
Overall GCP Load Balancer
can significantly improve the availability, scalability, and security of your WebSocket connections
from Cloud Run
. On the other hand while the Load Balancer
adds a small amount of latency, its benefits generally outweigh this minor drawback.
Refer to Cloud Load Balancing: A comprehensive solution for secure and private access to Cloud Run services and for examples check Deploy a secured serverless architecture using Cloud Run for more information.