Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

What is considered "stateful" and is not compatible with Cloud Run, exactly? Is my example viable?

Hello.

This might be a very easy question to answer but I'm a beginner in the cloud world so please bear with me.

I am planning to run a specific backend code written in Node.js on a scheduled time, once every day using Cloud Scheduler. This code is nothing but a while-loop that sleeps 15 seconds, reads some stuff from a Firestore collection, makes calculations, and then writes new results to another Firestore collection on each step. After 240 steps, in other words, after 1 hour, the while-loop terminates.

I was planning to containerise this piece of code and host it using Cloud Run, but I read that Cloud Run is only used for stateless containers. I think I am having trouble understanding what statelessness is in this matter. Does my container having an internal while-loop that increments a variable by one on each step mean that my container is stateful? Or is my container still stateless, because each day, a new instance is of this container is run, which is not dependent on previous instances?

Also, another question that might be related to this: I think every example I encountered that uses Cloud Run uses Express.js. Does there need to be an Express server? Can't you just simply host, for example, a while-loop that runs until a condition is met internally?

On a different note, please let me know if there's a better service in GCP for this use case.

Thanks in advance!

Solved Solved
0 3 2,977
1 ACCEPTED SOLUTION

Hi

I don't have any specific knowledge of Cloud Run but I think I can give you some pointers. If anyone has more background they can jump in and correct me 😀

Regarding "stateless", in this context, I think your second assumption is correct: 


@usinakenes wrote:

each day, a new instance is of this container is run, which is not dependent on previous instances


Based on what you're trying to do, it looks like Cloud Run Jobs would be a good fit (not Cloud Run Services). You can create and schedule a "Job" that is just Node.js - no Express.js required.

Check this out. I think it should get you started:

https://cloud.google.com/run/docs/quickstarts/jobs/build-create-nodejs?hl=en

Good luck!

 

 

 

View solution in original post

3 REPLIES 3

Hi

I don't have any specific knowledge of Cloud Run but I think I can give you some pointers. If anyone has more background they can jump in and correct me 😀

Regarding "stateless", in this context, I think your second assumption is correct: 


@usinakenes wrote:

each day, a new instance is of this container is run, which is not dependent on previous instances


Based on what you're trying to do, it looks like Cloud Run Jobs would be a good fit (not Cloud Run Services). You can create and schedule a "Job" that is just Node.js - no Express.js required.

Check this out. I think it should get you started:

https://cloud.google.com/run/docs/quickstarts/jobs/build-create-nodejs?hl=en

Good luck!

 

 

 

Thanks for the clarifications, they really helped. I'll definitely take a look at Cloud Run Jobs, it sounds like something I could utilise for my project.

Most modern applications are stateless. Stateful is anything that retains state either in memory, or on disk, on the local instance. The reason Cloud Run is stateless is that we don't preserve instance memory between restarts (app instances can disappear and reappear at will, without retaining state); and we don't have a local persistent disk. Applications can always keep "state" elsewhere. For in-memory state, you can use Cloud Memorystore. For disk state you can use a database, GCS or a network file system. Let me know if you have any further questions about architecting your application for Cloud Run or Cloud Run Jobs! Cloud Run should support your use case.

Top Solution Authors