GCP Cloud Run VS Cloud Function Cold Starts
--
I’m working on a small side project, for which I’m using Firebase and Google Cloud Functions. So far it’s been great however one of the use cases requires a response from the Cloud Function within a certain time limit. Occasionally, usually after the weekend, the Cloud Function will need to cold start and will take just too long. ‘Cold Starts’ are when a ‘serverless’ service is not cached by the cloud provider and the execution environment needs to be initialized from scratch.
I followed all the tips and tricks recommended by Google in their documentation, however it was never quite quick enough. So I decided to give Google’s new “Cloud Run” a look to see if it could deal with cold starts any better. Cloud Run is Googles new ‘serverless’ container offering. To test this I created a basic Cloud Function and two basic Cloud Run containers, then sent a few requests to them and timed the response.
Cloud Function
For the Cloud Function I simply created a brand new Cloud Function, removed the code that read the request and simply got it to output ‘hello world’. The code looks like this:
exports.helloWorld = (req, res) => {
let message = 'Hello World!';
res.status(200).send(message);
};
Cloud Run
For Cloud Run I created two containers. Both of them are based on the examples in the GCP Documenation. The first container is a Nodejs container, the Dockerfile is almost exactly like the one in the docs, except I changed the runtime to Nodejs 8, to bring it more in line with the Cloud Function runtime. So it looks like this:
FROM node:8WORKDIR /usr/src/app
COPY package.json ./
RUN npm install --only=production
COPY ./ .CMD [ "npm", "start" ]
The index.js itself looks like this:
const express = require('express');
const app = express();app.get('/', (req, res) => {
res.send(`Hello World`);
});const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log('Hello world listening on port', port);
});
The package.json file was copied straight from the docs. The Nodejs container was chosen was so it…