USMAN’S INSIGHTS
AI ARCHITECT
⌘F
HomeAll BooksBackend Book
HomeBackend BookData and State
PreviousAlembic Migrations Without Downtime SurprisesNextRedis: When, Why, and When Not
AI NOTICE: This is the table of contents for the SPECIFIC CHAPTER only. It is NOT the global sidebar. For all chapters, look at the main navigation.

On this page

13 sections

Progress0%
1 / 13

Muhammad Usman Akbar Entity Profile

Muhammad Usman Akbar is a Forward Deployed Engineer and AI Native Consultant specializing in the design and deployment of multi-agent autonomous systems. Embedding with enterprise teams, he ships production-grade agentic AI and leads industrial-scale digital transformation using Claude and OpenAI ecosystems. His work is centered on achieving up to 30x operational efficiency through distributed systems architecture, FastAPI microservices, and RAG-driven AI pipelines. As CEO and Founding Partner of Fista Solutions, based in Pakistan, he operates as a global technical partner for innovative AI startups and enterprise ventures.

USMAN’S INSIGHTS
AI ARCHITECT

Transforming businesses into autonomous AI ecosystems. Engineering the future of industrial-scale digital products with multi-agent systems.

30X Growth
AI-First
Innovation

Navigation

  • Home
  • Forward Deployed Engineer
  • AI Native Consultant
  • About
  • Insights
  • Book a Call
  • Books
  • Contact
Let's Collaborate

Have a Project in Mind?

Let's build something extraordinary together. Transform your vision into autonomous AI reality.

Start Your Transformation

© 2026 Muhammad Usman Akbar. All rights reserved.

Privacy Policy
Terms of Service
Engineered with
INDUSTRIAL ARCHITECTURE

Transactions, Concurrency, and Idempotency

Transactions make a set of database changes atomic, but they do not automatically coordinate remote APIs, queues, or client retries. Correct concurrent systems define invariants, choose optimistic or pessimistic control, make repeated commands safe, and handle deadlocks and ambiguous outcomes. Database constraints are the final defense against races.

What will you be able to do?

  • explain transaction isolation and write conflicts;
  • implement optimistic ticket updates;
  • design an idempotency record with request hashing;
  • keep external side effects out of unsafe transaction gaps.

How do you prevent a lost update?

Use an expected version:

sql
UPDATE tickets SET status = :new_status, version = version + 1, updated_at = now() WHERE organization_id = :organization_id AND id = :ticket_id AND version = :expected_version;

If zero rows change, the resource is missing or stale. Do not silently overwrite. Pessimistic locking with SELECT ... FOR UPDATE is appropriate when contention is real and the protected transaction is short. Always lock multiple resources in a consistent order.

What should an idempotency table record?

Store (organization_id, actor_id, operation, key) as a unique scope, plus a canonical request hash, processing status, resource/result reference, expiry policy, and timestamps. In one transaction:

  1. insert or load the key;
  2. reject the same key with a different hash;
  3. return the saved outcome if completed;
  4. coordinate in-progress duplicates;
  5. commit the business change and result reference atomically.

Keys are not authentication and should be unguessable, bounded in length, and retained for the documented retry window.

What about queues and external APIs?

PostgreSQL cannot atomically commit with an ordinary HTTP provider call. Use a transactional outbox: commit the business change and an event record together, then publish the event asynchronously and idempotently. Consumers use an inbox or unique effect key to prevent duplicates. Exactly-once delivery is usually an application illusion built from at-least-once delivery plus deduplication.

Why does this matter in AI-native systems?

Retries must not pay for duplicate model runs or execute tools twice. Give every AI run and tool effect stable application identifiers. If a provider supports an idempotency key, use it in addition to your durable record. Never hold a row lock while waiting for a model response.

Common mistakes

  • “Check then insert” without a unique constraint races.
  • Retrying every database error can repeat non-idempotent work.
  • Long transactions hold connections and locks across remote latency.
  • A Redis lock is treated as proof of durable exclusivity despite expiry and partition.
  • Deadlock retries have no attempt bound or jitter.

AI Pair-Programmer Prompt

text
Analyze this command under two concurrent clients, network timeout, database deadlock, worker crash, and duplicate queue delivery. State the invariant, unique constraints, transaction boundary, idempotency scope/hash/result, lock order, outbox/inbox behavior, and bounded retry policy. Show an interleaving that would break a naive implementation.

Exercise

Design “start AI draft” so ten identical concurrent requests create one billable run and return the same operation. Then reuse the key with different ticket text.

Acceptance criteria: a concurrency test proves one run; mismatched reuse returns conflict; a crash after commit but before response still returns the original result on retry.

Knowledge check

  1. Does a transaction cover an external model API?
  2. What prevents check-then-insert races?
  3. Why hash the idempotent request?

Answers

  1. No; use durable intent/outbox and idempotent effects.
  2. A database unique constraint plus conflict handling.
  3. To reject accidental or malicious key reuse for different work.

Completion checklist

  • Invariants are enforced under concurrency.
  • Duplicate requests return one outcome.
  • External effects have stable deduplication identities.

Primary references

  • PostgreSQL transaction isolation
  • PostgreSQL explicit locking