Trade operations are wrapped inside database transactions ($transaction). When a user buys an asset, the wallet deduction, transaction logging, and weighted average price (avgPrice) holding updates happen as a single atomic unit. If anything fails, it rolls back cleanly.