SKEG · GUIDES

Key-value records

Store content and binary values, use counters and understand multi-key writes.

Skeg 0.8 · Development documentation

Use key-value records for document text or other bytes you want to read by key. If the application also uses vector search, it can use a returned row ID to look up the corresponding record.

Start a local server

Install Skeg 0.8 and have redis-cli available. Use a fresh store and an unused local port. In the first terminal, start Skeg:

data_dir=$(mktemp -d) &&
  skeg --protocol resp3 --data-dir "$data_dir" --addr 127.0.0.1:6386

In another terminal, connect and enter the commands below at the prompt:

redis-cli -3 -h 127.0.0.1 -p 6386

On a shared server, sign in first. Keys belong to the tenant on the current connection.

Write and read a value

SET creates a key or replaces its entire value. Use GET to read it back:

SET kv_demo:title "Reset your password"
GET kv_demo:title

Expect OK followed by the stored title. An empty value is a byte string of length zero. A missing key returns null.

Store binary data

Values are byte strings. Your application can choose an encoding such as UTF-8 JSON and decode the bytes it reads. Skeg stores the value as supplied.

For the binary example, prepare a Python environment and download key_value.py. Run python key_value.py while the exercise server is listening on port 6386. The script writes three bytes, reads them back with decoding disabled, then deletes the key and checks its absence.

Expect Binary value verified and Deletion verified.

Append and count

APPEND adds bytes to the end of a value and returns the new byte length. On a missing key, the command creates the value.

SET kv_demo:message hello
APPEND kv_demo:message " world"

Expect a new length of 11 bytes. For a counter, use INCR, DECR, INCRBY or DECRBY. Counters are signed 64-bit integers stored as text. A missing counter starts at zero. Invalid integers and overflow return an error.

INCR kv_demo:visits
INCRBY kv_demo:visits 4
GET kv_demo:visits

On a fresh store, expect 1, then 5, then the stored text "5". A timeout can occur after the server has applied an increment. Follow write outcomes before resending it.

Multi-key operations

MGET returns values in request order, including null for each missing key. This example distinguishes a stored empty value from a missing key:

SET kv_demo:empty ""
MGET kv_demo:empty kv_demo:missing kv_demo:title

Expect an empty byte string, null and the title, in that order.

MSET applies the whole write atomically when every key routes to the same storage shard. A request that spans shards returns CROSSSLOT before writing any key. A shared prefix or a Redis hash tag does not ensure that Skeg routes the keys together. If the application requires an atomic update, retain that requirement when handling the refusal. Use separate writes only if the application can handle partial completion.

Remove the example records

EXISTS counts the named keys that are present. DEL removes keys and returns how many were removed. To remove the keys created in the terminal:

DEL kv_demo:title kv_demo:message kv_demo:visits kv_demo:empty

On a fresh run through all the steps, expect 4. A key deletion leaves a related vector row in place. Follow update and delete an article when the application needs to remove both.

Exit the client and stop Skeg with Ctrl+C. Remove the temporary data directory after shutdown if you no longer need it.

For supported commands and options, see protocol compatibility. For recovery, see backup and restore.