0
0
Firebasecloud~10 mins

Distributed counters pattern in Firebase - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize a distributed counter document reference in Firebase.

Firebase
const counterRef = firestore.collection('counters').doc([1]);
Drag options to blanks, or click blank then click option'
A'counterDoc'
B'counter'
C'distributedCounter'
D'countDoc'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a collection name instead of a document ID.
Leaving the document ID empty or undefined.
2fill in blank
medium

Complete the code to increment a shard's count atomically using a transaction.

Firebase
await firestore.runTransaction(async (transaction) => {
  const shardRef = counterRef.collection('shards').doc([1]);
  const shardDoc = await transaction.get(shardRef);
  if (!shardDoc.exists) {
    transaction.set(shardRef, { count: 1 });
  } else {
    transaction.update(shardRef, { count: shardDoc.data().count + 1 });
  }
});
Drag options to blanks, or click blank then click option'
A'shard0'
B'shard1'
C'shardA'
D'shardX'
Attempts:
3 left
💡 Hint
Common Mistakes
Using shard IDs that do not match the naming convention.
Using an undefined or empty shard ID.
3fill in blank
hard

Fix the error in the code to correctly sum counts from all shards.

Firebase
const shardsSnapshot = await counterRef.collection('shards').get();
let totalCount = 0;
shardsSnapshot.forEach(doc => {
  totalCount += doc.data().[1];
});
Drag options to blanks, or click blank then click option'
Avalue
Btotal
Cnumber
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect field names like 'value' or 'total' which do not exist.
Assuming the field is named 'number' instead of 'count'.
4fill in blank
hard

Fill both blanks to create a function that initializes shards with zero counts.

Firebase
async function initializeShards(counterRef, numShards) {
  const batch = firestore.batch();
  for (let i = 0; i < numShards; i++) {
    const shardRef = counterRef.collection('shards').doc([1]);
    batch.set(shardRef, { [2]: 0 });
  }
  await batch.commit();
}
Drag options to blanks, or click blank then click option'
A`shard${i}`
Bcount
Cvalue
D`shard_${i}`
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect shard naming without template literals.
Using wrong field names like 'value' instead of 'count'.
5fill in blank
hard

Fill all three blanks to implement a function that reads and sums counts from all shards.

Firebase
async function getDistributedCount(counterRef) {
  const shardsSnapshot = await counterRef.collection([1]).get();
  let totalCount = 0;
  shardsSnapshot.forEach(doc => {
    totalCount += doc.data()[[2]];
  });
  return totalCount;
}

const count = await getDistributedCount([3]);
Drag options to blanks, or click blank then click option'
A'shards'
B'count'
CcounterRef
D'counters'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong collection names like 'counters' instead of 'shards'.
Using incorrect field names or passing wrong arguments.