Tutorial Membangun Durable Workflow dengan Postgres dan DBOS
Fortune
Fortune

Dipublikasikan 29 Mei 2026

Tutorial Membangun Durable Workflow dengan Postgres dan DBOS

Salah satu problem klasik di distributed system adalah bagaimana memastikan workflow tetap berjalan meskipun ada node failure. Solusi tradisional seringkali over-engineering dengan orchestrator complex. Tapi ternyata, Postgres saja sudah cukup untuk durable execution. Di tutorial ini, kita akan breakdown cara membangun durable workflow menggunakan DBOS yang mengandalkan Postgres sebagai backbone-nya.

Inspirasi tutorial ini berdasarkan artikel dari DBOS blog: Postgres Is All You Need for Durable Execution. Jika kamu ingin deep dive ke konsep aslinya, silakan cek source link tersebut.

Persiapan Environment dan Instalasi DBOS

Pastikan kamu sudah punya Node.js 18+ dan Postgres 14+ running di local. Install DBOS globally via npm:

npm install -g @dbos-inc/dbos-cloud

Kemudian init project baru:

dbos-cloud init -n my-durable-app
cd my-durable-app
npm install

DBOS akan generate boilerplate dengan decorator-based workflow yang familiar buat kamu yang pernah pakai NestJS atau TypeORM.

Memahami Konsep Durable Execution

Durable execution artinya setiap step di workflow kamu dipersist ke Postgres sebelum dieksekusi. Kalau service restart di tengah jalan, DBOS bisa resume dari step terakhir yang tercatat. Ini mirip event sourcing tapi lebih lightweight. Kamu nggak perlu setup Kafka atau Redis Stream. Postgres WAL sudah jadi event log-nya.

Menulis Workflow Pertama

Buat file src/operations.ts dengan contoh workflow sederhana:

import { Transaction, Workflow, StepContext } from '@dbos-inc/dbos-sdk';

export class OrderWorkflow {
  @Workflow()
  static async processOrder(ctx: StepContext, orderId: string) {
    const inventory = await OrderWorkflow.checkInventory(ctx, orderId);
    if (inventory > 0) {
      await OrderWorkflow.chargePayment(ctx, orderId);
      await OrderWorkflow.shipOrder(ctx, orderId);
    }
    return { status: 'completed', orderId };
  }

  @Transaction()
  static async checkInventory(ctx: StepContext, orderId: string) {
    // query inventory dari DB
    return 1;
  }

  @Transaction()
  static async chargePayment(ctx: StepContext, orderId: string) {
    // proses pembayaran idempoten
  }

  @Transaction()
  static async shipOrder(ctx: StepContext, orderId: string) {
    // trigger logistics
  }
}

Perhatikan decorator @Workflow(), @Transaction(), dan @Step(). Setiap step auto-persisted ke Postgres sebelum execute.

Menjalankan dan Mengecek Durable Log

Start aplikasi dengan:

npx dbos-cloud start

Cek Postgres untuk melihat tabel internal DBOS:

SELECT * FROM dbos.workflow_outputs;
SELECT * FROM dbos.workflow_events;

Kamu akan lihat setiap workflow instance punya unique ID dan status. Kalau kamu kill process di tengah chargePayment, lalu restart, DBOS auto-resume dari step yang sama tanpa double-charge.

Deployment ke Cloud dengan DBOS Cloud

Kalau local sudah oke, deploy dengan satu command:

dbos-cloud deploy

DBOS Cloud akan provision Postgres managed dan auto-scale worker-nya. Yang menarik, karena state ada di Postgres, horizontal scaling jadi trivial. Worker baru tinggal baca event log dan continue workflow yang pending.

Kesimpulan dan Rekomendasi

Arsitektur durable execution berbasis Postgres ini bikin distributed system jadi lebih sederhana tanpa sacriface reliability. Kamu nggak perlu maintain Redis, RabbitMQ, atau temporal cluster hanya untuk workflow persistence. Cukup Postgres yang sudah familiar dan battle-tested selama puluhan tahun.

Referensi lengkap bisa kamu baca di DBOS official blog. Happy coding!