Skip to content

The Key-Value and Object Stores

Outcome: you have stored settings by name and read them back with their revision history, and stored a whole file and received it back byte-for-byte identical — both on machinery you now know is streams underneath.

JetStream's streams are general machinery. Two of the things people most often build on such machinery are so predictable that NATS builds them for you: a table of named values, and a bucket of files. Both are streams with a fixed shape, wearing friendlier commands.

The key-value store: settings with a memory

nats kv add SETTINGS
nats kv put SETTINGS shop.name "The Corner Shop"
nats kv put SETTINGS shop.currency "EUR"
nats kv put SETTINGS shop.currency "USD"
$ nats kv get SETTINGS shop.currency
SETTINGS > shop.currency revision: 3 created @ 2026-11-03 00:57:29

USD

Every put is a new revision, numbered across the bucket: the shop's name was revision 1, the two currency values revisions 2 and 3. kv get returns the latest; kv history shows the trail a key has left, and any revision can be read back while it is kept — which makes the store a settings table that can answer "what did this say before the change?", the question every plain config file fails at exactly when it matters.

The honest machinery shows if you look at the store directory (transcript t08): beside the lab's hand-made SHOP stream sits KV_SETTINGS — the bucket is a stream, its keys encoded as subjects. Nothing new to operate: streams' limits, storage and restart behaviour are the bucket's too.

Two more properties, stated from the documentation because the lab's single client could not exhibit them: any client can watch a key (or a whole bucket) and be told the moment a value changes — the standard way programs pick up new settings without restarting — and a put can be made conditional ("only if the current revision is 3"), which settles two writers racing over one key.

The object store: files, in chunks, with a checksum

nats object add FILES
nats object put FILES payload.txt
nats object get FILES payload.txt -O roundtrip.txt

The lab's payload was a 52-byte text file. object put reported it stored as 1 chunk with a SHA-256 digest; large files simply take more chunks (the default chunk size is 128 KB), which is how the 1 MB message ceiling from chapter 2 stops applying to files: the file never travels as one message. The return trip is the proof that matters (transcript t13):

848a30bda62758c6db395b34da0b5c6f  payload.txt
848a30bda62758c6db395b34da0b5c6f  roundtrip.txt
files identical

Same checksum, same bytes. nats object ls FILES lists the bucket like a directory. An object store is not a filesystem and not a backup system — no folders to browse, no versioning promises beyond the stream's own retention — but for "many programs need the same artefacts, templates, and blobs", it puts the files where the messages already are.

It all survives the server

Both stores inherit chapter 8's ending. When the lab restarted its server against the same store directory (transcript t09), the SHOP stream still held its three messages, and:

$ nats kv get SETTINGS shop.name
SETTINGS > shop.name revision: 1 ...

The Corner Shop

Restarting the server is routine; the store directory is the memory. Backing up a JetStream system starts — and nearly ends — with backing up that directory while the server is stopped or by the server's own snapshot commands, a topic chapter 13 points at honestly.

Check: put a key twice with different values, kv get it (the second value, revision 2 or later), then restart your server and get it again. Same value, same revision — the store kept it, not the process.