You want to keep track of the count for a group of entities. You need transactions for that. But this solution doesn’t scale up as it won’t handle many parallel writes. So what do you do? This article here goes to great lengths to give you a solid background and then proceeds to describing some solutions.
Update: here’s another technique that drops sharding in favour of offline processing