0
0
Firebasecloud~10 mins

Distributed counters pattern in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Distributed counters pattern
Start increment request
Select a shard randomly
Increment counter in selected shard
Save updated shard count
Repeat increments as needed
To get total count
Read all shards
Sum all shard counts
Return total count
Each increment updates one shard randomly to reduce contention. Total count is sum of all shards.
Execution Sample
Firebase
function incrementCounter() {
  const shardId = randomShard();
  const shardRef = db.collection('counters').doc(shardId);
  shardRef.update({count: firebase.firestore.FieldValue.increment(1)});
}

function getTotalCount() {
  return db.collection('counters').get().then(sumShards); 
}
Increment updates one shard; total count sums all shards.
Process Table
StepActionShard SelectedShard Count BeforeShard Count AfterTotal Count Calculation
1Increment request received----
2Select shard randomlyshard_3---
3Read shard_3 countshard_35--
4Increment shard_3 countshard_356-
5Save updated shard_3 countshard_3-6-
6Increment request received----
7Select shard randomlyshard_1---
8Read shard_1 countshard_13--
9Increment shard_1 countshard_134-
10Save updated shard_1 countshard_1-4-
11Request total count----
12Read all shards counts---shard_1=4, shard_2=2, shard_3=6
13Sum all shards---4 + 2 + 6 = 12
14Return total count---12
💡 Total count returned after summing all shard counts.
Status Tracker
VariableStartAfter Step 4After Step 9After Step 13
shard_1_count3344
shard_2_count2222
shard_3_count5666
total_count00012
Key Moments - 3 Insights
Why do we update only one shard per increment instead of all shards?
Updating one shard reduces conflicts and improves performance, as shown in steps 2-5 and 7-10 where only one shard is incremented each time.
How is the total count calculated from shards?
The total count is the sum of all shard counts, as shown in steps 12-14 where all shards are read and summed.
What happens if two increments select the same shard at the same time?
Firebase handles concurrent updates safely with atomic increments, so counts are correctly updated without loss, as implied in steps 3-5 and 8-10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the count of shard_3 after step 4?
A5
B4
C6
D3
💡 Hint
Check the 'Shard Count After' column at step 4 in the execution_table.
At which step does the total count become available?
AStep 14
BStep 12
CStep 11
DStep 10
💡 Hint
Look at the 'Total Count Calculation' column in the execution_table for when the total is returned.
If increments always selected the same shard, what would change in the execution_table?
ATotal count would not change
BOnly one shard's count would increase repeatedly
CAll shards would increase evenly
DShards would be deleted
💡 Hint
Refer to the 'Shard Selected' column in the execution_table to see shard selection impact.
Concept Snapshot
Distributed counters split count into shards.
Each increment updates one shard randomly.
Total count is sum of all shards.
Reduces write conflicts and scales better.
Use atomic increments for safe updates.
Full Transcript
The distributed counters pattern splits a counter into multiple shards to reduce write conflicts. When an increment request happens, the system picks one shard randomly and increments its count atomically. This avoids many clients trying to update the same document at once. To get the total count, the system reads all shards and sums their counts. This pattern improves performance and scalability in Firebase by distributing load. The execution table shows increments updating individual shards and the total count calculated by summing all shards. Variables track shard counts changing step-by-step. Key moments clarify why only one shard is updated per increment and how totals are computed. The visual quiz tests understanding of shard counts and total calculation steps.