Taken from this artice on Sharding Counters, the following function demonstrates how a random shard is selected before it is incremented. This happens within a transaction.
def increment():
"""Increment the value for a given sharded counter."""
def txn():
index = random.randint(0, NUM_SHARDS - 1)
shard_name = "shard" + str(index)
counter = SimpleCounterShard.get_by_key_name(shard_name)
if counter is None:
counter = SimpleCounterShard(key_name=shard_name)
counter.count += 1
counter.put()
db.run_in_transaction(txn)
Can only one transaction take place at a time and would that not prevent the different(random) sharded counters to be updated simultaneously? If so, what is the purpose of the sharding counters if only one sharded counter can be updated at a time?
Thanks!