0
0
Firebasecloud~10 mins

Transaction read-then-write pattern in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Transaction read-then-write pattern
Start Transaction
Read current data
Check or calculate new data
Write updated data
Commit Transaction
End Transaction
This flow shows how a transaction reads data first, then writes updated data, ensuring consistency.
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 writes it back atomically.
Process Table
StepActionData ReadData WrittenTransaction State
1Start transactionN/AN/AOpen
2Read document{ count: 5 }N/AOpen
3Calculate new count5N/AOpen
4Write updated countN/A{ count: 6 }Open
5Commit transactionN/ACommitted { count: 6 }Committed
6End transactionN/AN/AClosed
💡 Transaction ends after commit, ensuring atomic read-modify-write.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
countundefined5566
transactionStateNot startedOpenOpenOpenCommitted
Key Moments - 2 Insights
Why do we read data before writing in a transaction?
Because the transaction needs the current data to calculate the new value before writing, as shown in step 2 and 3 of the execution table.
What happens if the data changes between read and write?
The transaction automatically retries to ensure consistency, so the read-then-write pattern always works with the latest data, as implied by the transaction state in the table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after step 3?
A5
B6
Cundefined
DN/A
💡 Hint
Check the 'Data Read' and 'Data Written' columns at step 3 in the execution table.
At which step does the transaction commit the new data?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Transaction State' column for the 'Committed' status in the execution table.
If the initial count was 10 instead of 5, what would be the 'Data Written' at step 4?
A{ count: 5 }
B{ count: 6 }
C{ count: 11 }
D{ count: 10 }
💡 Hint
Refer to the variable_tracker for how 'count' changes after calculation.
Concept Snapshot
Transaction read-then-write pattern:
- Start a transaction
- Read current data inside transaction
- Calculate new data based on read
- Write updated data
- Commit transaction atomically
Ensures data consistency and avoids conflicts.
Full Transcript
This visual execution shows the transaction read-then-write pattern in Firebase. The transaction starts, reads the current document data (count = 5), calculates the new count (5 + 1 = 6), writes the updated count, commits the transaction, and ends. Variables like 'count' and 'transactionState' change step by step. Key points include reading before writing to get current data and committing atomically to avoid conflicts. The quizzes test understanding of variable values at steps and transaction commit timing.