<p>I'm making a realtime multiplayer game and I have an idea on how to structure the backend, but I don't even know if its possible, let alone how to build and deploy it. Basically I want to deploy my backend as a container to cloud run but instead of syncing data through a database common to all instances I want to store the game data locally in each instance and just connect players in the same game to the same instance. Essentially I would need a custom load balancer and scaling logic. I haven't been able to find any useful material on this topic so any help/input is greatly appreciated.</p>

Ben Baldwin
<p>This is possible to do on Kubernetes, but I doubt that this can be easily done on Google Cloud Run. Your game-pod is essentially <em>stateful</em> workload and does not fit well on Google Cloud Run, but you can run this on Kubernetes as <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a>.</p>
<blockquote>
<p>Essentially I would need a custom load balancer and scaling logic.</p>
</blockquote>
<p>Yes. For this you need to use an <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/service-parameters" rel="nofollow noreferrer">UDP/TCP load balancer</a> and you most likely need to deploy <a href="https://stackoverflow.com/questions/44601191/kubernetes-on-gce-ingress-timeout-configuration/59671993#59671993">custom configuration to allow longer connections</a> than default. This network load balancer will forward traffic to your <em>custom load balancer</em> - probably running as a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">Deployment</a>.</p>
<h2>Custom Load Balancer - Sharding Proxy</h2>
<p>What you need as <em>custom load balancer</em> is a <a href="https://stackoverflow.com/questions/58675990/sharded-load-balancing-for-stateful-services-in-kubernetes/58678622#58678622">Sharding Proxy</a> that can forward traffic to correct Game Session Pod and potentially also to scale up to more Game Session Pods. See the answer to <a href="https://stackoverflow.com/questions/58675990/sharded-load-balancing-for-stateful-services-in-kubernetes/58678622#58678622">Sharded load balancing for stateful services in Kubernetes</a> for more info on this.</p>
<h2>Game Session App</h2>
<p>The app that handles the Game Session need to be deployed as a <code>StatefulSet</code> so that they get an <em>unique network identity</em> e.g. <code>game-session-0</code>, <code>game-session-1</code> and <code>game-session-2</code> - so that your <em>custom load balancer</em> can use logic to direct the traffic to correct Game Session Pod.</p>
<blockquote>
<p>I want to store the game data locally in each instance and just connect players in the same game to the same instance.</p>
</blockquote>
<p>This is possible, use a volume of type <a href="https://kubernetes.io/docs/concepts/storage/volumes/#emptydir" rel="nofollow noreferrer">emptyDir</a>.</p>
<h2>Game Server Example on GKE</h2>
<p>For an extensive example of this type of deployment, see <a href="https://cloud.google.com/solutions/gaming/running-dedicated-game-servers-in-kubernetes-engine" rel="nofollow noreferrer">Running dedicated game servers in GKE</a></p>
