What if your app could count millions of clicks instantly without breaking?
Why Distributed counters pattern in Firebase? - Purpose & Use Cases
Imagine you have a popular app where thousands of users click a button to like a post at the same time. You try to count all these likes by updating a single number stored in your database manually.
When many users update the same number at once, the database gets overwhelmed. Updates can get lost or cause errors because multiple people try to change the number simultaneously. This makes your like count inaccurate and slow.
The distributed counters pattern splits the counting task into many small parts stored separately. Each user updates their own small counter, and the app adds them up when needed. This avoids conflicts and keeps the count accurate and fast.
likes = db.get('likes') likes += 1 db.set('likes', likes)
db.increment('likes_shard_1') db.increment('likes_shard_2') ... total_likes = sum_all_shards()
This pattern lets your app handle huge numbers of simultaneous updates smoothly without losing data or slowing down.
Popular social media apps use distributed counters to keep track of likes, views, or votes in real time, even when millions of users interact at once.
Manual counting fails under heavy simultaneous updates.
Distributed counters split the count to avoid conflicts.
This keeps counts accurate and apps responsive at scale.
Practice
Solution
Step 1: Understand the problem with single counters
Single counters cause write conflicts when many users update at once.Step 2: How distributed counters solve this
Splitting the count into shards lets many users write to different parts, avoiding conflicts.Final Answer:
To avoid write conflicts by splitting counts into multiple shards -> Option AQuick Check:
Distributed counters = avoid conflicts [OK]
- Thinking it speeds up authentication
- Confusing with file storage
- Assuming it reduces bandwidth
Solution
Step 1: Identify how shards are stored
Each shard is stored as a separate document to allow independent updates.Step 2: Why multiple documents
Multiple documents reduce write conflicts by spreading writes across shards.Final Answer:
Multiple documents each representing a shard -> Option CQuick Check:
Shards = multiple documents [OK]
- Using a single document for all shards
- Confusing shards with user profiles
- Thinking shards are stored in storage buckets
const shardId = Math.floor(Math.random() * 5);
const shardRef = firestore.collection('counters').doc('counter1').collection('shards').doc(shardId.toString());
await shardRef.update({ count: firebase.firestore.FieldValue.increment(1) });Solution
Step 1: Understand shard selection
The code picks a random shard ID between 0 and 4 to update.Step 2: What update does
It increments the count field in that shard by 1, avoiding conflicts.Final Answer:
It increments the count in a random shard to reduce write conflicts -> Option AQuick Check:
Random shard increment = reduce conflicts [OK]
- Thinking it updates all shards at once
- Assuming shardId type causes error
- Believing it resets count instead of incrementing
Solution
Step 1: Check how total count is calculated
Total count must be the sum of all shard counts.Step 2: Identify missing sum
If you read only one shard or forget to sum, total will be wrong.Final Answer:
You forgot to sum all shard counts when reading the total -> Option BQuick Check:
Sum all shards for total [OK]
- Assuming incrementing same shard causes wrong total
- Confusing single document with shards
- Thinking random shard increments cause errors
Solution
Step 1: Understand update strategy
Incrementing a random shard spreads writes and avoids conflicts.Step 2: Understand read strategy
Summing all shards gives accurate total, though slightly delayed.Step 3: Why other options fail
Incrementing all shards wastes writes; single document causes conflicts; sequential increments cause bottlenecks.Final Answer:
Increment a random shard on each update and sum all shards when reading total -> Option DQuick Check:
Random increment + sum shards = best balance [OK]
- Trying to increment all shards each time
- Using single document counter under high load
- Incrementing shards in fixed order causing bottlenecks
Ready to prove it?
Pass the test and this pattern moves unknown
