0
0
Firebasecloud~10 mins

Transaction basics in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Transaction basics
Start Transaction
Read Data Snapshot
Apply Update Logic
Attempt Commit
Commit
A transaction reads data, applies changes, tries to commit, and retries if conflicts occur.
Execution Sample
Firebase
db.runTransaction(transaction => {
  return transaction.get(docRef).then(doc => {
    const newCount = doc.data().count + 1;
    transaction.update(docRef, { count: newCount });
  });
});
This code reads a document's count, increments it by one, and updates it atomically.
Process Table
StepActionData ReadUpdate AppliedCommit AttemptResult
1Start transactionN/AN/ANoTransaction started
2Read document snapshot{"count": 5}N/ANoData read successfully
3Calculate new count5count = 6NoUpdate prepared
4Apply update5count = 6NoUpdate staged
5Attempt commit5count = 6YesCommit successful
6Transaction endsN/AN/ANoTransaction complete
💡 Commit successful, transaction ends without retry
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
countundefined5666
transactionStatenot startedstartedupdate preparedupdate stagedcommitted
Key Moments - 3 Insights
Why does the transaction retry sometimes?
If the commit fails due to data changes by others, the transaction retries from reading data again (see concept_flow and step 5 in execution_table).
Is the data read inside the transaction always the latest?
Yes, the transaction reads the latest data snapshot at step 2 before applying updates, ensuring consistency.
What happens if the update logic depends on stale data?
The transaction will detect conflicts at commit and retry, so the update logic always runs on fresh data (see retry path in concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after step 3?
A5
B6
Cundefined
D7
💡 Hint
Check the 'Data Read' and 'Update Applied' columns at step 3 in execution_table.
At which step does the transaction try to save changes to the database?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for 'Attempt commit' column in execution_table.
If the commit fails, what happens next according to the concept_flow?
ATransaction ends immediately
BUpdate is discarded without retry
CTransaction retries from reading data again
DTransaction commits anyway
💡 Hint
See the retry loop in the concept_flow ASCII diagram.
Concept Snapshot
Transaction basics in Firebase:
- Start transaction
- Read latest data snapshot
- Apply update logic
- Attempt commit
- If commit fails, retry transaction
- Ensures atomic, consistent updates
Full Transcript
A Firebase transaction starts by reading the latest data snapshot. Then it applies update logic based on that data. Next, it attempts to commit the changes. If the commit succeeds, the transaction ends. If the commit fails due to concurrent changes, the transaction retries from reading data again. This process ensures atomic and consistent updates to the database.