9

we're running Next.js in Docker.

When building the image locally and using the production backend, TTFB is virtually zero, but when we deploy this image, TTFB can be up to 30 seconds in some cases.

The production servers are warm servers that don't need to spin up.

I ran four tests to measure TTFB against the same route with the same production backend.

  1. Locally in production mode with docker
  2. Locally in production mode with yarn build && yarn start
  3. Locally in dev mode with yarn run dev
  4. On production

When testing production mode both in and out of docker, TTFB is near zero.

When testing locally in dev mode, TTFB is nearly identical to production.

I've SSH'd into production and confirmed that dev packages are not being installed, so I don't think it's possible that we're running in dev mode.

I would expect production TTFB to be closer to what we're experiencing locally.

What all could be causing the discrepancies between local and production?

Dockerfile

FROM node:10.13.0-alpine

WORKDIR /app

EXPOSE 8080

CMD [ "yarn", "start" ]

COPY . .

RUN yarn install

RUN yarn build

server.js snippet

const express = require('express');
const next = require('next');
const cookieParser = require('cookie-parser');
const jwtDecode = require('jwt-decode');
const { join } = require('path');
const admin = require('firebase-admin');

// serviceAccount used for local development
// https://firebase.google.com/docs/admin/setup
const path = require('path');
const { fetchNewToken } = require('./src/services/Firebase');

let serviceAccount;
const dev = process.env.NODE_ENV !== 'production';
if (dev) {
  serviceAccount = require('./credentials/serviceAccountKey.json')
}
admin.initializeApp({
  credential: dev ? admin.credential.cert(serviceAccount) : admin.credential.applicationDefault(),
});

const app = next({ dev });

package.json snippet

  "scripts": {
    "start": "NODE_ENV=production node server.js",
    "build": "next build",
    "dev": "NODE_ENV=development node server.js",
  },
Mike
  • 1,180
  • 3
  • 15
  • 28

1 Answers1

0

Two things come to mind to check.

  • The load balancer comes to mind, as if the new image isn't live as soon as it "ought" to be.

  • If production is serving requests at that time, the new image isn't live yet until all existing requests are finished.

Are you having the issue on every single request even after the new image has been up awhile? Like idk some sort of security check going on?

Is NextJS calling out to a third party provider for idk image compression or somesuch

Ron Newcomb
  • 2,886
  • 21
  • 24