AgentStack
Browse Sign in
Browse Why AgentStack Sell Docs
Sign in
SKILL verified MIT Self-run

Db Concurrency

skill-deadlymind-nanolama-db-concurrency · by Deadlymind

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…

No reviews yet
0 installs
31 views
0.0% view→install

Install

$ agentstack add skill-deadlymind-nanolama-db-concurrency

✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

View the full security report →

Verified badge

Passed review? Show it. Paste this badge into your README, it links to the public security report.

AgentStack Verified badge Links to your public security report.
[![AgentStack Verified](https://agentstack.voostack.com/badges/verified.svg)](https://agentstack.voostack.com/security/report/skill-deadlymind-nanolama-db-concurrency)

Reliability & compatibility

Security review passed
0 installs to date
no reviews yet
2mo ago

Declared compatibility

Claude CodeClaude Desktop

Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.

Preview Execution monitoring

We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.

How agent discovery & health will work →
Are you the author of Db Concurrency? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

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:

  1. Lock the row. Wrap the read-modify-write in transaction.atomic() and select

the row with select_for_update() — Postgres holds a row lock until commit, so concurrent writers queue instead of racing.

  1. 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.

  1. Decimal, never float, for money. Compute in Decimal and quantize() to a fixed

scale; float rounding corrupts ledgers.

  1. 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, 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 < amount` is False for
  a negative amount, so the guard passes and `balance -= amount` adds money.
- `select_for_update(skip_locked=True)` / `nowait=True` are 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`/`Tavily` inside the lock; do that work, then open a short
  transaction to persist (see `celery-tasks`).
- Float creeps in via `float(...)` or JSON parsing — coerce request input to `Decimal`
  (or `str` first) before arithmetic.

## See also
- `migrations`
- `celery-tasks`
- `drf-api`

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [Deadlymind](https://github.com/Deadlymind)
- **Source:** [Deadlymind/nanolama](https://github.com/Deadlymind/nanolama)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet, be the first.

Versions

  • v0.1.0 Imported from the upstream source.