Total number of records in Firebase and other aggregations

Firebase lacks API for that. The only Firebase only solution is a transaction, but it’s brittle because we have to update two things, data itself and total count, and Firebase doesn’t provide such transaction for that. We have basically only two solutions. Quick & dirty and hard & perfect.

Quick & dirty

Basically something like this based on transaction api. But evil user can increment value more than once. So this approach is only suitable for cases, where total count is not much important.

Hard & perfect

Aggregation over data shared across users must be a separated task for aws.amazon.com/lambda for example. http://thanish.me/2014-05-25-aggregation-for-firebase provides a nice example how to do it. Such service should use versioned aggregation results, so deploying a new service version will not break current running.