📖 Scenario: You are building a simple app that counts how many times users click a button. To handle many users clicking at the same time without slowing down, you will use the distributed counters pattern in Firebase.
🎯 Goal: Create a Firebase Firestore structure to store distributed counters, configure the number of shards, write code to increment the counter safely, and finalize the counter document with the total count.
📋 What You'll Learn
Create a Firestore collection with a document for the counter
Add a configuration for the number of shards
Write a function to increment a random shard's count
Add a field to store the total count in the main counter document
💡 Why This Matters
🌍 Real World
Distributed counters help apps handle many users updating the same count without conflicts or slowdowns.
💼 Career
Understanding distributed counters is important for building scalable cloud applications that handle high traffic.
Progress0 / 4 steps
1
Create the main counter document
Create a Firestore collection called counters and add a document with ID clicks that has a field totalCount set to 0.
Firebase
Hint
Use firestore.collection('counters').doc('clicks').set({ totalCount: 0 }) to create the document.
2
Add shard count configuration
Create a constant called NUM_SHARDS and set it to 5 to define how many shards the counter will use.
Firebase
Hint
Use const NUM_SHARDS = 5; to set the shard count.
3
Write function to increment a random shard
Write a function called incrementCounter that picks a random shard number from 0 to NUM_SHARDS - 1 and increments the count field in that shard document inside counters/clicks/shards collection using a transaction.
Firebase
Hint
Use Math.floor(Math.random() * NUM_SHARDS) to pick a shard. Use a transaction to safely update the shard's count.
4
Add total count field update
Add a field totalCount to the clicks document that will store the total count of all shards. Update the incrementCounter function to increment this totalCount field inside the same transaction.
Firebase
Hint
Inside the transaction, get the main counter document and update its totalCount by adding 1.
Practice
(1/5)
1. What is the main reason to use the distributed counters pattern in Firebase?
easy
A. To avoid write conflicts by splitting counts into multiple shards
B. To store large files efficiently
C. To speed up user authentication
D. To reduce network bandwidth usage
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 A
Quick Check:
Distributed counters = avoid conflicts [OK]
Hint: Distributed counters split counts to avoid conflicts [OK]
Common Mistakes:
Thinking it speeds up authentication
Confusing with file storage
Assuming it reduces bandwidth
2. Which Firebase data structure is typically used to implement shards in a distributed counter?
easy
A. A single document with a numeric field
B. A collection of user profiles
C. Multiple documents each representing a shard
D. A storage bucket
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 C
Quick Check:
Shards = multiple documents [OK]
Hint: Shards are separate documents, not single fields [OK]
Common Mistakes:
Using a single document for all shards
Confusing shards with user profiles
Thinking shards are stored in storage buckets
3. Given this code snippet to increment a distributed counter shard in Firebase Firestore, what is the expected behavior?