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.
- Locally in production mode with docker
- Locally in production mode with
yarn build && yarn start
- Locally in dev mode with
yarn run dev
- 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",
},