Within any application, certain side effects are always necessary. Developers might need to run tasks in parallel which aren’t that important. For example, updating the last login timestamp of users might not be of utmost importance for consumer-facing apps and it doesn’t make much sense for the app to exhibit a blocking behavior. Furthermore, for database transactions like these, the application might not need to return errors to the front end.
The event-driven nature of NodeJS should be leveraged as much as possible. The Pub/Sub architecture I use within my apps uses two things: event definitions and subscribers which are defined within the src/subscribers folder.
In the example project, with each successful user login, an event ‘updateUserLastLogin’ defined in src/declarations is fired from within the last middleware of the request lifecycle, which is part of the response middleware.
In src/subscribers folder, a subscriber function bound to the same ‘updateUserLastLogin’ is fired which correspondingly updates the last login timestamp of the user.
In this way, there’s a separation of the main business logic with this not-so-necessary side-effect and the application is free to serve its users without interruption.
There are caveats to this Pub/Sub architecture as well. In heavy HTTP load, the database connection will be clogged resulting in persistence errors and possibly a database connection failure. Moreover, not all side effects are equal. Developers might need to run background tasks and other important API calls after a certain unit of business function finishes within the app.
That is why it’s recommended to run important side-effects asynchronously via CRON jobs. In the next article, I’ll go over AgendaJS and how to effectively manage side effects that you might not want to run right away using the Pub/Sub mechanism.
Copyright © 2026. Salman Ahmad.