Pickels
Pickels

Reputation: 86

Node.JS Job / Background Process and high availability

I’m working on the design of a new Node.JS application. I see a lot of options and possibilities on getting a high availability production environment: load balanced apps and reverse proxies, Redis clusters, MongoDB replicas and sharding deployments, etc.

The solution I’m planning have the need of a background process, doing some processing on specific MongoDB tables, and in-memory Redis data. The job needs to run every minute, and I can’t be run in parallel with another instance of the same job.

I know how to build that kind of job in Node using timers. In the other hand, i know I can have some monitoring process probing the job process, but I can see a clear solution on how to replace a failing app in another host.

I found some blog posts about “HA Singleton” (a sort of pattern in the Java world), and database based locks for resolving this situation. May be I can implement a locking mechanism using a lock stored on a Redis replicated data store, with several jobs pooling the lock state, but I guess it will be hard to implement some “extreme” situations (a job pooling the lock state a few milliseconds after a previous job).

There is any pattern of best practice about how to resolve this king of situation?.

Upvotes: 2

Views: 652

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146094

I've used mongodb-queue for this. To get the "periodic singleton" behavior I create a queue with the appropriate delay setting on startup, check the queue.size() and if zero add a job. Then I run a setInterval that periodically does a queue.get. If that returns null, it's a no-op. If it returns a job, do the work and queue.add then next one.

Upvotes: 2

Related Questions