Why we built transactional actions
Most database bugs that make it to production are not bad queries. They are good queries, run in the wrong order, by two people at once.
Take something as ordinary as approving an expense. The obvious way to build it from the page is three requests: read the expense, check the budget has enough left, then write the new "approved" status and the new remaining balance. Each request on its own is correct. Put them next to a real user and a flaky connection, though, and the cracks show immediately. The second request fails after the first commits, and now the expense is approved with nobody having checked the budget. Two approvers click the button within the same second, both read "$400 remaining", both approve a $300 expense, and the budget goes to negative $200 with no error anywhere.
This is not a rare edge case. It is what happens to every multi-step write, eventually, at the exact moment it matters most - the last seat on a flight, the last slot in a calendar, the invoice that should only close once.
The shape of the fix
The fix is not cleverer client code. It is moving the whole operation into the database, as one transaction, so it either fully happens or fully does not. That is what a transactional action is: a named sequence of steps - read a row, check a condition, write a row - that runs from a single request and commits together or rolls back together.
The expense example becomes:
get expense
check expense.status = submitted, else refuse
get budget
check budget.remaining >= expense.amount, else refuse
insert an audit log line
update expense.status = approved (only if it was still submitted)
update budget.remaining -= expense.amount
respond with the new remaining balance
Seven moving parts, one all-or-nothing unit. If the budget check fails, nothing above it is written either, including the audit line. If two approvals race, the database's own row locking makes them take turns rather than both reading the same stale number - the second one to run sees the balance the first one already spent, and its own check catches the shortfall.
The part that is easy to forget
The steps run as the person calling them, not as some backend superuser. Every read and write inside the action is still judged by the table's own access rules, exactly as it would be if that row were touched directly. An action is a way to make several writes atomic, never a way to skip the rule that says who may make them. Get the rules wrong and the action does too - which is exactly why it is worth testing an action the way you would test the rules themselves: run it as someone who should succeed, and run it as someone who should be refused, before either is anywhere near a live button.
What this replaces
If your product has an "approve", a "book", a "close", or an "accept" that quietly touches two rows and you have been writing it as two fetch calls with your fingers crossed, that is the pattern to look for. The rule of thumb: the moment a change is not one row, it is not several requests either.

Comments
Loading comments…