A wallet limit is ineffective if two requests can independently observe the same remaining capacity and both proceed. The problem appears with withdrawals, purchases and any operation that reserves money or counts toward a configured usage limit. Enforcement must consider committed activity and relevant in-flight reservations together. Define the invariant first, then choose a concurrency mechanism that preserves it through retries, expiry and delayed external outcomes.

Define the limit and its accounting window

State what the limit measures: available funds, cumulative outgoing value, transaction count or another explicitly defined quantity. Specify the currency, window and event that consumes capacity. A daily limit based on requests has different behavior from one based on completed transfers, so the policy cannot remain implicit in a database query.

Decide how active reservations contribute. If pending operations may still complete, ignoring them can allow the total to exceed the intended boundary. Also define whether failed or cancelled operations restore capacity and when that restoration is justified. Keep policy configuration versioned so an operator can explain the rule applied to a particular request.

Make checking and reserving one atomic decision

Perform the capacity check and reservation creation within a transaction or equivalent mechanism that protects the shared invariant. A separate read followed by an insert is vulnerable to concurrent acceptance. Use the database’s documented locking or isolation behavior and enforce stable operation identities to prevent duplicate reservation creation.

PostgreSQL documents that serializable transactions may abort when concurrent activity would not be equivalent to a serial execution. If that mechanism is used, retry the complete transaction according to a bounded policy. Do not assume selecting a stronger isolation level eliminates the need to handle conflicts, duplicate requests or application-level ownership.

Keep reservation changes tied to authoritative outcomes

Model reservation creation, adjustment, consumption and release as explicit transitions. Record the operation that owns each reservation and prevent unrelated callbacks from releasing it. A partial completion may consume only part of the capacity and require a deliberate treatment of the remainder.

Be careful with time-based expiry. A local timeout does not prove an external payment failed. Releasing capacity while the provider can still complete the operation can violate the original limit. Use a reconciliation or cancellation-confirmation process for uncertain outcomes, with a visible state that explains why capacity remains reserved.

Test competing requests and policy changes

Run controlled concurrent scenarios where requests together exceed the limit but individually appear valid. Confirm that only the permitted set succeeds and that rejected requests receive a clear explanation. Include retries, duplicate events and a process failure after reservation but before provider dispatch.

Test window rollover and changes to configured limits while reservations remain active. Keep audit information showing the available capacity calculation and the accepted policy version without exposing unnecessary account data. The result should be a system that can explain why an operation was accepted, delayed or rejected and can recover unfinished work while preserving the same invariant it enforced initially.

TAKE IT FORWARD

The key decisions.

  • Define the measured quantity, time window and treatment of pending reservations.
  • Check capacity and create reservations atomically with stable operation identities.
  • Release capacity only on justified outcomes and test genuinely concurrent requests.

Sources & further reading

Primary references for the concepts discussed. Check the applicable documentation when planning implementation.

  1. PostgreSQL: transaction characteristics and isolation