News for hackers. Stuff that matters.
Re:Highly opinionated SQL schema design to put you on the fa... by McMini on Tuesday April 25, 2023 @05:38PM (#35707084)
Could you provide an example of how you would implement this approach to store Credit/Debit events for an account? Additionally, how would you handle a scenario where there are 30,000 events on the account, and you need to calculate the balance to prevent overdraft?
Re:Highly opinionated SQL schema design to put you on the fa... by hot_gril on Tuesday April 25, 2023 @06:03PM (#35707330)
This is one of those special performance-related cases where you want a denormalized table (point #6) you update in-place as a way to lock on the balance, just cause Postgres/MySQL `serializable` mode is way too slow to rely on. You still keep baseline insert-only credits/debits table(s) that you insert into in the same xact.

You can also do this without making such an exception. I used to keep a separate "pending" table that I'd insert into, commit, then check the balance with the pending row included before moving it to non-pending. So two transactions. That worked, problem is it was annoying. Though it was a good solution for debits/credits that involved an async external step that could fail or time out; simply ignore the pending rows that are too old and never got resolved. 30K rows is still small enough to query quickly.

Re:Highly opinionated SQL schema design to put you on the fa... by McMini on Wednesday April 26, 2023 @04:47AM (#35711005)
What are the key trade-offs in your approach vs doing event sourcing?
Re:Highly opinionated SQL schema design to put you on the fa... by hot_gril on Wednesday April 26, 2023 @12:32PM (#35716350)
Tbh I had to just look up event sourcing. Seems like my approach includes a form of event sourcing, though I'm not always storing the ordering of events, only in cases where it's needed. And that concept isn't specific to relational DBs.