許多實時應用程序都有充當計數器的文檔。例如,您可能會計算帖子上的“喜歡”或特定項目的“收藏夾”。
在 Cloud Firestore 中,您每秒只能更新一次文檔,這對於一些高流量的應用程序來說可能太低了。
解決方案:分佈式計數器
要支持更頻繁的計數器更新,請創建分佈式計數器。每個計數器都是一個包含“碎片”子集合的文檔,計數器的值是碎片值的總和。
寫入吞吐量隨分片數量線性增加,因此具有 10 個分片的分佈式計數器可以處理 10 倍於傳統計數器的寫入。
網絡
// counters/${ID}
{
"num_shards": NUM_SHARDS,
"shards": [subcollection]
}
// counters/${ID}/shards/${NUM}
{
"count": 123
}
迅速
注意:此產品不適用於 watchOS 和 App Clip 目標。
// counters/${ID} struct Counter { let numShards: Int init(numShards: Int) { self.numShards = numShards } } // counters/${ID}/shards/${NUM} struct Shard { let count: Int init(count: Int) { self.count = count } }
Objective-C
注意:此產品不適用於 watchOS 和 App Clip 目標。
// counters/${ID} @interface FIRCounter : NSObject @property (nonatomic, readonly) NSInteger shardCount; @end @implementation FIRCounter - (instancetype)initWithShardCount:(NSInteger)shardCount { self = [super init]; if (self != nil) { _shardCount = shardCount; } return self; } @end // counters/${ID}/shards/${NUM} @interface FIRShard : NSObject @property (nonatomic, readonly) NSInteger count; @end @implementation FIRShard - (instancetype)initWithCount:(NSInteger)count { self = [super init]; if (self != nil) { _count = count; } return self; } @end
Java
// counters/${ID} public class Counter { int numShards; public Counter(int numShards) { this.numShards = numShards; } } // counters/${ID}/shards/${NUM} public class Shard { int count; public Shard(int count) { this.count = count; } }
Kotlin+KTX
// counters/${ID} data class Counter(var numShards: Int) // counters/${ID}/shards/${NUM} data class Shard(var count: Int)
Python
Python
節點.js
不適用,請參閱下面的計數器增量片段。
去
PHP
不適用,請參閱下面的計數器初始化片段。
C#
/// <summary> /// Shard is a document that contains the count. /// </summary> [FirestoreData] public class Shard { [FirestoreProperty(name: "count")] public int Count { get; set; } }
以下代碼初始化一個分佈式計數器:
網絡
function createCounter(ref, num_shards) { var batch = db.batch(); // Initialize the counter document batch.set(ref, { num_shards: num_shards }); // Initialize each shard with count=0 for (let i = 0; i < num_shards; i++) { const shardRef = ref.collection('shards').doc(i.toString()); batch.set(shardRef, { count: 0 }); } // Commit the write batch return batch.commit(); }
迅速
注意:此產品不適用於 watchOS 和 App Clip 目標。
func createCounter(ref: DocumentReference, numShards: Int) { ref.setData(["numShards": numShards]){ (err) in for i in 0...numShards { ref.collection("shards").document(String(i)).setData(["count": 0]) } } }
Objective-C
注意:此產品不適用於 watchOS 和 App Clip 目標。
- (void)createCounterAtReference:(FIRDocumentReference *)reference shardCount:(NSInteger)shardCount { [reference setData:@{ @"numShards": @(shardCount) } completion:^(NSError * _Nullable error) { for (NSInteger i = 0; i < shardCount; i++) { NSString *shardName = [NSString stringWithFormat:@"%ld", (long)shardCount]; [[[reference collectionWithPath:@"shards"] documentWithPath:shardName] setData:@{ @"count": @(0) }]; } }]; }
Java
public Task<Void> createCounter(final DocumentReference ref, final int numShards) { // Initialize the counter document, then initialize each shard. return ref.set(new Counter(numShards)) .continueWithTask(new Continuation<Void, Task<Void>>() { @Override public Task<Void> then(@NonNull Task<Void> task) throws Exception { if (!task.isSuccessful()) { throw task.getException(); } List<Task<Void>> tasks = new ArrayList<>(); // Initialize each shard with count=0 for (int i = 0; i < numShards; i++) { Task<Void> makeShard = ref.collection("shards") .document(String.valueOf(i)) .set(new Shard(0)); tasks.add(makeShard); } return Tasks.whenAll(tasks); } }); }
Kotlin+KTX
fun createCounter(ref: DocumentReference, numShards: Int): Task<Void> { // Initialize the counter document, then initialize each shard. return ref.set(Counter(numShards)) .continueWithTask { task -> if (!task.isSuccessful) { throw task.exception!! } val tasks = arrayListOf<Task<Void>>() // Initialize each shard with count=0 for (i in 0 until numShards) { val makeShard = ref.collection("shards") .document(i.toString()) .set(Shard(0)) tasks.add(makeShard) } Tasks.whenAll(tasks) } }
Python
Python
節點.js
不適用,請參閱下面的計數器增量片段。
去
PHP
$numShards = 10; $ref = $db->collection('samples/php/distributedCounters'); for ($i = 0; $i < $numShards; $i++) { $doc = $ref->document($i); $doc->set(['Cnt' => 0]); }
C#
/// <summary> /// Create a given number of shards as a /// subcollection of specified document. /// </summary> /// <param name="docRef">The document reference <see cref="DocumentReference"/></param> private static async Task CreateCounterAsync(DocumentReference docRef, int numOfShards) { CollectionReference colRef = docRef.Collection("shards"); var tasks = new List<Task>(); // Initialize each shard with Count=0 for (var i = 0; i < numOfShards; i++) { tasks.Add(colRef.Document(i.ToString()).SetAsync(new Shard() { Count = 0 })); } await Task.WhenAll(tasks); }
紅寶石
要增加計數器,請選擇一個隨機分片並增加計數:
網絡
function incrementCounter(db, ref, num_shards) { // Select a shard of the counter at random const shard_id = Math.floor(Math.random() * num_shards).toString(); const shard_ref = ref.collection('shards').doc(shard_id); // Update count return shard_ref.update("count", firebase.firestore.FieldValue.increment(1)); }
迅速
注意:此產品不適用於 watchOS 和 App Clip 目標。
func incrementCounter(ref: DocumentReference, numShards: Int) { // Select a shard of the counter at random let shardId = Int(arc4random_uniform(UInt32(numShards))) let shardRef = ref.collection("shards").document(String(shardId)) shardRef.updateData([ "count": FieldValue.increment(Int64(1)) ]) }
Objective-C
注意:此產品不適用於 watchOS 和 App Clip 目標。
- (void)incrementCounterAtReference:(FIRDocumentReference *)reference shardCount:(NSInteger)shardCount { // Select a shard of the counter at random NSInteger shardID = (NSInteger)arc4random_uniform((uint32_t)shardCount); NSString *shardName = [NSString stringWithFormat:@"%ld", (long)shardID]; FIRDocumentReference *shardReference = [[reference collectionWithPath:@"shards"] documentWithPath:shardName]; [shardReference updateData:@{ @"count": [FIRFieldValue fieldValueForIntegerIncrement:1] }]; }
Java
public Task<Void> incrementCounter(final DocumentReference ref, final int numShards) { int shardId = (int) Math.floor(Math.random() * numShards); DocumentReference shardRef = ref.collection("shards").document(String.valueOf(shardId)); return shardRef.update("count", FieldValue.increment(1)); }
Kotlin+KTX
fun incrementCounter(ref: DocumentReference, numShards: Int): Task<Void> { val shardId = Math.floor(Math.random() * numShards).toInt() val shardRef = ref.collection("shards").document(shardId.toString()) return shardRef.update("count", FieldValue.increment(1)) }
Python
Python
節點.js
去
PHP
$ref = $db->collection('samples/php/distributedCounters'); $numShards = 0; $docCollection = $ref->documents(); foreach ($docCollection as $doc) { $numShards++; } $shardIdx = random_int(0, $numShards - 1); $doc = $ref->document($shardIdx); $doc->update([ ['path' => 'Cnt', 'value' => FieldValue::increment(1)] ]);
C#
/// <summary> /// Increment a randomly picked shard by 1. /// </summary> /// <param name="docRef">The document reference <see cref="DocumentReference"/></param> /// <returns>The <see cref="Task"/></returns> private static async Task IncrementCounterAsync(DocumentReference docRef, int numOfShards) { int documentId; lock (s_randLock) { documentId = s_rand.Next(numOfShards); } var shardRef = docRef.Collection("shards").Document(documentId.ToString()); await shardRef.UpdateAsync("count", FieldValue.Increment(1)); }
紅寶石
要獲得總計數,請查詢所有分片並將它們的count
字段相加:
網絡
function getCount(ref) { // Sum the count of each shard in the subcollection return ref.collection('shards').get().then((snapshot) => { let total_count = 0; snapshot.forEach((doc) => { total_count += doc.data().count; }); return total_count; }); }
迅速
注意:此產品不適用於 watchOS 和 App Clip 目標。
func getCount(ref: DocumentReference) { ref.collection("shards").getDocuments() { (querySnapshot, err) in var totalCount = 0 if err != nil { // Error getting shards // ... } else { for document in querySnapshot!.documents { let count = document.data()["count"] as! Int totalCount += count } } print("Total count is \(totalCount)") } }
Objective-C
注意:此產品不適用於 watchOS 和 App Clip 目標。
- (void)getCountWithReference:(FIRDocumentReference *)reference { [[reference collectionWithPath:@"shards"] getDocumentsWithCompletion:^(FIRQuerySnapshot *snapshot, NSError *error) { NSInteger totalCount = 0; if (error != nil) { // Error getting shards // ... } else { for (FIRDocumentSnapshot *document in snapshot.documents) { NSInteger count = [document[@"count"] integerValue]; totalCount += count; } NSLog(@"Total count is %ld", (long)totalCount); } }]; }
Java
public Task<Integer> getCount(final DocumentReference ref) { // Sum the count of each shard in the subcollection return ref.collection("shards").get() .continueWith(new Continuation<QuerySnapshot, Integer>() { @Override public Integer then(@NonNull Task<QuerySnapshot> task) throws Exception { int count = 0; for (DocumentSnapshot snap : task.getResult()) { Shard shard = snap.toObject(Shard.class); count += shard.count; } return count; } }); }
Kotlin+KTX
fun getCount(ref: DocumentReference): Task<Int> { // Sum the count of each shard in the subcollection return ref.collection("shards").get() .continueWith { task -> var count = 0 for (snap in task.result!!) { val shard = snap.toObject<Shard>() count += shard.count } count } }
Python
Python
節點.js
去
PHP
$result = 0; $docCollection = $db->collection('samples/php/distributedCounters')->documents(); foreach ($docCollection as $doc) { $result += $doc->data()['Cnt']; }
C#
/// <summary> /// Get total count across all shards. /// </summary> /// <param name="docRef">The document reference <see cref="DocumentReference"/></param> /// <returns>The <see cref="int"/></returns> private static async Task<int> GetCountAsync(DocumentReference docRef) { var snapshotList = await docRef.Collection("shards").GetSnapshotAsync(); return snapshotList.Sum(shard => shard.GetValue<int>("count")); }
紅寶石
限制
上面顯示的解決方案是在 Cloud Firestore 中創建共享計數器的一種可擴展方式,但您應該注意以下限制:
- 分片數- 分片數控制分佈式計數器的性能。如果分片太少,某些事務可能需要重試才能成功,這會減慢寫入速度。如果分片太多,讀取會變得更慢且更昂貴。您可以通過將計數器總數保存在單獨的匯總文檔中來抵消讀取費用,該文檔以較慢的節奏(例如每秒一次)更新,並讓客戶從該文檔中讀取以獲取總數。權衡是客戶端將不得不等待匯總文檔更新,而不是通過在任何更新後立即讀取所有分片來計算總數。
- 成本- 讀取計數器值的成本隨著分片數量線性增加,因為必須加載整個分片子集合。