Firestore Transactions

معاملات Firestore

Intermediatedatabase1 min read
Firestore transactionatomic operationrunTransaction

Definition

A set of Firestore read and write operations that all succeed together or all fail together — used when multiple documents must stay in sync.

مجموعة من عمليات القراءة والكتابة في Firestore تنجح معاً أو تفشل معاً — تُستخدم عندما يجب أن تظل مستندات متعددة متزامنة.

Why It Matters

404Fault's glossary counter (`meta/glossary_counter`) uses a transaction every time a batch is seeded. Without the transaction, two simultaneous seed operations could assign overlapping GLS IDs, causing document collisions. Transactions are the correct pattern for any shared mutable state in Firestore.

عدّاد قاموس 404Fault (`meta/glossary_counter`) يستخدم معاملة في كل مرة تُبذر دفعة. بدون المعاملة، يمكن لعمليتي بذر متزامنتين تخصيص معرّفات GLS متداخلة مما يُسبّب تصادمات مستندات. المعاملات هي النمط الصحيح لأي حالة قابلة للتغيير ومشتركة في Firestore.

Full Definition

A Firestore transaction is an atomic operation — a sequence of reads and writes that either all commit or all roll back if any step fails. Transactions are essential for operations that depend on current values, like incrementing a counter: `runTransaction(async (tx) => { const doc = await tx.get(ref); const current = doc.data().count; tx.set(ref, { count: current + 1 }); })`. If another write changes `count` between the read and the write, Firestore automatically retries the transaction. Without transactions, concurrent writes can cause race conditions and lost updates.
معاملة Firestore هي عملية ذرية — تسلسل من القراءات والكتابات إما تُودَع جميعاً أو تُراجَع جميعاً إذا فشل أي خطوة. المعاملات ضرورية للعمليات التي تعتمد على القيم الحالية، مثل تزايد عدّاد: `runTransaction(async (tx) => { const doc = await tx.get(ref); const current = doc.data().count; tx.set(ref, { count: current + 1 }); })`. إذا غيّرت كتابة أخرى `count` بين القراءة والكتابة، تُعيد Firestore المحاولة تلقائياً. بدون المعاملات، قد تُسبّب الكتابات المتزامنة حالات تسابق وتحديثات مفقودة.

Example Usage

Counter increment in 404Fault seed routes: `await db.runTransaction(async (tx) => { const snap = await tx.get(counterRef); const current = snap.exists ? snap.data().count : 155; tx.set(counterRef, { count: current + toSeed.length }, { merge: true }); });` — atomic: no two concurrent seeds can get the same starting counter value.

تزايد العدّاد في مسارات البذر في 404Fault: `await db.runTransaction(async (tx) => { const snap = await tx.get(counterRef); const current = snap.exists ? snap.data().count : 155; tx.set(counterRef, { count: current + toSeed.length }, { merge: true }); });` — ذري: لا يمكن لعمليتي بذر متزامنتين الحصول على نفس قيمة العدّاد الابتدائية.

Knowledge Graph

Avoid these mistakes when using Firestore Transactions:

1

Performing side effects (API calls, sending emails) inside a transaction — if the transaction retries, the side effect runs multiple times

2

Reading a document you don't intend to write inside a transaction — reads inside transactions take a consistent snapshot but add latency; only read what you need

3

Using transactions for write-only operations — if you don't need to read-then-write, use a batch write instead (faster, no contention)

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Firestore Transactions.

Explain:
1. What is Firestore Transactions and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Performing side effects (API calls, sending emails) inside a transaction — if the transaction retries, the side effect runs multiple times, Reading a document you don't intend to write inside a transaction — reads inside transactions take a consistent snapshot but add latency; only read what you need, Using transactions for write-only operations — if you don't need to read-then-write, use a batch write instead (faster, no contention)
5. Best practices and production tips

Official Resources