# 💸 Xử lý giao dịch: validation và tính nhất quán

By Everwise Intern

> This lesson is premium content. Sign in and purchase the course to view the full content.

## Mục tiêu: một lệnh chuyển tiền chỉ được áp dụng đúng một lần

Bài trước bạn đã dựng khung project axum, có router, `AppState` và handler trả về dữ liệu giả. Giờ ta làm phần khó nhất của một service ngân hàng: nhận lệnh chuyển tiền sao cho tiền chỉ rời khỏi tài khoản nguồn đúng một lần, kể cả khi client retry, load balancer gửi lại, hay hai request chạy song song.

Ở môi trường thật, phần lớn "trừ tiền hai lần" không đến từ code lỗi rõ ràng, mà từ một trong ba thứ: timeout mạng khiến client gửi lại, `SELECT balance` rồi `UPDATE` tạo khoảng hở giữa đọc và ghi, và idempotency key được ghi nhận ở ngoài transaction. Ba cạm bẫy đó là toàn bộ nội dung bài này.

## Spec: một transfer hợp lệ cần những gì

Chốt spec trước khi viết code. Một lệnh chuyển tiền hợp lệ phải thỏa:

- `amount_minor > 0`, lưu bằng `i64` theo đơn vị nhỏ nhất, không dùng `f64`.
- `from != to`, và hai account phải tồn tại, cùng `Currency`.
- Request bắt buộc có `idempotency_key`, unique trong hệ thống.
- Số dư nguồn đủ tại đúng thời điểm trừ tiền, kiểm tra nguyên tử.
- Toàn bộ claim key, trừ tiền, cộng tiền, ghi receipt nằm trong một DB transaction.

Bắt đầu bằng validation thuần, không chạm DB, để test được bằng `cargo test` mà không cần Postgres:

## In this lesson

- Lộ trình: validation, claim key, trừ tiền, commit
- Nghiệm thu: khi nào coi là xong
- Cạm bẫy khi làm transfer bằng Rust
- Chốt lại


---

## Contents

Part of [🦀 Rust core cho fintech: nắm ownership để làm codebase tài chính](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh.md)

- **🦀 Rust core cho fintech**
  - [🧠 Ownership và borrowing khi xử lý tiền](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/ownership-borrowing-xu-ly-tien.md)
  - [🔗 Lifetimes và domain model tài chính](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/lifetimes-domain-model-tai-chinh.md) (sign-in required)
  - [🧩 Traits và generics để trừu tượng hóa](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/traits-generics-truu-tuong-hoa.md)
  - [✅ Checkpoint: Rust core cho fintech](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/checkpoint-rust-core-fintech.md)
- **🛡️ Error handling và concurrency an toàn**
  - [🚨 Result thay cho panic trong luồng tiền](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/result-khong-panic-luong-tien.md)
  - [⚡ Async/await, Tokio và chia sẻ state an toàn](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/async-tokio-chia-se-state-an-toan.md)
  - [✅ Checkpoint: Error handling và concurrency](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/checkpoint-error-handling-concurrency.md)
- **🏦 Capstone: service ngân hàng bằng Rust**
  - [🧱 Dựng API với axum và cấu trúc project](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/dung-api-axum-cau-truc-project.md)
  - [💸 Xử lý giao dịch: validation và tính nhất quán](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/xu-ly-giao-dich-validation-nhat-quan.md) (premium content) — this lesson
  - [🗄️ Kết nối database, test và quan sát lỗi](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/ket-noi-database-test-quan-sat-loi.md)
  - [✅ Checkpoint: Capstone service ngân hàng](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/checkpoint-capstone-service-ngan-hang.md)
- [Course cheatsheet](https://everwise.study/courses/rust-core-cho-fintech-nam-ownership-de-lam-codebase-tai-chinh/cheatsheet.md) (premium content)
