Urgent.News

What's breaking now, across thousands of outlets.

Tech

PostgreSQL Constraint Triggers for AI Feature Usage Quotas: Enforcing Hard Limits at the Database Layer

PostgreSQL Constraint Triggers for AI Feature Usage Quotas: Enforcing Hard Limits at the Database Layer I've watched quota systems fail at scale more times than I'd like to admit. You implement soft checks in FastAPI—verify the tenant hasn't exceeded their monthly Claude API calls, then call the API. Works fine locally. Ships to production. Then during a traffic spike, three concurrent requests…

PostgreSQL database layer enforcement of quota systems can prevent application-layer quotas from failing during high traffic periods. Application-layer quotas implemented in FastAPI can lead to race conditions where multiple requests may all read the same count value, decide they are under the limit, and then all proceed to make API calls.

This results in exceeding the quota. Wrapping the database transaction with row-level locking addresses this issue but holds a database lock while making the external API call, leading to connection pool exhaustion. A more efficient approach is to enforce quotas at the database layer using triggers.

In this schema, tenant_feature_limits stores the monthly limit for each tenant and feature. Tenant_usage_log keeps track of each API call attempt. A materialized view, tenant_monthly_usage, calculates the current month's usage per tenant and feature. An index on this view allows fast lookups. The trigger function check_usage_quota is triggered after each new record is inserted into tenant_usage_log.

It retrieves the current month's usage and limit for the new record's tenant and feature. If the limit is reached or exceeded, the trigger rejects the insert, preventing the API call. This ensures quotas are enforced atomically and without the need for holding database locks during external API calls.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

The Weekly Operating Rhythm That Keeps a Solo Studio Shipping

Monday is planning only, no building, protects the week's focus Tuesday through Thursday ship one thing each, nothing parallel Friday handles publishing, scheduling, and the 30-minute metrics review I…

  • Monday is for planning, not building; limited to three tasks at most
  • Middle of the week focuses on building, one thing per day to prevent context switching
  • Friday is for publishing and reviewing, scheduling social media content in bulk

TOPCon Workshops Require Both Heating and Cooling: Why Conventional HVAC Designs Cannot Simply Be Replicated

In the mass production of n-type TOPCon (tunnel oxide passivated contact) solar cells, photovoltaic manufacturing has entered an era of extreme competition defined by micrometer-scale structural…

  • TOPCon production demands unique HVAC approach, separating environmental control from heat removal
  • Process cooling essential for specialized equipment like tube diffusion furnaces and PECVD systems

Debounce vs Throttle: I Counted Every Call

Debounce waits for a pause; 25 fast keystrokes collapsed into exactly 1 call at 300 ms Throttle keeps a steady beat; 60 scroll events at 60 Hz became 11 calls at 100 ms Debounce alone let a slow…

  • Debounce waits for event pause before calling function
  • Throttle maintains steady pace regardless of event frequency
  • Throttle includes trailing call to ensure final event not dropped

More from Monday 12 October →