Mongodb-client-encryption for NodeJS does not work with worker threads

Hi everyone,

just had a quick conversation with Joe Drumgoole over at Twitter:

What am I trying to achieve?
I’m building an application with NodeJS that writes to and reads data from a MongoDB database. The application does periodically look at a specific collection to check if records are pending. I was trying to run this periodic task in a NodeJS worker thread to prevent blocking the main thread.

What happened?
Some fields of the documents in the collection are encrypted with mongodb-client-encryption. Running the script that does these periodic tasks in a worker threads will require the connection to the database and therefore instantiation of ClientEncryption.

However, when ClientEncryption is used inside a worker threads the application will crash on startup saying got error from worker Error: Module did not self-register.

What did I already find out?
There’s a closed Issue in the NodeJs Repository, that describe the exact same error message. In the same Issue there’s already a comment that explains a solution. To be executed inside worker threads a native add-on has to be context-aware. This is also described in the NodeJS Docs.

What does that error mean for me?
Well, I’m not blocked. Will run the periodic task without worker threads for now. I don’t expect much load in the coming weeks. Strategically it would anyways be a better idea to have the scheduled task run in a completely separate process or even machine.

One could argue that the access to database via NodeJS driver and client encryption are already non-blocking, so worker threads might not be a performance improvement if you consider the penalty for managing threads, too. Anyways I was surprised and disappointed that it didn’t work out of the box. :slightly_smiling_face:

What’s your opinion?