3 ms·
I've written billing/usage systems before, something your missing is the audit trail, you will get a query on a bill by a customer (for example, why did my bill
by plasma 4y ago
I've written billing/usage systems before, something your missing is the audit trail, you will get a query on a bill by a customer (for example, why did my bill increase this month so much, or spike on Wednesday?) and you need to be able to dig into the raw data (eg, your raw usage logs that contain high detail of start/stop events or usage information for each service call) to provide comfort to yourself and your customer about the billing query and validate there was no billing error.
Since you will want to plan for that, one thing you can do is treat the end of month billing calculation (or ongoing usage per day) as a calculation based off your raw usage logs.
So your usage of the product (service usage start/stop times) are log events (eg, pushed to S3 if its a huge system or start with just another database table like billing_usage_event) that describes each service start/stop and calculated rate etc.
Then your bill is the aggregation of the raw events for the day/month, allowing you to both do an audit if there is a billing query (find out why the bill spike occurred by looking at billing_usage_event) and also provide peace of mind to you and your customer the billing is accurate.
("Accounting for Developers" was posted recently on HN, it was a great read - https://news.ycombinator.com/item?id=32495724 https://news.ycombinator.com/item?id=32495724)
- derefr 4y agoAlternately, you could model it the other way ‘round: mutable tables, like OP has, but with change-data-capture (Debezium et al) attached to them, exporting to the logs-at-rest as a secondary format. Probably easier to pick up / add on, if you’re not already modelling anything else with a CQRS/ES approach. (Though just doing CQRS/ES from the start is always going to get you much cleaner code with many more guarantees that can be made about it.)