1Lan gọi WalletService.withdraw(W1, 200000, ...). Hàm load() hỏi snapshots.latest(W1) và nhận Snapshot v300, số dư 1.500.000đ, để khỏi replay 300 event đầu.Lan calls WalletService.withdraw(W1, 200000, ...). load() asks snapshots.latest(W1) and gets Snapshot v300 with a 1,500,000 balance, skipping the first 300 events.
2Snapshot chưa phải số dư mới nhất: events.load(W1, 300) đọc các dòng version > 300 theo ORDER BY version. Mỗi dòng (type, payload) được giải mã lại thành MoneyDeposited hay MoneyWithdrawn (Go: decode dùng as[Deposited], as[Withdrawn]).The snapshot is not the latest balance: events.load(W1, 300) reads rows with version > 300 ORDER BY version. Each (type, payload) row is decoded back into MoneyDeposited or MoneyWithdrawn (Go: decode uses as[Deposited], as[Withdrawn]).
3Wallet.rehydrate(snap, history) chép balance và version từ Snapshot rồi apply từng event: case MoneyDeposited cộng, case MoneyWithdrawn trừ, mỗi lần version++. (Stream của W1 bắt đầu bằng WalletOpened ở v1, đã nằm trong snapshot; case WalletOpened chỉ gán id.) Kết quả 1.250.000đ ở v307; apply không kiểm tra quy tắc và không gửi SMS cũ.Wallet.rehydrate(snap, history) copies balance and version from the Snapshot, then applies each event: case MoneyDeposited adds, case MoneyWithdrawn subtracts, version++ each time. (W1's stream began with WalletOpened at v1, already inside the snapshot; case WalletOpened only sets the id.) Result: 1,250,000 at v307; apply re-checks no rules and resends no old messages.
4Service nhớ expectedVersion = 307 rồi gọi wallet.withdraw(200000, reason). Aggregate kiểm tra số tiền dương, đủ số dư, rồi raise(new MoneyWithdrawn(...)): apply trừ tiền trong bộ nhớ và đưa event vào pending.The service remembers expectedVersion = 307 and calls wallet.withdraw(200000, reason). The aggregate checks the amount is positive and affordable, then raise(new MoneyWithdrawn(...)): apply subtracts in memory and the event joins pending.
5events.append(W1, 307, pendingEvents()) INSERT dòng version 308, type MoneyWithdrawn vào wallet_events rồi commit. Event store chỉ INSERT, không bao giờ UPDATE hay DELETE.events.append(W1, 307, pendingEvents()) INSERTs row version 308, type MoneyWithdrawn into wallet_events and commits. The event store only INSERTs, never UPDATEs or DELETEs.
6Nếu một request khác đã ghi v308 trước, UNIQUE(stream_id, version) báo lỗi: Java ném DuplicateKeyException, Go thấy mã 23505 và trả ErrConcurrentWrite. Caller phải tải lại và kiểm tra lại số dư trước khi thử tiếp.If another request already wrote v308, UNIQUE(stream_id, version) fails: Java throws DuplicateKeyException, Go sees code 23505 and returns ErrConcurrentWrite. The caller must reload and recheck the balance before retrying.
7Sau khi append, service so 308 / 100 với 307 / 100: chưa vượt mốc nên không chụp snapshot. Khi một giao dịch sau đưa version từ 399 lên 400, service gọi snapshots.save(wallet.toSnapshot()).After appending, the service compares 308 / 100 with 307 / 100: no boundary crossed, so no snapshot. When a later transaction moves the version from 399 to 400, the service calls snapshots.save(wallet.toSnapshot()).
8Lan thử rút 2.000.000đ khi số dư chỉ còn 1.050.000đ: withdraw() ném InsufficientFundsException (Go: lỗi bọc ErrInsufficientFunds) trước khi raise, nên không có MoneyWithdrawn nào được ghi.Lan tries to withdraw 2,000,000 with only 1,050,000 left: withdraw() throws InsufficientFundsException (Go: an error wrapping ErrInsufficientFunds) before raising, so no MoneyWithdrawn is recorded.