Field Manual PHP / Laravel reference
← Financial-system patterns

Atomic operations

Atomic operations either apply completely or have no effect.

What it is

Atomic operations either apply completely or have no effect.

When to use it

Protect balance movement and its durable record as one unit.

Syntax

DB::transaction(function () {
    // debit + credit + transaction record + audit
});

Example

// Validate positive amount and matching currencies first.
// Inside transaction:
// 1. Conditional debit, assert affected rows = 1.
// 2. Credit destination, assert affected rows = 1.
// 3. Insert transfer and audit rows.
// Any exception must escape to roll back all writes.

Common mistakes

Atomicity does not alone prevent lost updates or duplicate effects.

Related topics

Browse Financial-system patterns