Skip to main content

Practice ยท 1 of 2

Optimistic concurrency, simulated

Implement apply_update(row, mutate, conflict_on_attempt=()) โ€” an optimistic-concurrency update over row (a dict containing 'id' and 'version'): - loop up to 3 attempts; on each: seen = row['version'], copy the row, apply mutate(copy) - if the attempt number is in conflict_on_attempt, simulate losing the race: the stored row's version increments (another writer committed) and you retry - otherwise the write lands: the new row keeps the mutations and gets version = seen + 1; return {'ok': True, 'row': new_row, 'attempts': n} - after 3 failed attempts: return {'ok': False, 'attempts': 3} The discriminating check: a conflict must re-read the bumped version, not blindly overwrite with a stale one.

Difficulty: advanced

Back to lesson: Practice: Transaction & Pool Drills