NATS Key/Value Store Walkthrough
You can then check that JetStream is enabled by using
Which should output something like:
Connection Information:
Client ID: 6
Client IP: 127.0.0.1
Headers Supported: true
Maximum Payload: 1.0 MiB
Connected URL: nats://127.0.0.1:4222
Connected Address: 127.0.0.1:4222
Connected Server ID: ND2XVDA4Q363JOIFKJTPZW3ZKZCANH7NJI4EJMFSSPTRXDBFG4M4C34K
JetStream Account Information:
Memory: 0 B of Unlimited
Storage: 0 B of Unlimited
Streams: 0 of Unlimited
If you see the below instead then JetStream is not enabled
JetStream Account Information:
JetStream is not supported in this account
Creating a KV bucket
Just like you have to create streams before you can use them, you need to first create a ‘KV bucket’ using nats kv add <KV Bucket Name>
:
nats kv add my_kv
Now that we have a bucket, we can use it to ‘put’ (store) values at keys:
which should return Value1
Getting a value
Now that we have value stored at key “Key1” we can retrieve that value with a ‘get’:
nats kv get my_kv Key1
which should output
my_kv > Key1 created @ 12 Oct 21 20:08 UTC
Value1
Watching a K/V Store
A functionality (normally not provided by Key/Value stores) is available with the NATS KV Store is the ability to ‘watch’ a bucket (or a particular key in that bucket) and receive real-time updates to changes in the store.
For example run nats kv watch my_kv
: this will start a watcher on the bucket we have just created earlier. If you followed this walkthrough the last operation that happened on the key is that it was deleted. Because by default the KV bucket is set with a history size of one (i.e. it keeps only the last change) and the last operation on the bucket was a delete of the value associated with the key “Key1” that is the only thing that get received by the watcher:
which should output
[2021-10-12 13:15:03] DEL my_kv > Key1
Keep that nats kv watch
running and in another window do another ‘put’
nats kv put my_kv Key1 Value2
Once you are finished playing, you can easily delete the KV bucket and release the resource associated with it by using: