Original reseller budget queues diagram: Quote, Lock, Commit, Reconcile.

SMM Reseller Budgets: Queue Orders Before Spending

Two workers can read the same available allowance, each decide an order fits, and together commit more than the client approved. A balance check does not solve this race. You need one atomic local admission decision before either worker submits its order.

This guide builds that decision around an application-owned budget ledger. The AlwaysCompleted API reference documents balance, order creation and order-status reads. It does not document a reservation endpoint or a client-budget lock. All allowances, reservations, intent IDs and queue states below belong to your application.

Keep three balances conceptually separate

The API account balance is an observation of the account at a particular time. A client's allowance is a commercial limit you maintain. A local reservation is your application's commitment to consider a particular queued job against that allowance. They are not interchangeable.

For the DEMO, define available allowance as:

available = allowance - committed - reserved

Here committed is the amount already assigned to accepted work under the local accounting policy. reserved covers work admitted locally whose amount has not yet been moved into committed. Never subtract a charge twice by putting the same commitment in both totals. Store API-observed charges in separate order observations and reconcile them through explicit ledger transitions.

The fictional client has a USD 10.00 allowance and no prior commitments. Worker A proposes USD 7.00; worker B proposes USD 6.00. Either fits alone. Both together do not. The application must serialize the two decisions, even if the workers started at nearly the same time.

Freeze the quote used for admission

Store a quote version, currency, service type and the estimate basis beside the intent. Preserve decimal strings at input and use an exact decimal representation for calculations. Python's decimal documentation explains why converting from a string avoids an intermediate binary-float representation.

Do not apply one pricing formula to every service type. A package, a quantity-based order and a subscription can have different parameters and continuing commitments. The downloadable SQL is a fixed-amount admission demonstration. Adapting it to recurring or variable work requires a separate limit and renewal policy.

The demonstration uses six decimal places and rejects an estimate that needs more precision. It also excludes special nonfinite numeric values from its stored money fields. PostgreSQL can round values to a declared scale and supports special numeric values, so choose and validate your own storage policy deliberately; see its numeric type reference.

If a quote has expired or the catalog conditions changed before submission, stop and re-evaluate under the agreed policy. Increasing a reservation is another atomic budget decision. A local approval does not guarantee that the API will accept the order or that an observed balance will remain unchanged until submission.

Lock, decide, record, then commit

The example uses a PostgreSQL transaction and a row lock on the client's budget row. PostgreSQL's locking documentation explains how competing row-lock operations wait and how locks are released when the transaction ends.

Inside one transaction, the demonstration function:

  1. Locks the client's budget row.
  2. Checks currency and whether this local intent already exists.
  3. Compares the requested estimate with the current available allowance.
  4. Records the admitted intent and increases the reserved amount together.
  5. Returns the admission result to the caller, which commits the transaction.

Only after commit should a dispatcher consider the API request. Do not keep the database transaction open while waiting for the network. A slow remote response would otherwise hold the budget row and block unrelated decisions for the same client.

The local unique intent record is useful when your application retries its own admission transaction. It does not make an API add request idempotent. If an existing intent is presented with different quote details, the demonstration rejects the mismatch rather than quietly repurposing the old reservation.

What the two-worker example proves

Without atomic admission, both workers could read USD 10.00 and decide to proceed. With the lock, worker B waits for worker A's transaction. Once A commits its USD 7.00 reservation, B evaluates against USD 3.00 and receives a local budget rejection. No API request is involved in this exercise.

If A rolls back instead, its reservation and intent record disappear together. B then evaluates the unchanged allowance. A local rollback is evidence about that local transaction only. It says nothing about a network request performed elsewhere, which is another reason to keep the API call after commit.

This example was checked locally with PostgreSQL 16.11 and fictional rows. The checks covered competing workers, commit and rollback outcomes, duplicate local intent handling, invalid numeric inputs and a deadlock followed by a local transaction retry. No API calls or paid orders were used; the result concerns the local admission example.

The downloadable exercise contains a schema, admission function and two session scripts. Run it only in a disposable database. It uses explicitly named DEMO tables, not production account data. The examples also show how to observe a waiting session and check the resulting reserved amount.

Preserve uncertain submissions

After admission, a dispatcher should claim the intent once, record that submission has started, and preserve the result. A returned order ID provides the link for later status reads. An ambiguous timeout requires an unknown_outcome state and reconciliation; it must not automatically release the reservation and resubmit.

The duplicate-order timeout guide covers that boundary. This article's budget function deliberately stops before network dispatch. A production queue also needs a claim policy, crash handling and reconciliation ownership. Do not interpret a crashed worker as proof that its submitted order never reached the API.

When an accepted order has a confirmed charge, update the local commitment under a defined policy and retain the observation. If the charge differs from the estimate, record the variance and review its effect on remaining allowance. A cancellation request or nonzero remains value does not, by itself, authorize release of a financial commitment.

Bound retries to the transaction that failed

A deadlock or serialization failure can require retrying a local database transaction. Keep a consistent lock order when a larger implementation touches several budgets or accounts. Retry only the failed local transaction with a bounded policy and a stable local intent.

Never wrap “reserve, commit, call add” in a generic retry block that starts over after any exception. Once the transaction committed and the network call began, a later failure belongs to a different recovery path. The downloaded timeline makes that split explicit.

Use the SQL admission example, two-session exercise, and blank budget decision record to review the design with your developer. The exercise is an educational local test, not a production-ready billing system or an API sandbox.

Agree the business rule before automation

Decide who can raise an allowance, how long a reservation remains valid, who resolves unknown submissions and which evidence closes a commitment. Put those decisions beside the queue so a worker cannot invent its own exception under pressure.

Review the current developer guide, API reference and reseller workflow. Confirm the actual service parameters and cost basis before admitting work. Contact technical support for an order-specific uncertainty, while keeping customer allowance decisions within your own authorized process.

By AlwaysCompleted · Published