db-concurrency
Skill1 starsChanged 5 months agoMITView on GitHub
Keeps money and counter writes correct under concurrent access on Django 5.2 / PostgreSQL — transaction.atomic plus select_for_update to lock the row, F() expressions for read-modify-write counters, and Decimal with quantize for money. Use when writing a debit/credit or balance update, an inventory/stock decrement, a "spent count" or quota counter, a Celery task that mutates shared rows, or when fixing a lost-update, oversell, or race-condition bug. Not for schema changes and adding constraints (see migrations) or async task orchestration itself (see celery-tasks).
In this repository49 other files it gives its agents.
- accessibilitySkill
- ai-evalsSkill
- ai-integrationSkill
- architecture-decisionsSkill
- audit-loggingSkill
- aws-servicesSkill
- browser-e2e-testingSkill
- celery-tasksSkill
- changelogSkill
- ci-cdSkill
- codebase-guideSkill
- code-reviewSkill
- commit-messageSkill
- cookie-auth-csrfSkill
- dashboard-data-designSkill
- debugSkill
- dependency-auditSkill
- deploy-awsSkill
- design-systemSkill
- drf-apiSkill
- drf-zod-contractSkill
- frontend-perfSkill
- git-workflowSkill
- i18n-rtlSkill
- incident-responseSkill
- migrationsSkill
- money-decimalSkill
- multi-tenancySkill
- nextjs-moduleSkill
- observabilitySkill
- perf-reviewSkill
- production-readinessSkill
- product-ux-designSkill
- rbac-permissionsSkill
- react-querySkill
- security-reviewSkill
- skill-authoringSkill
- tailwind-shadcnSkill
- tenant-session-switchSkill
- threat-modelingSkill
- verifySkill
- version-checkSkill
- visual-ui-designSkill
- webhook-handlerSkill
- websockets-channelsSkill
- write-testsSkill
- zod-formsSkill
- zustand-stateSkill
- example-skillSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 25
Keep the transaction short: No network/AI/HTTP calls, no time.sleep, no email inside the lock — hold it only for the read, check, and write.
- Very specific: line 61
Do not use F() when you must reject the write on a condition (e.g.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
DB concurrency (money & counters under concurrent writes)
When to use
Any write that reads a value, computes a new one, and writes it back — balances, stock, quotas, "used" counters — where two requests or workers can run at once. Without a lock the second write silently overwrites the first (lost update); with money, a float or an un-revalidated check turns that into a wrong ledger.
Pattern
Four rules, held together:
- Lock the row. Wrap the read-modify-write in
transaction.atomic()and select the row withselect_for_update()— Postgres holds a row lock until commit, so concurrent writers queue instead of racing. - Validate inside the lock. Re-check the invariant (sufficient balance, stock >= 0) after acquiring the lock, on the freshly-read value — a check before the lock is already stale.
- Decimal, never float, for money. Compute in
Decimalandquantize()to a fixed scale; float rounding corrupts ledgers. - Keep the transaction short. No network/AI/HTTP calls, no
time.sleep, no email inside the lock — hold it only for the read, check, and write.
For a pure integer counter with no cross-value invariant, an F() expression does the
increment atomically in the database without a lock at all.
Pattern — worked example
from decimal import Decimal
from django.db import transaction
from django.db.models import F
from rest_framework.exceptions import ValidationError
def debit_account(account_id, amount: Decimal, entreprise):
if not amount.is_finite() or amount <= 0: # reject NaN/Inf and non-positive input:
raise ValidationError("Amount must be a positive number") # a negative debit CREDITS
amount = amount.quantize(Decimal("0.01")) # money = Decimal, fixed scale
if amount <= 0: # e.g. 0.004 rounds to 0.00
raise ValidationError("Amount rounds to zero at the currency scale")
with transaction.atomic(): # one short, committed unit
account = (
Account.objects
.select_for_update() # row lock until commit
.get(pk=account_id, entreprise=entreprise) # stay tenant-scoped
)
if account.balance < amount: # VALIDATE INSIDE THE LOCK
raise ValidationError("Insufficient funds")
account.balance -= amount # safe: no one else holds the row
account.save(update_fields=["balance"])
return account
# Lock-free counter: NO invariant to check, so let the DB do read+add atomically.
Post.objects.filter(pk=pk).update(view_count=F("view_count") + 1)Do not use F() when you must reject the write on a condition (e.g. balance can't go
negative) — a bare F() update can't fail-close on that; take the lock instead.
Adapt to your repo
Rename Account/balance/entreprise to your models and tenant FK. Pick the
quantize scale your currency needs ("0.01" for 2 decimals, "0.001" for some).
Confirm the money column is DecimalField(max_digits=…, decimal_places=…), not
FloatField. If you enforce non-negativity, back the lock with a DB CheckConstraint
(see migrations) so the invariant holds even for writes that skip this path.
Gotchas
select_for_update()requires being insideatomic()and does not work under autocommit — outside a transaction it raisesTransactionManagementError..get()afterselect_for_update()— never read the row unlocked first, then lock; the first read is stale by the time you lock.- A check done before
select_for_update()proves nothing; re-read and re-check after. - Validate the input (amount > 0, finite) before opening the transaction, and the
state invariant (sufficient balance) after locking — a negative amount turns a
debit into a credit, and no row lock will catch it.
balance < amountis False for a negative amount, so the guard passes andbalance -= amountadds money. select_for_update(skip_locked=True)/nowait=Trueare for queue-style claiming, not for balances — a skipped row means the update never happened.- Long transactions block other writers and exhaust connections — never call an
external API or
Anthropic/Tavilyinside the lock; do that work, then open a short transaction to persist (seecelery-tasks). - Float creeps in via
float(...)or JSON parsing — coerce request input toDecimal(orstrfirst) before arithmetic.
See also
migrationscelery-tasksdrf-api
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

