Using a single Firestore document to store all my PRO customer ids.
All servers can proactively listen to that one low traffic doc.
=> realtime cache invalidation
=> synchronous billing checks in hotpath
=> scales to 10k customers
=> scales to $500k revenue if $50 subs
My logic is that when the 1MB doc size limit runs out, I will be making 500k a month and will easily be able to fix the scaling limit. But for now I am left with a stupidly simple billing code.
let pros = {};
// Subscribe to single doc firebase list of PRO accounts.
export const setBillingFirebase = (firebase) => new Promise(resolve => {
const firestore = firebase.firestore();
firestore.doc(`/services/billing/config/subs`).onSnapshot(snap => {
pros = Object.fromEntries(
Object.keys(snap.data().pro).map(k => [k, 'pro'])
);
resolve();
});
});
// Check will check namespace if it is PRO
export const isPro = (namespace) => !!pros[namespace]
It's also much cheaper, costing a Firestore read each time the doc is changed, vs. costing a read every billing check. Firestore listens only cost money when they change, whereas polling a DB costs every check!