Ask HN: How to design database schema for usage based billing?
16 by punkpeye | 8 comments on Hacker News.
The service I am building is usage-based, i.e. * I am charging per second the service was used, e.g. USD 0.01/second * I would like to give credits upon sign-up. * There is no monthly minimum. At the moment, I am thinking that I can achieve this with just a few tables: * `billing_account (id, billing_rate_id, running_balance)` * `billing_rate (id, rate)` * `account_credit (id, billing_account_id, timestamp, amount, function_execution_id)` * `account_debit (id, billing_account_id, timestamp, amount, source [platform_credit or stripe])` When a user creates an account, I would create an entry in `billing_account` and associate whatever the current `billing_rate`. I would also create an `account_debit` entry with `source=platform_credit` and update `billing_account` `running_balance` value to reflect their balance after the `platform_credit`. Then whenever they run a function that costs them, I create `account_credit` account with an entry equal to the amount they spent and update `billing_account` `running_balance` value. When they top-up their account, I would just add entry to `account_debit` and update the balance again. This appears to cover all my use cases, but I wanted to check with anyone who's designed such database schemas before.
Post Top Ad
Responsive Ads Here
Subscribe to:
Post Comments (Atom)
Post Bottom Ad
Responsive Ads Here
Author Details
Templatesyard is a blogger resources site is a provider of high quality blogger template with premium looking layout and robust design. The main mission of templatesyard is to provide the best quality blogger templates which are professionally designed and perfectlly seo optimized to deliver best result for your blog.
No comments:
Post a Comment