Skip to main content

Transaction Priority Admission

When a node receives more transactions than it can run well, all of them slow down together. A nightly backfill or analytics sweep that opens thousands of transactions can then delay the checkout or login transactions that users are waiting on.

Transaction priority admission lets an operator cap how many transactions a node executes at once, and lets Kahuna decide which waiting transaction starts next. It works like a connection pool limit with a priority queue in front of it: each transaction carries a priority, and when the node is at its limit, high-priority work starts before background work.

Use it when the same cluster serves both latency-critical requests and bulk jobs, and bulk work slows down user-facing traffic. If your nodes aren't overloaded, you don't need it. It is disabled by default, and it doesn't change anything until you set a limit. To also make bulk jobs give way to foreground writers on write conflicts, see Yielding Transactions.

MaxConcurrentTransactions and MaxConcurrentSessions both default to 0, which means that all transactions start immediately. In that pass-through mode, Kahuna records priority for metrics only. Priority doesn't queue or reorder transactions, and it doesn't throttle them.

When an operator enables a concurrency ceiling, transactions that cannot start immediately wait in a per-node priority queue. Kahuna starts the highest-priority eligible waiter first.

With a ceiling set, each transaction passes through this gate before it starts. Reserved slots decide whether a slot is free for a given priority.

Priority affects only admission.

  • It governs when a transaction starts.
  • It does not preempt a transaction that is already running.
  • It doesn't change the transaction protocol, for example MVCC or two-phase commit.
  • It applies to each node separately. Each node orders only the transactions that it receives.

Priority Levels​

PriorityValueUse for
Background0Bulk or deferrable work such as analytics sweeps.
Low1Work below ordinary traffic that is latency-relevant.
Normal2Default for callers that do not specify a priority.
High3Latency-critical application transactions.
Critical4Work that must not be deferred behind anything else.

Do not mark ordinary traffic as Critical. If everything is critical, priority stops carrying useful information and the reserve cannot protect genuinely urgent work.

Unknown priority values are normalized to Normal. They aren't clamped upward. That prevents untrusted or newer clients from accidentally claiming critical priority.

What Is Gated​

The admission gate applies to transactions.

  • explicit BEGIN ... COMMIT ... END scripts
  • multi-statement scripts that run as auto-commit transactions
  • interactive transaction sessions

Single standalone script commands are not gated. A script that contains only one command, such as SET or GET, executes directly against the store. Priority can be supplied for those calls, but it is ignored because no transaction is opened.

Setting Priority​

Interactive Sessions​

await using KahunaTransactionSession session =
await client.StartTransactionSession(new KahunaTransactionOptions
{
Priority = TransactionPriority.High,
Timeout = 30_000,
AdmissionWaitMs = 2_000
});
await using session = await client.beginTransaction({
priority: "high",
timeout: 30_000,
admissionWaitMs: 2000
});

Script Execution​

KahunaKeyValueTransactionResult result =
await client.ExecuteKeyValueTransactionScript(
script,
hash: null,
parameters: parameters,
priority: TransactionPriority.Background
);
const result = await client.executeScript(script, {
priority: "background",
parameters
});

Compiled scripts expose the same control.

KahunaTransactionScript compiled = client.LoadTransactionScript(script);

KahunaKeyValueTransactionResult result =
await compiled.Run(TransactionPriority.High, parameters);
const compiled = client.loadScript(script);

const result = await compiled.run({
priority: "high",
parameters
});

Inline Script Option​

A script can set its priority. Inline priority overrides the priority carried by the transport.

begin (priority="high", locking="optimistic")
let row = get `orders/42`
set `orders/42` row
commit
end

Inline priority uses the level names in lowercase: background, low, normal, high, critical.

Scripts can also set admissionWait, in milliseconds. This is how long the caller is willing to wait for a slot before the transaction starts. It is separate from timeout, which bounds how long the transaction may run after it has been admitted.

begin (priority="high", admissionWait=2000, timeout=10000)
let row = get `orders/42`
set `orders/42` row
commit
end

Capacity Model​

Kahuna has two independent admission gates.

GateCeilingSlot held for
ScriptMaxConcurrentTransactionsThe duration of the script transaction execution.
SessionMaxConcurrentSessionsAs long as the interactive session remains open.

They are separate because scripts and sessions hold capacity for very different lengths of time. A script is bounded by its execution. An interactive session is client-paced and can sit idle while it remains open.

Size MaxConcurrentSessions more generously than MaxConcurrentTransactions. It limits all open sessions, including idle sessions.

Reserved Slots​

TransactionPriorityReservedSlots protects capacity for High and Critical transactions.

Each gate follows these rules.

total in flight <= MaxConcurrent...
ordinary in flight <= MaxConcurrent... - TransactionPriorityReservedSlots

The ordinary priorities are Background, Low, Normal. They cannot occupy reserved slots. High and Critical can use both ordinary capacity and the reserve.

This prevents bulk traffic from filling all slots on a saturated node.

Aging​

Strict priority can starve low-priority work. Kahuna avoids that with aging.

After each wait of TransactionPriorityAgingThreshold milliseconds, a queued transaction gains one effective priority level for dispatch ordering. Aging is capped at High. Critical doesn't age, because it is already at the top.

Aging changes the position in line. It doesn't change the eligibility for reserved capacity. An aged Background transaction can overtake newer ordinary work, but it can't use a reserved slot.

Set TransactionPriorityAgingThreshold to 0 to disable aging.

Queue Backpressure​

TransactionPriorityMaxQueued bounds how many callers may wait per gate. When the queue is full, admission fails immediately with AdmissionRefused.

No transaction has started in that case. This means that a retry is safe. Clients should back off and retry. This result isn't a conflict.

Admission wait is budgeted separately from transaction lifetime.

  • AdmissionWaitMs on KahunaTransactionOptions controls how long an interactive session waits to start.
  • admissionWait in a script begin (...) block controls how long that script waits to start.
  • timeout controls the admitted transaction lifetime or script execution time.

An explicit admissionWait=0 means "start only if a slot is free right now". Omitting the option uses the server default, and negative values are refused as script errors.

If the caller's admission budget expires before a slot opens, Kahuna returns AdmissionRefused.

AdmissionRefused and MustRetry are both retryable, but they mean different things. Use backoff for AdmissionRefused because the node is shedding load. Retry MustRetry promptly, because it usually means a transient condition, for example a leadership change.

Configuration​

SettingCLI flagDefaultMeaning
MaxConcurrentTransactions--max-concurrent-transactions0Script transactions running at once. 0 disables the script gate.
MaxConcurrentSessions--max-concurrent-sessions0Interactive sessions open at once. 0 disables the session gate.
TransactionPriorityReservedSlots--transaction-priority-reserved-slots0Slots only High and Critical transactions may occupy.
TransactionPriorityAgingThreshold--transaction-priority-aging-threshold1000Milliseconds of waiting per effective priority promotion. 0 disables aging.
TransactionPriorityMaxQueued--transaction-priority-max-queued4096Waiting callers per gate before new arrivals receive AdmissionRefused. 0 makes the wait queue unbounded.
DefaultAdmissionWaitMs--default-admission-wait5000Admission wait used when the caller does not specify one.
MaxAdmissionWaitMs--max-admission-wait30000Maximum admission wait allowed by the server. Caller-supplied waits are clamped to this value.

Start with pass-through mode and observe the metrics before setting a ceiling. Set ceilings near healthy observed concurrency, then add a small reserve only if high-priority work queues behind bulk work.

Metrics​

Transaction admission metrics are published on the Kahuna meter. Each instrument is tagged by gate (script or session) and priority.

InstrumentMeaning
kahuna.tx_admission.in_flightTransactions currently holding an admission slot.
kahuna.tx_admission.queuedTransactions currently waiting for a slot. Non-zero means the gate is active.
kahuna.tx_admission.max_queue_depthHigh-water mark of simultaneous waiters.
kahuna.tx_admission.admittedTransactions admitted since startup.
kahuna.tx_admission.aged_promotionsWaiters promoted by aging at least once.
kahuna.tx_admission.abandoned_while_waitingWaiters whose admission budget expired before they started.
kahuna.tx_admission.rejected_queue_fullRequests refused because the wait queue was full.

Watch queued first. If it remains zero, the gate is transparent. If queued is sustained for High or Critical, the ceiling may be too low or a reserve may be needed. If rejected_queue_full rises, the node is shedding transaction starts and callers should be backing off.