Alokai
Features

Graceful Shutdown

How the Nuxt and Next.js storefront servers drain in-flight requests on SIGTERM, and the environment variables that control the delay and drain timeout.

When a storefront pod is descheduled, Kubernetes sends SIGTERM and keeps routing traffic to the pod until the load balancer notices it is gone. Without a controlled shutdown, requests that are still in flight - or that arrive in that window - fail with HTTP 502.

The Nuxt and Next.js storefront servers shut down gracefully on SIGTERM:

  1. The server keeps serving for a short delay, so the load balancer can stop sending it new requests.
  2. It stops accepting new connections and closes idle keep-alive connections.
  3. It waits for every in-flight request to finish.
  4. It exits with code 0. Requests still open when the drain timeout passes are closed, and the process exits anyway.

SIGINT is not affected: yarn dev and a local Ctrl+C still stop the server right away.

Prerequisites Next.js requires @vue-storefront/next and @alokai/cli from the release that introduced this feature, because the Docker image loads a preload script shipped by @vue-storefront/next. Nuxt requires the matching @vue-storefront/nuxt release.

Configuration

Both storefronts read the same environment variables, in milliseconds:

VariableDefaultDescription
ALOKAI_SHUTDOWN_DELAY_MS5000How long the server keeps accepting connections after SIGTERM.
ALOKAI_SHUTDOWN_DRAIN_TIMEOUT_MS15000How long in-flight requests get to finish before they are closed.

Keep ALOKAI_SHUTDOWN_DELAY_MS + ALOKAI_SHUTDOWN_DRAIN_TIMEOUT_MS below the pod's terminationGracePeriodSeconds (30 seconds by default). Past that, Kubernetes sends SIGKILL and the drain is cut anyway.

Invalid or negative values fall back to the defaults.

How it is wired

  • Nuxt: @vue-storefront/nuxt registers a Nitro server plugin. It turns off Nitro's own SIGTERM handling and runs the drain above, then calls Nitro's close hook. Nitro keeps handling SIGINT. Set NITRO_SHUTDOWN_DISABLED=1 to opt out.
  • Next.js: the image built by alokai deploy starts the server with node --require ./graceful-shutdown.cjs server.js. The preload comes from @vue-storefront/next/graceful-shutdown, sets NEXT_MANUAL_SIG_HANDLE=true to replace Next.js' built-in handler (which has no timeout and exits with code 143), and runs the drain. If you build your own image, copy that file next to server.js and keep the --require flag.

On this page