nuxt-modules / og-image

Generate OG Images with Vue templates in Nuxt.
https://nuxtseo.com/og-image
400 stars 25 forks source link

Cold starts with PM2 cluster mode #182

Closed tberk closed 2 weeks ago

tberk commented 6 months ago

Hello,

In my local setup, generating OG images typically takes less than 600ms. However, when deployed in production, I encounter a significant delay due to cold starts for each PM2 instance, lasting about 20 seconds.

Using PM2 in fork mode with a single instance mitigates this issue, resulting in just one cold start followed by rapid image generation. On the other hand, switching to cluster mode and scaling up to 16 instances introduces a cold start delay of approximately 20 seconds for each instance post-update.

Is this expected behavior in cluster mode, or are there strategies to minimize these cold starts?

Our production environment consists of Ubuntu, Node.js version 20, PM2 running in cluster mode, and Nginx.

ogImage config

ogImage: {
    defaults: {
      renderer: 'satori',      
      cacheMaxAgeSeconds: 60 * 60 * 24 * 3 * 1000, // 3 days      
    },
    compatibility: {
      dev: {
        chromium: false,
      },
      runtime: {
        chromium: false,
      },
    },
    runtimeCacheStorage: {
      driver: 'redis',
      host: process.env.NUXT_REDIS_HOST,
      port: 6379,
      ttl: 60 * 60 * 24 * 3,
      base: 'best-og',
      password: process.env.NUXT_REDIS_PASSWORD,
    },
  }
harlan-zw commented 6 months ago

I honestly have no idea about this infrastructure. The most likely area to explore is lazily importing the modules needed for OG Image creation (ReSVG, Satori, etc). This is already done in an environment that can only use the WASM build.

The nitro handlers should be lazily loaded so not too sure though.

I don't have the capacity to jump on this in the short term but will try and solve it when I have a chance. PRs welcome of course :)