Professional Cloud Developer free question 1 / 10 · Domain 1: Designing highly scalable, secure, and reliable cloud-native applications
A team keeps each visitor's shopping cart in the memory of its Cloud Run instances and turns on session affinity for the service. Which statement should the developers rely on when they design the cart code?
- Affinity has to be switched on in the load balancer's serverless network endpoint group, or the Cloud Run setting has no effect
- Each client is bound to an instance that serves nobody else, so cart data from different visitors stays apart in memory
- A returning client is usually sent to the same instance, but affinity can break, so the cart must also survive a move to another instance
- Cloud Run keeps routing a client to the same instance until the affinity cookie expires, even if that instance is terminated or busy
Show the answer and explanation
Answer: C
Why this is correct
Cloud Run session affinity routes sequential requests from a client to the same revision instance by using a session affinity cookie. The page calls this best effort affinity because Cloud Run still autoscales: if the instance is terminated for any reason, or reaches maximum request concurrency or maximum CPU utilization, later requests are routed elsewhere. It adds that you can cache client session data in memory, but you cannot assume that a client will always reconnect to the same instance.
Why the other options are wrong
"Each client is bound to an instance that serves nobody else, so cart data from different visitors stays apart in memory" … The page says an instance can receive requests from different clients. Session affinity does not make the instance dedicated to one client, so per-client data still has to be keyed by client.
"Cloud Run keeps routing a client to the same instance until the affinity cookie expires, even if that instance is terminated or busy" … Affinity is best effort. If the instance is terminated, or reaches maximum request concurrency or maximum CPU utilization, affinity is broken and requests go to a different instance.
"Affinity has to be switched on in the load balancer's serverless network endpoint group, or the Cloud Run setting has no effect" … The page describes the two implementations as separate and independent, and says Cloud Load Balancing session affinity is not supported on a serverless network endpoint group.
Key point
Treat in-memory data behind session affinity as a cache. You can cache client session data in memory, but you cannot assume that a client will always reconnect to the same instance.