1Caller gọi MomoClient.charge(req) cho ORD-1001; lời gọi đi qua proxy @Retryable nên chính sách thử lại nằm ngoài code nghiệp vụ. maxAttempts = 4 gồm cả lần gọi đầu.The caller invokes MomoClient.charge(req) for ORD-1001; the call passes through the @Retryable proxy so retry policy stays outside business code. maxAttempts = 4 includes the initial call.
2Lần 1: charge() gửi ChargeRequest có requestId cố định tới Momo API, nhận 503. onStatus ném MomoUnavailableException, nằm trong retryFor nên proxy sẽ thử lại.Attempt 1: charge() sends the ChargeRequest with a stable requestId to the Momo API and gets 503. onStatus throws MomoUnavailableException, which is in retryFor, so the proxy will retry.
3Proxy hỏi @Backoff(delay = 200, multiplier = 2, random = true) và chờ 280ms, một giá trị ngẫu nhiên quanh mức nền 200ms. Jitter giúp nhiều instance không thử lại cùng một nhịp.The proxy asks @Backoff(delay = 200, multiplier = 2, random = true) and waits 280ms, a randomized value around the 200ms base. Jitter keeps instances from retrying in lockstep.
4Lần 2 gửi lại đúng ChargeRequest cũ nhưng timeout, thành ResourceAccessException (cũng nằm trong retryFor). Chưa thể kết luận tiền chưa bị trừ, nên tuyệt đối không đổi requestId.Attempt 2 resends the same ChargeRequest but times out as a ResourceAccessException (also in retryFor). The charge may already have happened, so requestId must not change.
5Mức nền tăng lên 400ms (multiplier = 2), lần này chọn 610ms, không vượt maxDelay = 2000. maxAttempts không tự giới hạn tổng thời gian, nên vẫn cần deadline riêng.The base doubles to 400ms (multiplier = 2), this time producing 610ms, never above maxDelay = 2000. maxAttempts alone does not bound total time, so a separate deadline is still needed.
6Lần 3 gửi cùng requestId; Momo trả 200 và proxy trả ChargeResult về caller ngay, không dùng nốt lượt thứ tư. Nếu lần 2 thực ra đã trừ tiền, cơ chế chống trùng trả lại đúng giao dịch cũ.Attempt 3 reuses requestId; Momo returns 200 and the proxy hands ChargeResult back to the caller at once, skipping the fourth attempt. If attempt 2 had actually charged, deduplication returns that same transaction.
7Nhánh khác: nếu cả 4 lần đều lỗi, proxy gọi markPending(ex, req) có @Recover, trả ChargeResult.pending(orderId, message). PENDING nghĩa là chưa rõ kết quả, cần đối soát, không phải chắc chắn chưa trừ tiền.Alternative branch: if all 4 attempts fail, the proxy calls the @Recover method markPending(ex, req), returning ChargeResult.pending(orderId, message). PENDING means the outcome is unknown and needs reconciliation, not that no charge happened.
8Bản Go: MomoClient.Charge gọi retry.Do với Policy{MaxAttempts: 4, Base: 200ms, Max: 2s} và tạo request mới mỗi lần. 503 trả lỗi thường nên Do chờ full jitter rand.N(ceiling + 1) qua timer/select; 400 bị bọc &retry.Permanent nên errors.As thấy và dừng ngay.Go: MomoClient.Charge calls retry.Do with Policy{MaxAttempts: 4, Base: 200ms, Max: 2s} and builds a fresh request each attempt. A 503 returns a plain error, so Do waits a full-jitter rand.N(ceiling + 1) via timer/select; a 400 is wrapped in &retry.Permanent, so errors.As finds it and stops at once.
9Biến thể Resilience4j: ShipmentService.createShipment gọi retry.executeCallable(() -> ghn.createOrder(req)) với Retry "ghn" (5 lần, IntervalFunction 300ms x2 ±50%, trần 5s). IOException được thử lại và log onRetry; InvalidAddressException bị bỏ qua. Go CreateShipment làm tương tự bằng backoff.Retry và ngân sách 10s.The Resilience4j variant: ShipmentService.createShipment calls retry.executeCallable(() -> ghn.createOrder(req)) with Retry "ghn" (5 attempts, IntervalFunction 300ms x2 ±50%, capped at 5s). IOException is retried and logged via onRetry; InvalidAddressException is ignored. Go's CreateShipment does the same with backoff.Retry and a 10s budget.