As we’ve seen in the previous article of this series, an in-memory queue can be useful in streamlining a work load and retrying by recovering from errors, but it’s subject to some problems.
If, for a period of time, the production of messages exceeds the worker capacity, the queue may start taking too much memory.
Also, if the Node.js process goes down, you will lose whatever pending work it had. This may or not pose a problem, depending on the application.
By having the work queue taken out of memory and put into persistent storage we can obviate these problems: by keeping only the current work in memory, we can absorb work peaks, as they will get written onto the disk. Also, when the Node.js process starts, any pending work is resumed.
For the sake of an example, let’s say that you’re building a home alarm system and want to relay every event (alarm switched off, alarm switched on, door opened, alert, etc.), into a remote service for storage and future audit purposes. This remote service may be down or unreachable at times, but you still want the events to eventually — sorry for the pun — get there.
Let’s then build a module that creates and exports a persistent queue where security events will be stored. This module will also be responsible for relaying these events into the remote service.
event_relay.js:
Here we start off by creating a LevelDB database where all the events will be persistently stored, inside a folder name db inside the current directory:
LevelDB is a generic key-value store, but here we’ll not use it directly; instead we’ll hand it off to the level-jobs package, which implements a worker queue on top of a LevelDB database:
Here we’re creating a job queue by providing the LevelDB database that we created earlier and a worker function, and defining a maximum concurrency of 1.
The worker function is a function very similar to the async.queue worker function: it accepts a work unit as the first argument and a callback function as the second argument:
The worker function simply tries to send the work unit (an event, in our case) to the remote event storage service using this sendEventToRemoteService function. If the sending fails for some reason, our callback is invoked with an error, which we propagate to the worker callback. When the worker callback is invoked with an error, level-jobs retries until succeeded (up to a pre-defined maximum number of attempts, using a back-off algorithm internally as in the previous section).
We then simulate event-delivery errors with a 50% probability in a fake implementation of the sendEventToRemoteService function:
Let’s then create an event producer so that we can test our event relay module:
event_producer.js
and run it:
We can see by the output that, even though some failed, all the events eventually got relayed. Also, by setting the concurrency to 1, we make sure that no more than one pending work unit is being processed at any given time, no matter what the job creation rate is, thus saving memory and streamlining the work load.
Next article
So far we’ve seen the usefulness of creating a local queue for streamlining the work load and persisting the work throughout process restarts. However, depending on the type of work that you’re doing, this approach still has some problems: if this work is somehow CPU-intensive, you may need to outsource the work to a set of external worker processes.
In the next article of this series we’ll be looking at how we can use a queue service to distribute the load between several node processes.
This article was extracted from the Work Queues book, a book from the Node Patterns series.