1
0 Comments

Super simple billing check with Firestore

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!

posted to Icon for group Building in Public
Building in Public
on January 6, 2022
Trending on Indie Hackers
I'm a lawyer who launched an AI contract tool on Product Hunt today — here's what building it as a non-technical founder actually felt like User Avatar 150 comments A simple way to keep AI automations from making bad decisions User Avatar 54 comments “This contract looked normal - but could cost millions” User Avatar 54 comments Never hire an SEO Agency for your Saas Startup User Avatar 42 comments 👉 The most expensive contract mistakes don’t feel risky User Avatar 41 comments The indie maker's dilemma: 2 months in, 700 downloads, and I'm stuck User Avatar 40 comments