NextJS and Kubernetes - router.push is sending the request to another POD instead of using the same

49 Views Asked by At

I have a NextJS app deployed on a Kubernetes Cluster. My app handles redirects (Same website, different path) based on business logic and i was wondering if there is a possibility to handle that "redirect" (im using router.push from useRouter) in the same pod.

What happens is that when the first request comes in, POD#1 handle it, when a redirect is detected, then the logic fires router.push and that redirect its been handled by POD#2.

Schema

1

There are 1 best solutions below

0
François On

What you could do is to configure on your load balancer "sticky sessions" to make sure requests from a client goes to the same node.

And then using anti-affinity rules, make sure you don't deploy more than 1 pod of your app per node

Anyway, make sure your requests are stateless as possible, except for some caching optimisation it shouldn't matter which pod the request hits.

Documentation references