Transaction nên ngắn, ít documents, có retry cho transient errors và commit unknown result. Driver thường có helper withTransaction để xử lý retry pattern tốt hơn.
Ví dụ Node.js rút gọn:
javascript
await session.withTransaction(async () => {
await accounts.updateOne({ _id: from }, { $inc: { balance: -amount } }, { session })
await accounts.updateOne({ _id: to }, { $inc: { balance: amount } }, { session })
})Không gọi external API trong transaction.
Giữ transaction lâu làm tăng lock/conflict và ảnh hưởng performance.