0
0
Firebasecloud~10 mins

Distributed counters pattern in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
When many users update a counter at the same time, it can slow down or cause errors. The distributed counters pattern splits the count into pieces to handle many updates quickly and safely.
When you want to count likes on a popular post without delays or errors.
When many users vote or click a button at the same time and you need an accurate total.
When you track views on a video that gets thousands of views per second.
When you want to avoid your database slowing down because too many updates happen at once.
When you want to keep a total count but allow many small updates to happen separately.
Config File - firestore.rules
firestore.rules
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /counters/{counterId} {
      allow read: if true;
      allow write: if request.auth != null;
    }
    match /counterShards/{shardId} {
      allow read: if true;
      allow write: if request.auth != null;
    }
  }
}

This security rules file allows authenticated users to read and write to the main counter document and its shards. It ensures only signed-in users can update counts, protecting your data from unauthorized changes.

Commands
Initialize Firestore in your Firebase project to start using the database for counters.
Terminal
firebase init firestore
Expected OutputExpected
=== Firestore Setup === You are about to initialize Firestore in your project. Firestore rules and indexes files have been created.
Deploy the Firestore security rules to protect your counters and shards from unauthorized access.
Terminal
firebase deploy --only firestore:rules
Expected OutputExpected
=== Deploying to 'your-project-id'... āœ” firestore: rules deployed successfully Project Console: https://console.firebase.google.com/project/your-project-id/firestore
→
--only firestore:rules - Deploy only Firestore security rules without affecting other services
Run a script that updates one shard of the distributed counter to safely increment the count.
Terminal
node updateCounterShard.js
Expected OutputExpected
Shard updated successfully with increment 1
Run a script that reads all shards and sums their counts to get the total counter value.
Terminal
node getTotalCount.js
Expected OutputExpected
Total count is 1234
Key Concept

If you remember nothing else from this pattern, remember: split a big counter into many small pieces to handle many updates safely and quickly.

Common Mistakes
Updating the main counter document directly for every increment.
This causes conflicts and slows down the database when many users update at once.
Update only one shard document per increment and sum shards to get the total.
Not setting proper security rules for shards.
Anyone could change counts, causing wrong totals or data loss.
Use Firestore rules to allow writes only from authenticated users.
Reading only one shard to get the total count.
You get an incomplete count because the total is spread across shards.
Read all shards and add their counts to get the correct total.
Summary
Initialize Firestore and set security rules to protect your counters.
Use multiple shard documents to split the counter for safe concurrent updates.
Update one shard at a time and sum all shards to get the total count.