Menu

An append-only credit ledger in Laravel: AI metering that can't double-spend
📰

An append-only credit ledger in Laravel: AI metering that can't double-spend

DEV Community: laravel·Keel·3 days ago
#sWeKMGpE
#devtolaravel#devto#dev#table#balance
Long article (2391 words) — no AI summary yet.
Reading 0:00
15s threshold

Every AI feature eventually needs an answer to the question "how much has this user spent". The first version everyone writes is a credits integer on the users table, and the first version everyone writes is wrong in the same two ways: two concurrent requests can read the same balance and both spend it, and when the number is wrong you have no idea why, because the number is all you kept.

Here is the version the kit this series is drawn from ships instead: an append-only ledger where the balance is derived, never stored, every change is a row with a reason, and the exactly-once guarantees live in the database schema rather than in hopeful application code.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.