How do I stop nodejs-firestore writes from blocking the node.js event loop?

Viewed 37

When using the node.js client for firebase admin, calling DocumentReference.set blocks the nodejs event loop for approximately 200ms for every write. 200ms approximately corresponds to the latency expected by a network round-trip to and from the firestore server. This is a major cause of slowdown for my application.

The documentation for DocumentReference.set seems to imply that this should be a non-blocking call because the method returns a promise (note that I did not include an await in the code, so I do not wait for the promise to resolve).

Is there a way to make this call not block the node.js event loop?

Code

logger.info("start of DB write")
const collectionReference = this.db.collection("notification");
logger.info("Before DB reference");
const documentReference = collectionReference.doc(key);
logger.info("Before DB write");
documentReference.set(value);
logger.info("after DB write");

Logs from the server

{"label":"index.js","level":"info","message":"start of DB write","timestamp":"2022-06-01T09:13:13.684Z"}
{"label":"index.js","level":"info","message":"Before DB reference","timestamp":"2022-06-01T09:13:13.684Z"}
{"label":"index.js","level":"info","message":"Before DB write","timestamp":"2022-06-01T09:13:13.684Z"}
{"label":"index.js","level":"info","message":"after DB write","timestamp":"2022-06-01T09:13:13.867Z"}

Environment details

  • OS: node:16-alpine (docker image)
  • Node.js version: v16.13.2
  • npm version: 8.5.5
  • @google-cloud/firestore version: 4.15.1
1 Answers

It turns out this problem was caused by something I did not expect: The esbuild bundler I was using. It seems like esbuild does not play nice with firebase admin. Removing esbuild from the build process fixes the problem. Now all 4 log statements are logged at the same ms. FYI: esbuild version I used was 0.14.21 .

Related