
Tạo/huỷ đơn, trạng thái đơn, transactionCreating and cancelling orders, order states, transactions
Sale trưa 300 đơn mỗi giâyA 300-orders-per-second lunch sale.Một sàn bán 50.000 đơn mỗi ngày, đợt sale trưa đẩy lên 300 đơn mỗi giây. Mỗi đơn chạm tới kho, thanh toán và vận chuyển ở ba service khác nhau.A marketplace sells 50,000 orders a day, and the lunch sale pushes that to 300 per second. Each order touches stock, payment and shipping in three different services.
Hãy theo đơn 1001 của người dùng 42 trong đợt sale trưa 12:00: Order Service ghi đơn, dòng sản phẩm, lịch sử và outbox trong một transaction, rồi Saga Orchestrator lần lượt gọi Inventory, Payment và Shipping, chạy bước bù khi một bước lỗi. Mọi thay đổi trạng thái đi qua bảng chuyển trạng thái và cột version, nên không bước nào nhảy cóc hay ghi đè lên nhau. Chạy luồng thành công trước, rồi thử “Thanh toán thất bại”, “App gửi lại yêu cầu tạo đơn” và “Huỷ sau khi giao vận chuyển” để thấy hàng được nhả, đơn không bị trùng và yêu cầu sai bị từ chối.Follow user 42's order 1001 during the 12:00 lunch sale: the Order Service writes the order, its items, history and an outbox row in one transaction, then the Saga Orchestrator calls Inventory, Payment and Shipping in turn and runs compensations when a step fails. Every status change goes through the transition table and the version column, so no step skips ahead or overwrites another. Play the success flow first, then try “Payment fails”, “App retries order creation” and “Cancel after shipping” to see stock released, duplicates avoided and invalid requests rejected.
1Người dùng 42 bấm “Đặt hàng” với 2 sản phẩm lúc 12:00 sale trưa. App sinh clientOrderId một lần cho lần bấm này, để mọi lần gửi lại đều được nhận ra là cùng một đơn.User 42 taps “Place order” with 2 items at the 12:00 lunch sale. The app generates a clientOrderId once for this tap, so every resend is recognized as the same order.
Mỗi trạng thái khai báo các bước kế tiếp được phép. Mọi thay đổi đi qua một hàm duy nhất kiểm tra bảng này, không cho set status tuỳ ý từ controller.Each status declares the transitions it allows. Every change goes through a single method that checks this table; no controller sets the status directly.
Người dùng bấm huỷ đúng lúc saga ghi PAID. Cột version cùng UPDATE … WHERE version = ? làm một bên nhận 0 dòng và phải đọc lại.The user cancels just as the saga writes PAID. A version column with UPDATE … WHERE version = ? makes one side see 0 rows and re-read.
Đơn, các dòng sản phẩm với giá chốt, lịch sử và outbox nằm trong một transaction. Lỗi ở bất kỳ dòng nào thì không còn gì.The order, its items with the price snapshot, the history and the outbox row share one transaction. If any row fails, nothing remains.
Kho, thanh toán, vận chuyển là các service riêng. Saga orchestration chạy từng bước, lưu tiến độ ở saga_state và chạy bù theo thứ tự ngược khi một bước lỗi.Inventory, payment and shipping are separate services. An orchestrated saga runs each step, stores progress in saga_state and compensates in reverse order when a step fails.
Không publish Kafka trong transaction DB. Ghi outbox cùng commit, relay đẩy sau; consumer xử lý idempotent theo event id.Do not publish to Kafka inside the DB transaction. Write an outbox row in the same commit and let a relay publish it; consumers deduplicate by event id.
App sinh clientOrderId một lần cho mỗi lần đặt. Unique (user_id, client_order_id) biến lần gửi thứ hai thành trả lại đơn đã có.The app generates a clientOrderId once per checkout. A unique (user_id, client_order_id) index turns the second call into returning the existing order.
| BảngTable | Cột chínhKey columns | Ghi chúNotes |
|---|---|---|
orders | id, user_id, client_order_id, status, version, total_amount, currency, created_at, updated_at | unique(user_id, client_order_id); version tăng mỗi lần đổi trạng tháiunique(user_id, client_order_id); version increments on every status change |
order_item | order_id, product_id, sku, product_name, unit_price, quantity | Giá và tên chốt lúc đặt, catalog đổi giá sau không ảnh hưởngPrice and name captured at order time; later catalog changes do not affect it |
order_history | id, order_id, from_status, to_status, reason, actor, created_at | Chỉ thêm, không sửa; phục vụ audit và CSKHAppend-only; used for audit and customer support |
outbox | id, aggregate_id, type, payload, created_at, published_at | OrderCreated, OrderPaid, OrderCancelled; relay đẩy lên order.eventsOrderCreated, OrderPaid, OrderCancelled; published to order.events by a relay |
saga_state | order_id, step, status, attempts, updated_at | RESERVE → CHARGE → SHIP; status RUNNING · COMPENSATING · COMPLETED · COMPENSATEDRESERVE → CHARGE → SHIP; status RUNNING · COMPENSATING · COMPLETED · COMPENSATED |
OrderService (cả Java và Go) là hộp Order Service ở sơ đồ; OrderRepository, OrderHistoryRepository và OutboxRepository ghi vào hộp PostgreSQL, còn dòng outbox OrderCancelled là thứ relay đẩy lên Kafka cho Saga Orchestrator. Hàm cancel chọn trạng thái đích, kiểm bảng NEXT, cập nhật có điều kiện version rồi ghi lịch sử và outbox trong cùng transaction.OrderService (Java and Go) is the Order Service box in the diagram; OrderRepository, OrderHistoryRepository and OutboxRepository write into the PostgreSQL box, and the OrderCancelled outbox row is what the relay publishes to Kafka for the Saga Orchestrator. cancel picks the target status, checks the NEXT table, updates conditioned on the version, and writes history and outbox in the same transaction.
package com.example.order;
import static com.example.order.OrderStatus.CANCELLED;
import static com.example.order.OrderStatus.DELIVERED;
import static com.example.order.OrderStatus.PAID;
import static com.example.order.OrderStatus.PENDING;
import static com.example.order.OrderStatus.REFUNDED;
import static com.example.order.OrderStatus.SHIPPED;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class OrderService {
private static final Map<OrderStatus, Set<OrderStatus>> NEXT = Map.of(
PENDING, Set.of(PAID, CANCELLED),
PAID, Set.of(SHIPPED, REFUNDED),
SHIPPED, Set.of(DELIVERED),
DELIVERED, Set.of(REFUNDED),
CANCELLED, Set.of(),
REFUNDED, Set.of());
private final OrderRepository orders;
private final OrderHistoryRepository history;
private final OutboxRepository outbox;
@Transactional
public Order cancel(UUID orderId, String actor) {
Order o = orders.findById(orderId).orElseThrow(OrderNotFoundException::new);
OrderStatus from = o.getStatus();
OrderStatus to = switch (from) {
case PENDING -> CANCELLED;
case PAID -> REFUNDED; // saga refunds and releases stock
default -> throw new OrderNotCancellableException(from); // 409
};
move(o, to, actor);
outbox.save(OutboxEvent.of("OrderCancelled", o.getId(),
Map.of("from", from, "to", to)));
return o; // flush: UPDATE orders … WHERE id = ? AND version = ?
}
private void move(Order o, OrderStatus to, String actor) {
OrderStatus from = o.getStatus();
if (!NEXT.get(from).contains(to))
throw new IllegalTransitionException(from, to);
o.setStatus(to); // Order has @Version
history.save(new OrderHistory(o.getId(), from, to, actor));
}
}package main
import (
"context"
"slices"
"github.com/google/uuid"
)
var next = map[Status][]Status{
Pending: {Paid, Cancelled},
Paid: {Shipped, Refunded},
Shipped: {Delivered},
Delivered: {Refunded},
}
func canMove(from, to Status) bool {
return slices.Contains(next[from], to)
}
func (s *OrderService) Cancel(ctx context.Context, id uuid.UUID, actor string) error {
return s.tx.Do(ctx, func(tx Tx) error {
o, err := tx.GetOrder(ctx, id)
if err != nil {
return err
}
var to Status
switch o.Status {
case Pending:
to = Cancelled
case Paid:
to = Refunded // saga refunds and releases stock
default:
return ErrNotCancellable // 409
}
if !canMove(o.Status, to) {
return ErrIllegalTransition
}
res, err := tx.Exec(ctx, `UPDATE orders SET status = $1, version = version + 1
WHERE id = $2 AND version = $3`, to, id, o.Version)
if err != nil {
return err
}
if n, _ := res.RowsAffected(); n == 0 {
return ErrConcurrentUpdate // someone changed it first
}
if err := tx.InsertHistory(ctx, id, o.Status, to, actor); err != nil {
return err
}
return tx.InsertOutbox(ctx, "OrderCancelled", id, map[string]any{"from": o.Status, "to": to})
})
}| OrchestrationOrchestration | ChoreographyChoreography | |
|---|---|---|
| Luồng nằm ở đâuWhere the flow lives | Một orchestrator giữ thứ tự bước và bước bùOne orchestrator holds the step order and compensations | Rải rác trong các service, mỗi service nghe sự kiện của service khácSpread across services, each listening to the others' events |
| Theo dõi, gỡ lỗiTracing and debugging | Đọc saga_state là biết đơn đang ở bước nàoReading saga_state shows which step an order is in | Phải ghép sự kiện từ nhiều serviceEvents from several services must be stitched together |
| Phụ thuộcCoupling | Orchestrator biết API của mọi bênThe orchestrator knows every participant's API | Service độc lập hơn, nhưng dễ sinh vòng sự kiệnServices are more independent, but event cycles creep in |
| Chọn khiPick it when | Từ 3 bước trở lên, có bước bù rõ ràngThree or more steps with clear compensations | 2 bước đơn giản, các team muốn tự chủTwo simple steps and autonomous teams |
Khi controller, job và consumer đều tự gọi order.setStatus(), sớm muộn sẽ có đơn nhảy từ CANCELLED sang SHIPPED. Gom mọi thay đổi vào một phương thức transition(to) kiểm bảng cho phép, ghi order_history, và thêm CHECK constraint hoặc test đi qua mọi cặp trạng thái để bắt bước không hợp lệ.When controllers, jobs and consumers all call order.setStatus(), sooner or later an order jumps from CANCELLED to SHIPPED. Route every change through one transition(to) method that checks the allowed table and writes order_history, and add a test that walks every state pair to catch illegal moves.
Code commit DB rồi gọi kafkaTemplate.send() sẽ mất sự kiện khi pod bị kill giữa hai lệnh, để lại đơn PENDING không bao giờ có saga. Ghi outbox trong cùng transaction, cho relay hoặc Debezium đẩy sang Kafka, và cảnh báo khi dòng outbox chưa gửi cũ hơn 1 phút.Code that commits and then calls kafkaTemplate.send() loses the event when the pod is killed between the two, leaving a PENDING order that never gets a saga. Write the outbox in the same transaction, let a relay or Debezium publish to Kafka, and alert when an unsent outbox row is older than 1 minute.
Nếu orchestrator chết sau khi giữ hàng và trước khi thanh toán, hàng bị khoá mãi và sản phẩm báo hết. Đặt hạn 15 phút cho mỗi reservation, chạy job nhả hàng hết hạn, và theo dõi số reservation quá hạn chưa nhả như một chỉ số sức khoẻ của saga.If the orchestrator dies after reserving and before charging, stock stays locked forever and the product shows sold out. Give every reservation a 15-minute expiry, run a job that releases expired ones, and track overdue unreleased reservations as a saga health metric.
Retry 5 lần cho card_declined chỉ làm ngân hàng nghi gian lận và khoá thẻ của khách. Phân loại lỗi: timeout và 5xx thì retry có backoff với cùng idempotency key, còn 402 hay out_of_stock thì chuyển ngay sang bước bù.Retrying card_declined five times only makes the bank suspect fraud and block the customer's card. Classify errors: retry timeouts and 5xx with backoff and the same idempotency key, but move straight to compensation on 402 or out_of_stock.
Mỗi pattern mở bài học tương ứng trong menu Pattern.Each one opens its lesson in the Patterns menu.
Giá trong catalog đổi theo thời gian. Hoá đơn, hoàn tiền và đối soát phải dựa trên giá lúc khách đặt, nên đơn giữ bản sao giá và tên sản phẩm.Catalog prices change over time. Invoices, refunds and reconciliation must use the price at order time, so the order keeps its own copy of price and name.
Cả hai đọc version 0. Bên commit trước tăng lên 1; bên sau UPDATE … WHERE version = 0 nhận 0 dòng, đọc lại đơn và áp quy tắc theo trạng thái mới, ví dụ PAID thì chuyển sang luồng hoàn tiền.Both read version 0. The first commit moves it to 1; the other UPDATE … WHERE version = 0 hits 0 rows, re-reads the order and applies the rule for the new status, e.g. PAID goes to the refund flow.
Rollback xoá như chưa từng xảy ra trong một DB. Bước bù là một nghiệp vụ mới làm ngược tác dụng (trả hàng về kho, hoàn tiền), để lại dấu vết và phải idempotent vì có thể chạy lại.A rollback undoes work inside one DB as if it never happened. A compensation is a new business action that reverses the effect (release stock, refund), leaves a trace and must be idempotent because it may run again.
Client sinh clientOrderId trước khi gửi và dùng lại khi retry. DB có unique (user_id, client_order_id); lần thứ hai bắt lỗi unique và trả lại đơn đã tạo với cùng kết quả.The client generates a clientOrderId before sending and reuses it on retry. The DB has unique (user_id, client_order_id); the second call catches the unique violation and returns the existing order with the same result.
Phím 1–4 để chọn đáp án, Enter để kiểm tra.Press 1–4 to choose an answer, Enter to check.
Ôn tập ngắt quãng (spaced repetition): thẻ được lặp lại sau 1 → 3 → 7 → 14 → 30 ngày để củng cố trí nhớ dài hạn.Spaced repetition: cards are repeated after 1 → 3 → 7 → 14 → 30 days to build long-term memory.