Billing code is the most dangerous place to have subtle bugs. It rarely crashes — it just silently does the wrong thing. Here are two we found and fixed in ClipCrafter , an AI video clip extraction tool.
Bug 1: The usage counter that lost data under load
We track how many seconds of video each user processes per day to enforce plan limits. The original increment looked like this:
const { data } = await db . from ( " users " ) . select ( " daily_usage_seconds " ) . eq ( " clerk_id " , clerkUserId ) . single ();
const next = ( data . daily_usage_seconds ?? 0 ) + seconds ;
await db . from ( " users " ) . update ({ daily_usage_seconds : next }) . eq ( " clerk_id " , clerkUserId );
Enter fullscreen mode
Exit fullscreen mode
This is a textbook read-modify-write race.…