Skip to main content

Key/Value API Reference

This page is the reference for the basic key/value operations. Each section shows the .NET signature and examples for the Kahuna CLI, the .NET client, and the TypeScript client. For an overview of the store, see Distributed Key/Value Store.

Use the .NET or TypeScript client from application code. Use the Kahuna CLI to inspect or change keys by hand. Use Kahuna Script when several operations must run together on the server, as one transaction. The CLI examples are Kahuna Script commands, and each section links to the command page with the full syntax and modifiers.

Each operation takes a durability level, persistent or ephemeral; see Durability Levels.

Payload Values​

Values are byte payloads. A null payload means the key exists without a value, while an empty byte array means the key exists with a zero-byte value. Kahuna preserves that difference over both REST and gRPC, including compare-value operations and transaction results.

In REST JSON, null stays null and any byte array is encoded as base64. That means a zero-byte payload is the empty base64 string "".

Set​

Creates or overwrites a key. Optional flags make the write depend on whether the key exists, its current value, or its current revision. Script syntax: set (persistent) and eset (ephemeral).

Task<KahunaKeyValue> SetKeyValue(
string key,
byte[]? value,
int expiryTime = 0,
KeyValueFlags flags = KeyValueFlags.Set,
KeyValueDurability durability = KeyValueDurability.Persistent
);
  • key: A unique identifier for the key/value pair.
  • value: The data object associated with the key. null is distinct from an empty byte array.
  • expiryTime: The expiration time of the key in milliseconds.
  • flags:
    • If KeyValueFlags.SetIfExists is specified, the value is set only if the key already exists.
    • If KeyValueFlags.SetIfNotExists is specified, the value is set only if the key does not exist.
  • durability: Defines whether the key durability is Ephemeral or Persistent.

Returns:

  • Success: true if the key's value was modified.
  • Revision: The key's current revision after the operation.

Compare Value and Set​

Sets or overwrites key/value pairs but only if the current value matches a specified comparison value. For when to use compare-and-set (CAS) and worked examples, see Compare-and-Set (CAS).

Task<KahunaKeyValue> TryCompareValueAndSetKeyValue(
string key,
byte[] value,
byte[] compareValue,
int expiryTime = 0,
KeyValueDurability durability = KeyValueDurability.Persistent
);
  • key: A unique identifier for the key/value pair.
  • value: The data object associated with the key.
  • compareValue: The value is changed only if the current value matches the provided one.
  • expiryTime: The expiration time of the key in milliseconds.
  • durability: Defines whether the key durability is Ephemeral or Persistent.

Returns:

  • Success: true if the comparison matched and the key was modified.
  • Revision: The key's current revision after the operation.

Compare Revision and Set​

Sets or overwrites key/value pairs but only if the current revision matches a specified comparison revision. See Compare-and-Set (CAS) and Revisions.

Task<KahunaKeyValue> TryCompareRevisionAndSetKeyValue(
string key,
byte[]? value,
long compareRevision,
int expiryTime = 0,
KeyValueDurability durability = KeyValueDurability.Persistent
);
  • key: A unique identifier for the key/value pair.
  • value: The data object associated with the key.
  • compareRevision: The value is changed only if the current revision matches the provided one.
  • expiryTime: The expiration time of the key in milliseconds.
  • durability: Defines whether the key durability is Ephemeral or Persistent.

Returns:

  • Success: true if the revision matched and the key was modified.
  • Revision: The key's current revision after the operation.

Get​

Retrieves the value of a key along with its revision. If the key does not exist, the special value nil is returned. Script syntax: get and eget.

Task<KahunaKeyValue> GetKeyValue(
string key,
KeyValueDurability durability = KeyValueDurability.Persistent,
long snapshotMs = 0,
CancellationToken cancellationToken = default
);
  • key: The key to be queried.
  • durability: Defines whether the key durability is Ephemeral or Persistent.
  • snapshotMs: Optional Unix-epoch-ms snapshot timestamp. 0 means latest.

Returns:

  • KahunaKeyValue: Result object with success state, value, revision, and LastModified.

Get Revision​

Retrieves the value of a key at the specific revision. If the key/revision combination does not exist in the key/value store, the special value nil is returned.

Task<KahunaKeyValue> GetKeyValueRevision(
string key,
long revision,
KeyValueDurability durability = KeyValueDurability.Persistent
);
  • key: The key to be queried.
  • revision: The revision to be returned.
  • durability: Defines whether the key durability is Ephemeral or Persistent.

Returns:

  • Success: true if the key exists at the requested revision.
  • Value: The value associated with the key.
  • Revision: The queried revision.

Use AT <revision> or a client revision-read method when you know the exact revision number. For the value visible at a point in time, use AS OF <timestamp> in Kahuna Script or snapshotMs in the clients; see AT Revision vs AS OF Timestamp.


Get By Bucket​

Retrieves key/value pairs that share the same bucket. The operation is consistent when the prefix identifies a single-partition bucket, because all keys in that bucket are routed to the same partition. Script syntax: get by bucket and eget by bucket.

Task<List<KahunaKeyValue>> GetByBucket(
string prefixKey,
KeyValueDurability durability,
long snapshotMs = 0,
CancellationToken cancellationToken = default
);
  • prefixKey: The bucket prefix to query.
  • durability: Defines whether key durability is Ephemeral or Persistent.
  • snapshotMs: Optional Unix-epoch-ms snapshot timestamp. 0 means latest.

Returns: List<KahunaKeyValue>, one item per key found:

  • Key: The key found.
  • Value: The value associated with the key.
  • Revision: The current revision of the key.
  • LastModified: The Unix timestamp in milliseconds of the key's last modification.

Use get by bucket for grouped prefixes that are intentionally single-partition. For large ordered key spaces that may split over time, see Key-Range Sharding.


Get By Range​

Retrieves key/value pairs inside an ordered interval under one prefix. This is the right abstraction for ordered key spaces and bounded range reads.

Task<List<KahunaKeyValue>> GetByRange(
string prefix,
string? startKey,
bool startInclusive,
string? endKey,
bool endInclusive,
int limit = 100,
KeyValueDurability durability = KeyValueDurability.Persistent,
long snapshotMs = 0,
CancellationToken cancellationToken = default
);
  • prefix: Logical key space prefix being scanned.
  • startKey / endKey: Optional ordered bounds.
  • startInclusive / endInclusive: Whether each bound is inclusive.
  • limit: Maximum number of items returned in this page.
  • durability: Defines whether key durability is Ephemeral or Persistent.
  • snapshotMs: Optional Unix-epoch-ms snapshot timestamp. 0 means latest.

Returns:

  • List<KahunaKeyValue>: Ordered keys in the requested interval.

The top-level clients also expose streaming range scans when you want paged traversal over the latest state or over one stable historical snapshot. In TypeScript, use scanByRange(...) as an async iterable.

Range-backed reads are internally paged. If a page repeatedly hits retryable state because a range is moving, leadership is changing, restore is in progress, or an undecided transaction intent blocks visibility, Kahuna returns a retryable error instead of scanning forever. Retry the range read after a short backoff.

In interactive transactions, GetByRange(...) is especially useful for ordered key spaces. Under pessimistic locking it can also protect the requested interval with a range lock, preventing phantom inserts and conflicting writes inside that range until the transaction completes.


Scan By Prefix​

Scans every node in the cluster for keys that start with the prefix. Each node returns its keys as they are at the moment it is visited, so the result can contain stale data. This operation is slow because it visits every node and every internal worker. Script syntax: scan by prefix and escan by prefix.

Task<List<KahunaKeyValue>> ScanAllByPrefix(
string prefixKey,
KeyValueDurability durability,
long snapshotMs = 0,
CancellationToken cancellationToken = default
);
  • prefixKey: The prefix to scan for.
  • durability: Defines whether key durability is Ephemeral or Persistent.
  • snapshotMs: Optional Unix-epoch-ms snapshot timestamp. 0 means latest.

Returns: List<KahunaKeyValue>, one item per key found:

  • Key: The key found.
  • Value: The value associated with the key.
  • Revision: The current revision of the key.
  • LastModified: The Unix timestamp in milliseconds of the key's last modification.

Delete​

Deletes a key and its associated value. Deleting a key does not remove the key history. Script syntax: delete and edelete.

Task<KahunaKeyValue> DeleteKeyValue(
string key,
KeyValueDurability durability = KeyValueDurability.Persistent
);
  • key: The key to be deleted.
  • durability: Defines whether the key durability is Ephemeral or Persistent.

Returns:

  • Success: true if the key/value pair was deleted.
  • Revision: The tombstone revision created by the delete. For example, deleting a key at revision 0 returns revision 1, and the next successful set returns revision 2.

Extend​

Sets a new expiration time on a key. The key is deleted when it expires. An expiration of 0 means the key does not expire. Script syntax: extend and eextend.

Task<KahunaKeyValue> ExtendKeyValue(
string key,
int expiresMs,
KeyValueDurability durability = KeyValueDurability.Persistent
);
  • key: The key to be extended.
  • expiresMs: The expiration time of the key in milliseconds.
  • durability: Defines whether the key durability is Ephemeral or Persistent.

Returns:

  • Success: true if the key/value pair was extended.
  • Revision: The key's current revision. Extending the key does not increment the revision counter.

Exists​

Checks whether a key exists. Script syntax: exists and eexists.

Task<KahunaKeyValue> ExistsKeyValue(
string key,
KeyValueDurability durability = KeyValueDurability.Persistent
);
  • key: The key to be checked if exists.
  • durability: Defines whether the key durability is Ephemeral or Persistent.

Returns:

  • Success: true if the key/value pair exists.
  • Revision: The key's current revision at the time of the query.