How We Reset Monthly Quotas Without Race Conditions or Cron Drift
This article was originally published on Jo4 Blog . If you're billing monthly via Stripe or RevenueCat, it's tempting to reset usage quotas inside your webhook handler. The customer pays → invoice fires → you zero out their urlCount . Clean. One source of truth. It works for some customers. Two customer types it doesn't work for, and they're both yours: Yearly subscribers. Their payment event…
This article explains how to reset monthly usage quotas for customers billed on a monthly or yearly subscription without causing race conditions or cron drift. The solution involves using Spring Scheduler and Postgres advisory locks. Two key points are:
1. The job runs once per calendar month, regardless of the number of app instances, using a cron schedule that staggered 5 minutes past midnight UTC on the first of the month.
2. An instance-level advisory lock is used, keyed on an integer, which auto-releases at transaction end. This prevents multiple instances from running the reset simultaneously.
The reset is performed by executing a single SQL UPDATE across the whole table inside a transaction, which is more efficient than fetching each user and saving them individually. Additionally, an admin override is provided for emergency situations.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — it may contain errors, so check the original before relying on it.