All jobs / Backend developer

Backend developer interview question · 3–5 years

Two requests read a balance of 100, each subtracts 30, and the balance ends at 70. What happened and what fixes it?

The strongest answer

A lost update: both read then wrote; fix with a single UPDATE that does the arithmetic in the database (SET balance = balance - 30 WHERE balance >= 30), optimistic locking with a version column, or SELECT ... FOR UPDATE

Why it works

Right. Let the database do the arithmetic, or lock or version the row.

What a good answer covers

Answers that fall short

Who gets asked this

More Backend developer questions