Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How to handle transactions in Node.js

Introduction to the Node.js reference architecture, Part 15

July 31, 2023
Michael Dawson
Related topics:
Node.js
Related products:
Red Hat build of Node.js

Share:

    In many applications, completing more than a single update as an atomic unit (transaction) is needed to preserve data integrity. This installment of the ongoing Node.js Reference Architecture series covers the Node.js reference architecture team’s experience with integrating transactions into your application to satisfy this requirement.

    Follow the series:

    • Part 1: Overview of the Node.js reference architecture
    • Part 2: Logging in Node.js
    • Part 3: Code consistency in Node.js
    • Part 4: GraphQL in Node.js
    • Part 5: Building good containers
    • Part 6: Choosing web frameworks
    • Part 7: Code coverage
    • Part 8: Typescript
    • Part 9: Securing Node.js applications
    • Part 10: Accessibility
    • Part 11: Typical development workflows
    • Part 12: Npm development
    • Part 13: Problem determination
    • Part 14: Testing
    • Part 15: Transaction handling
    • Part 16: Load balancing, threading, and scaling
    • Part 17: CI/CD best practices in Node.js
    • Part 18: Wrapping up

    Ecosystem support for transactions

    Unlike the Java ecosystem, there are no well-established application servers with built in support for transactions and no JavaScript specific standards for transactions. Instead, Node.js applications either depend on the transaction support within databases and their related Node.js clients and/or use patterns which are needed when business logic is spread over a number of microservices. These patterns include:

    • 2 phase commit
    • Saga pattern

    Leveraging database support for transactions

    In the team's experience, if the updates that need to be completed atomically can be handled by a single Node.js component, and you are using a database with support for transactions that is the easiest case.

    In some Node.js database clients, there is no specific support for transactions in the client, but that does not mean that transactions are not supported. Instead transactions are managed by using protocol level statements (for example BEGIN/COMMIT in SQL). PostgreSQL is one of those databases. A simple example of using a transaction from the node-postgres client documentation is as follows:

    const { Pool } = require('pg')
    const pool = new Pool()
     
    const client = await pool.connect()
     
    try {
      await client.query('BEGIN')
      const queryText = 'INSERT INTO users(name) VALUES($1) RETURNING id'
      const res = await client.query(queryText, ['brianc'])
     
      const insertPhotoText = 'INSERT INTO photos(user_id, photo_url) VALUES ($1, $2)'
      const insertPhotoValues = [res.rows[0].id, 's3.bucket.foo']
      await client.query(insertPhotoText, insertPhotoValues)
      await client.query('COMMIT')
    } catch (e) {
      await client.query('ROLLBACK')
      throw e
    } finally {
      client.release()
    }
    

    In the team's experience transactions work best with async/await versus generators, and it is good to have a try/catch around the rollback sections in addition to those which do the commit.

    The challenges of microservices

    In the team’s experience, the more common case is that a Node.js application incorporates a number of microservices, each of which might have a connection to the database or even may store data across different types or instances of datastores.

    The challenges that come with implementing transactions with microservices are not unique to Node.js and the common approaches include:

    • 2 phase commit
    • Saga pattern

    These are also used in Node.js applications.

    Patterns for distributed transactions within a microservices architecture provides a nice overview of the challenges introduced when managing transactions across microservices and these two patterns.

    In the team's experience, Node.js applications will most often use the Saga pattern as opposed to two-phase commit, in part since the microservices may not share the same data store.

    When two-phase commit is used, it will have to depend on external support from an underlying data store. Some databases offer support to help with implementing the two-phase commit technique, so read through the documentation for the database you are using if you are planning to use that technique.

    Learn more about Node.js reference architecture

    I hope that this quick overview of the transaction handling part of the Node.js reference architecture, along with the team discussions that led to that content, has been helpful, and that the information shared in the architecture helps you in your future implementations.

    We cover new topics regularly for the Node.js reference architecture series. In the next installment, read about load balancing, threading, and scaling in Node.js.

    We invite you to visit the Node.js reference architecture repository on GitHub, where you will see the work we have done and future topics. To learn more about what Red Hat is up to on the Node.js front, check out our Node.js page.

    Last updated: January 9, 2024

    Related Posts

    • Monitor Node.js applications on Red Hat OpenShift with Prometheus

    • Introduction to the Node.js reference architecture, Part 8: TypeScript

    • Node.js Reference Architecture, Part 10: Accessibility

    • Introduction to the Node.js reference architecture, Part 11: Typical development workflows

    • Introduction to the Node.js reference architecture: Wrapping up

    • 8 elements of securing Node.js applications

    Recent Posts

    • LLM Compressor: Optimize LLMs for low-latency deployments

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    • Leveraging Ansible Event-Driven Automation for Automatic CPU Scaling in OpenShift Virtualization

    • Python packaging for RHEL 9 & 10 using pyproject RPM macros

    • Kafka Monthly Digest: April 2025

    What’s up next?

    This cheat sheet gets you started using Ansible to automate tasks on Cisco Meraki, an enterprise-cloud managed networking solution for managing access points, security appliances, L2 and L3 switches, and more.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue

    OSZAR »