๐ Temporal ๊ฐ๋ ๊ณผ ํต์ฌ ๋ช ๋ น์ด ๋น ๋ฅธ ์กฐํ๊ฐ ํ์ํ์ ๊ฐ์?
์ค๋ฌด ๋ ํผ๋ฐ์ค๋ฅผ ์ํ Temporal ์นํธ์ํธ ๋ฐ๋ก๊ฐ๊ธฐ โ
0. ๐ ์ ๋ฉ์์ง ํ ๋์ โTemporal Durable Executionโ์ธ๊ฐ์?
์ฃผ๋ฌธ โ ์ฌ๊ณ ์ฐจ๊ฐ โ ๊ฒฐ์ ์น์ธ โ ๋ฐฐ์ก ์์ฒญ์ผ๋ก ์ด์ด์ง๋ ๋ถ์ฐ ํธ๋์ญ์
์์ ์ค๊ฐ ๋จ๊ณ๊ฐ ์คํจํ๊ฑฐ๋ ์๋ฒ๊ฐ ์ฌ์์๋๋ฉด, ๊ฐ๋ฐ์๋ DB ์ํ ํ
์ด๋ธยทํฌ๋ก ์ก(Cron)ยท๋ฐ๋๋ ํฐ ํ(DLQ)ยท๋ณด์ ๋ก์ง์ ์ง์ ์ง์ผ ํฉ๋๋ค.
**Temporal(Server v1.32+)**์ Durable Execution(์์ ์คํ) ์์ง์ ํตํด ์ฝ๋์ ์คํ ์ํ(Event History)๋ฅผ ์๋์ผ๋ก ์๊ตฌ ์ ์ฅํฉ๋๋ค. ์์ปค ํ๋ก์ธ์ค๊ฐ ์ฅ์ ๋ก ์ฃฝ๋๋ผ๋ ์ ์์ปค๊ฐ ์ ํํ ์ค๋จ๋ ์ง์ ๋ถํฐ ์ํ๋ฅผ ๋ณต์(Replay)ํด ์ด์ด์ ์คํํ๋ฏ๋ก ํฌ๋์ยทํ์์์ยท๋คํธ์ํฌ ๋จ์ ์๋ ๋น์ฆ๋์ค ๋ก์ง์ด ์ ๋ ์ ์ค๋์ง ์์ต๋๋ค.
| ๋น๊ต ํญ๋ชฉ | ์ง์ ๊ตฌํ (DB + Cron + MQ) | Temporal Durable Execution (v1.32+) |
|---|---|---|
| ์ํ ๊ด๋ฆฌ | ์๋ ์ํ ๋จธ์ ์ปฌ๋ผ + ํด๋ง ๋ฐฐ์น | Event History ์๋ ์์ํ & Replay |
| ์ฌ์๋ / ํ์์์ | DLQ + ์ฌ์ฒ๋ฆฌ ์คํฌ๋ฆฝํธ ์ง์ ์์ฑ | Activity RetryPolicy (์ง์ ๋ฐฑ์คํ) ๋ด์ฅ |
| ์ฅ๊ธฐ ๋๊ธฐ (Sleep) | ์ค์ผ์ค๋ฌ ํ ์ด๋ธ ํ์ | sleep('30 days') ์ฝ๋ ํ ์ค๋ก ๋ฌด๋ถํ ๋๊ธฐ |
| ๋ถ์ฐ ๋กค๋ฐฑ (Saga) | ์ญ์ ๋ณด์ ์ด๋ฒคํธ ์๋ ๋ฐํ | try/catch + ๋ณด์ Activity ์คํ์ผ๋ก ์ง๊ด์ ๊ตฌํ |
1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11 (WSL2), macOS 13+, Linux | Ubuntu 24.04 LTS / macOS Apple Silicon | Temporal CLI ๋ก์ปฌ ๊ฐ๋ฐ ์๋ฒ ๋๋ Docker ์ง์ |
| CPU ์ํคํ ์ฒ | 2์ฝ์ด ์ด์ (x86_64 / ARM64) | 4์ฝ์ด ์ด์ | Temporal Server + Web UI + Worker ๋์ ๊ตฌ๋ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | ์ต์ 4 GB ์ด์ | 8 GB ~ 16 GB ์ด์ | ๋ก์ปฌ Temporal Dev Server + Node.js Worker |
| ๋์คํฌ (Storage) | 5 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 15 GB+ SSD | Event History SQLite/PostgreSQL ์ ์ฅ |
| ๋ฐํ์ / SDK | Node.js 22 LTS, temporal CLI | TypeScript 5.6+, tsx | Temporal Server v1.32.2, @temporalio/* 1.25.0 |
๐ ๏ธ Temporal CLI ์ค์น ๋ฐ ๋ก์ปฌ ์๋ฒ ์คํ
# 1) macOS (Homebrew) ๋๋ Linux/WSL2 ๋ฐ์ด๋๋ฆฌ ์ค์น
brew install temporal
# ๋๋ Linux/WSL2:
# curl -sSf https://temporal.download/cli.sh | sh
# 2) ๋ก์ปฌ Temporal ๊ฐ๋ฐ ์๋ฒ + Web UI ํ ๋ฒ์ ์คํ (ํฌํธ 7233, UI 8233)
temporal server start-dev
๋ธ๋ผ์ฐ์ ์์ http://localhost:8233์ ์ ์ํ๋ฉด Temporal Web UI ๋์๋ณด๋๋ฅผ ๋ฐ๋ก ํ์ธํ ์ ์์ต๋๋ค.
2. ๐ง ์ํคํ ์ฒ & ๋ฉํ ๋ชจ๋ธ (Mental Model)
+--------------------+ startWorkflow() +-------------------------------+
| API Server | -------------------> | Temporal Server (v1.32.2) |
| (@temporalio/ | | - Event History (Append-Only)|
| client 1.25.0) | signal / query | - Task Queue: "order-queue" |
+--------------------+ -------------------> +---------------+---------------+
| Poll Tasks
v
+---------------------------------------------------------------------------+
| Temporal Worker (@temporalio/worker 1.25.0) |
| 1) Workflow (Deterministic ์๋๋ฐ์ค): ํ๋ฆ ์ ์ด, Retry, Compensation |
| 2) Activity (Side-Effects ํ์ฉ): ์ค์ DB ์ฐ๊ธฐ, ๊ฒฐ์ PG API, ์ด๋ฉ์ผ ๋ฐ์ก |
+---------------------------------------------------------------------------+
- Workflow (๊ฒฐ์ ๋ก ์ ): ์๋ฒ ํฌ๋์ ํ Replay ์ ํญ์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ด์ผ ํ๋ฏ๋ก Workflow ๋ด๋ถ์์๋
Math.random(),Date.now(), ์ง์ HTTP ํธ์ถ์ ๊ธ์งํ๊ณ ๋ฐ๋์ Activity๋ก ์์ํฉ๋๋ค. - Activity (๋น๊ฒฐ์ ๋ก ์ I/O ์ ๋ด): ์ธ๋ถ API ํธ์ถ, DB ํธ๋์ญ์
๋ฑ ์คํจ ๊ฐ๋ฅ์ฑ์ด ์๋ ์์
์ ๋ด๋นํ๋ฉฐ, ์คํจ ์ Temporal์ด ์ค์ ๋
RetryPolicy์ ๋ฐ๋ผ ์๋ ์ฌ์๋ํฉ๋๋ค.
3. [Phase 1] TypeScript SDK (1.25.0) ํ๋ก์ ํธ ์ด๊ธฐํ
โ ๏ธ ๋ฒ์ ์ผ์น ๊ท์น: ๋ชจ๋
@temporalio/*ํจํค์ง๋ ๋ฐ๋์ **๋์ผํ ๋ฒ์ (1.25.0)**์ผ๋ก ์ค์นํด์ผ ํฉ๋๋ค.
mkdir temporal-saga-lab && cd temporal-saga-lab
npm init -y
npm pkg set type=module
npm install @temporalio/client@1.25.0 @temporalio/worker@1.25.0 @temporalio/workflow@1.25.0 @temporalio/activity@1.25.0 @temporalio/common@1.25.0
npm install -D typescript tsx @types/node
4. [Phase 2] Activities & Saga ๋ณด์ ํธ๋์ญ์ Workflow ๊ตฌํ
์ฃผ๋ฌธ ์ฒ๋ฆฌ ์ค **๊ฒฐ์ ์น์ธ(chargePayment)์ด ์คํจํ๋ฉด ์์ ์ฑ๊ณตํ ์ฌ๊ณ ์์ฝ(reserveInventory)์ ์๋์ผ๋ก ์ทจ์(releaseInventory)**ํ๋ Saga ํจํด์ ๊ตฌํํฉ๋๋ค.
1) Activities (src/activities.ts)
export interface OrderInput {
orderId: string;
itemId: string;
amount: number;
forcePaymentFail?: boolean;
}
export async function reserveInventory(input: OrderInput): Promise<string> {
console.log(`[Activity] ์ฌ๊ณ ์์ฝ ์๋ฃ: order=${input.orderId}, item=${input.itemId}`);
return `RES-${input.orderId}`;
}
export async function releaseInventory(orderId: string): Promise<void> {
console.log(`[Compensation] ์ฌ๊ณ ์์ฝ ์ทจ์(๋ณด์) ์๋ฃ: order=${orderId}`);
}
export async function chargePayment(input: OrderInput): Promise<string> {
if (input.forcePaymentFail) {
throw new Error(`PG ๊ฒฐ์ ์น์ธ ๊ฑฐ์ (์์ก ๋ถ์กฑ): order=${input.orderId}`);
}
console.log(`[Activity] ๊ฒฐ์ ์น์ธ ์๋ฃ: ${input.amount}์`);
return `PAY-${input.orderId}`;
}
export async function refundPayment(orderId: string): Promise<void> {
console.log(`[Compensation] ๊ฒฐ์ ํ๋ถ(๋ณด์) ์๋ฃ: order=${orderId}`);
}
2) Saga Workflow (src/workflows.ts)
import { proxyActivities, defineSignal, defineQuery, setHandler, condition } from '@temporalio/workflow';
import type * as activities from './activities.js';
const { reserveInventory, releaseInventory, chargePayment, refundPayment } = proxyActivities<typeof activities>({
startToCloseTimeout: '10 seconds',
retry: {
initialInterval: '1 second',
backoffCoefficient: 2,
maximumAttempts: 3, // 3ํ ์คํจ ์ catch ๋ธ๋ก์ผ๋ก ์ด๋ํ์ฌ Saga ๋ณด์ ์คํ
},
});
export const approveManualReviewSignal = defineSignal('approveManualReview');
export const getOrderStatusQuery = defineQuery<string>('getOrderStatus');
export async function orderSagaWorkflow(input: activities.OrderInput): Promise<string> {
const compensations: Array<() => Promise<void>> = [];
let status = 'INITIATED';
let manualApproved = input.amount < 1000000;
setHandler(getOrderStatusQuery, () => status);
setHandler(approveManualReviewSignal, () => {
manualApproved = true;
});
try {
// 1๋จ๊ณ: ๊ณ ์ก ์ฃผ๋ฌธ(100๋ง ์ ์ด์)์ผ ๊ฒฝ์ฐ Signal ์น์ธ ๋๊ธฐ
if (!manualApproved) {
status = 'WAITING_MANUAL_REVIEW';
const approved = await condition(() => manualApproved, '24 hours');
if (!approved) throw new Error('์๋ ์น์ธ ํ์์์');
}
// 2๋จ๊ณ: ์ฌ๊ณ ์์ฝ + ๋ณด์ ํจ์ ์คํ์ ๋ฑ๋ก
status = 'RESERVING_INVENTORY';
await reserveInventory(input);
compensations.push(() => releaseInventory(input.orderId));
// 3๋จ๊ณ: ๊ฒฐ์ ์น์ธ + ๋ณด์ ํจ์ ์คํ์ ๋ฑ๋ก
status = 'CHARGING_PAYMENT';
const paymentId = await chargePayment(input);
compensations.push(() => refundPayment(input.orderId));
status = 'COMPLETED';
return `ORDER_SUCCESS:${paymentId}`;
} catch (err) {
// ์คํจ ์ ๋ฑ๋ก๋ ๋ณด์ ํจ์๋ฅผ LIFO(์ญ์)๋ก ์คํ
status = 'COMPENSATING';
for (const compensate of compensations.reverse()) {
await compensate();
}
status = 'ROLLED_BACK';
throw err;
}
}
5. [Phase 3] Worker & Client ์คํ ๋ฐ ์ฅ์ ๋ณต๊ตฌ ํ ์คํธ
1) Worker (src/worker.ts)
import { Worker } from '@temporalio/worker';
import * as activities from './activities.js';
async function run() {
const worker = await Worker.create({
workflowsPath: new URL('./workflows.ts', import.meta.url).pathname,
activities,
taskQueue: 'order-queue',
});
console.log('Temporal Worker listening on taskQueue="order-queue"...');
await worker.run();
}
run().catch((err) => {
console.error(err);
process.exit(1);
});
2) Client (src/client.ts)
import { Connection, Client } from '@temporalio/client';
import { orderSagaWorkflow, getOrderStatusQuery } from './workflows.js';
async function main() {
const connection = await Connection.connect({ address: 'localhost:7233' });
const client = new Client({ connection });
const handle = await client.workflow.start(orderSagaWorkflow, {
taskQueue: 'order-queue',
workflowId: `order-saga-${Date.now()}`,
args: [{ orderId: 'ORD-9001', itemId: 'GPU-4090', amount: 50000, forcePaymentFail: true }],
});
console.log(`Started Workflow: ${handle.workflowId}`);
try {
const result = await handle.result();
console.log('Result:', result);
} catch {
const finalStatus = await handle.query(getOrderStatusQuery);
console.log('Saga Rollback Completed. Final Status:', finalStatus);
}
}
main();
# ํฐ๋ฏธ๋ 1: Worker ์คํ
npx tsx src/worker.ts
# ํฐ๋ฏธ๋ 2: Client ์คํ (๊ฒฐ์ ์คํจ -> ์ฌ๊ณ ์์ฝ ์๋ ๋ณด์ ํ์ธ)
npx tsx src/client.ts
์์ ์ถ๋ ฅ (Worker ์ฝ์):
[Activity] ์ฌ๊ณ ์์ฝ ์๋ฃ: order=ORD-9001, item=GPU-4090
[Compensation] ์ฌ๊ณ ์์ฝ ์ทจ์(๋ณด์) ์๋ฃ: order=ORD-9001
6. [Phase 4] ํ๋ก๋์ ์ด์ ์ฒดํฌ๋ฆฌ์คํธ
- Activity ๋ฉฑ๋ฑ์ฑ(Idempotency) ๋ณด์ฅ: Temporal์ At-Least-Once ์คํ์ ๋ณด์ฅํ๋ฏ๋ก, ๊ฒฐ์ /DB ์ฐจ๊ฐ Activity๋ ๋ฐ๋์
orderId๊ธฐ๋ฐ Idempotency Key๋ก ์ค๋ณต ์คํ์ ๋ฐฉ์ดํ์ธ์. - Workflow ๊ฒฐ์ ๋ก (Determinism) ์ค์: ์ด๋ฏธ ์คํ ์ค์ธ Workflow ์ฝ๋๋ฅผ ๋ฐฐํฌํ ๋๋
patched()API๋ Worker Versioning์ ์ฌ์ฉํด Replay ๋ถ์ผ์น(NonDeterminismError)๋ฅผ ๋ฐฉ์งํ์ธ์. - Heartbeat ์ค์ : 1๋ถ ์ด์ ๊ฑธ๋ฆฌ๋ ๋์ฉ๋ ๋ฐฐ์น Activity๋
Context.current().heartbeat()๋ฅผ ์ฃผ๊ธฐ์ ์ผ๋ก ํธ์ถํด ์์ปค ํฌ๋์ ์ ๋น ๋ฅด๊ฒ ๋ค๋ฅธ ์์ปค๋ก ๋๊ธฐ์ธ์. - ๋ฐฑ์๋ DB ์์ํ: ํ๋ก๋์ Temporal Server๋ SQLite ๋์ PostgreSQL 16+ ๋๋ Cassandra๋ฅผ Persistence Store๋ก ์ฐ๊ฒฐํ์ธ์.
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์