Skip to main content

Decrement-ZrCounter

Atomically decreases a distributed counter by a positive amount and returns its new value. Creates the counter if it does not exist.

Syntax

Decrement-ZrCounter [-Key] <string> [[-Delta] <long>] [-ConnectionString <string>] [-StoreName <string>] [-Endpoints <string[]>] [-TimeoutSec <int>]

Description

Decrement-ZrCounter subtracts -Delta from the atomic counter stored under -Key and returns the resulting value. Internally it applies a negative delta through the store's counter API (Counters.AddAsync), so the operation is atomic on the owning node and consistent under concurrent access. If the counter does not yet exist it is created from the negative applied delta.

-Delta defaults to 1 and is specified as a positive value (1 through Int64.MaxValue); the command negates it before applying, and the emitted Delta field reflects the negative amount actually applied. To increase a counter use Increment-ZrCounter.md. The command runs against the active Connect-ZrStore session, or against a one-shot connection when -StoreName and -Endpoints are supplied.

Parameters

ParameterTypeRequiredDescription
-KeyStringYesKey identifying the counter to decrement. Positional (position 0). Must not be null or empty.
-DeltaInt64NoPositive amount to subtract from the counter. Positional (position 1). Range 1 to Int64.MaxValue. Default 1.
-ConnectionStringStringNoConnect via a Zaris connection string, e.g. zaris://host:7861/store; an alternative to -StoreName + -Endpoints for a one-shot connection. Ignored when a Connect-ZrStore session is active.
-StoreNameStringNoStore to target when using an independent (one-shot) connection instead of the active session. Requires -Endpoints.
-EndpointsString[]NoOne or more node endpoints (host:port) for an independent connection. Ignored when a Connect-ZrStore session is active.
-TimeoutSecInt32NoConnection timeout in seconds (1–120) for an independent connection. Default 5.

Examples

Decrease a counter by the default of 1:

Decrement-ZrCounter -Key "inventory:widgets"

Decrease a counter by 5:

Decrement-ZrCounter -Key "inventory:widgets" -Delta 5