Key Value Store

As the Key Value Store is built on top of the JetStream persistence layer you obtain a KeyValueManager object from your JetStream context.

Creating, and deleting KV buckets

You can create as many independent key/value store instance, called ‘buckets’, as you need. Buckets are typically created, purged or deleted administratively (e.g. using the nats CLI tool), but this can also be done using one of the following KeyValueManager calls:

Go

  1. // KeyValue will lookup and bind to an existing KeyValue store.
  2. KeyValue(bucket string) (KeyValue, error)
  3. // CreateKeyValue will create a KeyValue store with the following configuration.
  4. CreateKeyValue(cfg *KeyValueConfig) (KeyValue, error)
  5. // DeleteKeyValue will delete this KeyValue store (JetStream stream).
  6. DeleteKeyValue(bucket string) error

Java

  1. /**
  2. * Create a key value store.
  3. * @param config the key value configuration
  4. * @return bucket info
  5. * @throws IOException covers various communication issues with the NATS
  6. * server such as timeout or interruption
  7. * @throws JetStreamApiException the request had an error related to the data
  8. * @throws IllegalArgumentException the server is not JetStream enabled
  9. */
  10. KeyValueStatus create(KeyValueConfiguration config) throws IOException, JetStreamApiException;
  11. /**
  12. * Get the list of bucket names.
  13. * @return list of bucket names
  14. * @throws IOException covers various communication issues with the NATS
  15. * server such as timeout or interruption
  16. * @throws JetStreamApiException the request had an error related to the data
  17. * @throws InterruptedException if the thread is interrupted
  18. */
  19. List<String> getBucketNames() throws IOException, JetStreamApiException, InterruptedException;
  20. /**
  21. * Gets the info for an existing bucket.
  22. * @param bucketName the bucket name to use
  23. * @throws IOException covers various communication issues with the NATS
  24. * server such as timeout or interruption
  25. * @throws JetStreamApiException the request had an error related to the data
  26. * @return the bucket status object
  27. */
  28. KeyValueStatus getBucketInfo(String bucketName) throws IOException, JetStreamApiException;
  29. /**
  30. * Deletes an existing bucket. Will throw a JetStreamApiException if the delete fails.
  31. * @param bucketName the stream name to use.
  32. * @throws IOException covers various communication issues with the NATS
  33. * server such as timeout or interruption
  34. * @throws JetStreamApiException the request had an error related to the data
  35. */
  36. void delete(String bucketName) throws IOException, JetStreamApiException;

JavaScript

  1. static async create(
  2. js: JetStreamClient,
  3. name: string,
  4. opts: Partial<KvOptions> = {},
  5. ): Promise<KV>
  6. static async bind(
  7. js: JetStreamClient,
  8. name: string,
  9. opts: Partial<{ codec: KvCodecs }> = {},
  10. ): Promise<KV>
  11. destroy(): Promise<boolean>

Python

  1. # from the JetStreamContext
  2. async def key_value(self, bucket: str) -> KeyValue:
  3. async def create_key_value(
  4. self,
  5. config: Optional[api.KeyValueConfig] = None,
  6. **params,
  7. ) -> KeyValue:
  8. """
  9. create_key_value takes an api.KeyValueConfig and creates a KV in JetStream.
  10. """
  11. async def delete_key_value(self, bucket: str) -> bool:
  12. """
  13. delete_key_value deletes a JetStream KeyValue store by destroying
  14. the associated stream.
  15. """

C

  1. NATS_EXTERN natsStatus kvConfig_Init (kvConfig *cfg)
  2. Initializes a KeyValue configuration structure.
  3. NATS_EXTERN natsStatus js_CreateKeyValue (kvStore **new_kv, jsCtx *js, kvConfig *cfg)
  4. Creates a KeyValue store with a given configuration.
  5. NATS_EXTERN natsStatus js_KeyValue (kvStore **new_kv, jsCtx *js, const char *bucket)
  6. Looks-up and binds to an existing KeyValue store.
  7. NATS_EXTERN natsStatus js_DeleteKeyValue (jsCtx *js, const char *bucket)
  8. Deletes a KeyValue store.
  9. NATS_EXTERN void kvStore_Destroy (kvStore *kv)
  10. Destroys a KeyValue store object.

C#

  1. /// <summary>
  2. /// Create a key value store.
  3. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  4. /// </summary>
  5. /// <param name="config">the key value configuration</param>
  6. /// <returns></returns>
  7. KeyValueStatus Create(KeyValueConfiguration config);
  8. /// <summary>
  9. /// Get the list of bucket names.
  10. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  11. /// </summary>
  12. /// <returns>list of bucket names</returns>
  13. IList<string> GetBucketNames();
  14. /// <summary>
  15. /// Gets the info for an existing bucket.
  16. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  17. /// </summary>
  18. /// <param name="bucketName">the bucket name to use</param>
  19. /// <returns>the bucket status object</returns>
  20. KeyValueStatus GetBucketInfo(string bucketName);
  21. /// <summary>
  22. /// Deletes an existing bucket. Will throw a NATSJetStreamException if the delete fails.
  23. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  24. /// </summary>
  25. /// <param name="bucketName"></param>
  26. void Delete(string bucketName);

{% endtabs %}

Getting

You can do a get to get the current value on a key, or ask to get a specific revision of the value.

Go

  1. // Get returns the latest value for the key.
  2. Get(key string) (entry KeyValueEntry, err error)
  3. // GetRevision returns a specific revision value for the key.
  4. GetRevision(key string, revision uint64) (entry KeyValueEntry, err error)

Java

  1. /**
  2. * Get the entry for a key
  3. * @param key the key
  4. * @return the KvEntry object or null if not found.
  5. * @throws IOException covers various communication issues with the NATS
  6. * server such as timeout or interruption
  7. * @throws JetStreamApiException the request had an error related to the data
  8. * @throws IllegalArgumentException the server is not JetStream enabled
  9. */
  10. KeyValueEntry get(String key) throws IOException, JetStreamApiException;
  11. /**
  12. * Get the specific revision of an entry for a key.
  13. * @param key the key
  14. * @param revision the revision
  15. * @return the KvEntry object or null if not found.
  16. * @throws IOException covers various communication issues with the NATS
  17. * server such as timeout or interruption
  18. * @throws JetStreamApiException the request had an error related to the data
  19. * @throws IllegalArgumentException the server is not JetStream enabled
  20. */
  21. KeyValueEntry get(String key, long revision) throws IOException, JetStreamApiException;

JavaScript %}

  1. async get(k: string): Promise<KvEntry | null>

Python

  1. async def get(self, key: str) -> Entry:
  2. """
  3. get returns the latest value for the key.
  4. """

C

  1. NATS_EXTERN natsStatus kvStore_Get (kvEntry **new_entry, kvStore *kv, const char *key)
  2. Returns the latest entry for the key.
  3. NATS_EXTERN natsStatus kvStore_GetRevision (kvEntry **new_entry, kvStore *kv, const char *key, uint64_t revision)
  4. Returns the entry at the specific revision for the key.

C#

  1. /// <summary>
  2. /// Get the entry for a key
  3. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  4. /// </summary>
  5. /// <param name="key">the key</param>
  6. /// <returns>The entry</returns>
  7. KeyValueEntry Get(string key);
  8. /// <summary>
  9. /// Get the specific revision of an entry for a key
  10. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  11. /// </summary>
  12. /// <param name="key">the key</param>
  13. /// <param name="revision">the specific revision</param>
  14. /// <returns>The entry</returns>
  15. KeyValueEntry Get(string key, ulong revision);

{% endtabs %}

Putting

The key is always a string, you can simply use Put to store a byte array, or the convenience PutString to put a string. For ‘compare and set’ functionality you can use Create and Update.

Go

  1. Put(key string, value []byte) (revision uint64, err error)
  2. // PutString will place the string for the key into the store.
  3. PutString(key string, value string) (revision uint64, err error)
  4. // Create will add the key/value pair iff it does not exist.
  5. Create(key string, value []byte) (revision uint64, err error)
  6. // Update will update the value iff the latest revision matches.
  7. Update(key string, value []byte, last uint64) (revision uint64, err error)

Java

  1. /**
  2. * Put a byte[] as the value for a key
  3. * @param key the key
  4. * @param value the bytes of the value
  5. * @return the revision number for the key
  6. * @throws IOException covers various communication issues with the NATS
  7. * server such as timeout or interruption
  8. * @throws JetStreamApiException the request had an error related to the data
  9. * @throws IllegalArgumentException the server is not JetStream enabled
  10. */
  11. long put(String key, byte[] value) throws IOException, JetStreamApiException;
  12. /**
  13. * Put a string as the value for a key
  14. * @param key the key
  15. * @param value the UTF-8 string
  16. * @return the revision number for the key
  17. * @throws IOException covers various communication issues with the NATS
  18. * server such as timeout or interruption
  19. * @throws JetStreamApiException the request had an error related to the data
  20. * @throws IllegalArgumentException the server is not JetStream enabled
  21. */
  22. long put(String key, String value) throws IOException, JetStreamApiException;
  23. /**
  24. * Put a long as the value for a key
  25. * @param key the key
  26. * @param value the number
  27. * @return the revision number for the key
  28. * @throws IOException covers various communication issues with the NATS
  29. * server such as timeout or interruption
  30. * @throws JetStreamApiException the request had an error related to the data
  31. * @throws IllegalArgumentException the server is not JetStream enabled
  32. */
  33. long put(String key, Number value) throws IOException, JetStreamApiException;
  34. /**
  35. * Put as the value for a key iff the key does not exist (there is no history)
  36. * or is deleted (history shows the key is deleted)
  37. * @param key the key
  38. * @param value the bytes of the value
  39. * @return the revision number for the key
  40. * @throws IOException covers various communication issues with the NATS
  41. * server such as timeout or interruption
  42. * @throws JetStreamApiException the request had an error related to the data
  43. * @throws IllegalArgumentException the server is not JetStream enabled
  44. */
  45. long create(String key, byte[] value) throws IOException, JetStreamApiException;
  46. /**
  47. * Put as the value for a key iff the key exists and its last revision matches the expected
  48. * @param key the key
  49. * @param value the bytes of the value
  50. * @param expectedRevision the expected last revision
  51. * @return the revision number for the key
  52. * @throws IOException covers various communication issues with the NATS
  53. * server such as timeout or interruption
  54. * @throws JetStreamApiException the request had an error related to the data
  55. * @throws IllegalArgumentException the server is not JetStream enabled
  56. */
  57. long update(String key, byte[] value, long expectedRevision) throws IOException, JetStreamApiException;

JavaScript

  1. async put(
  2. k: string,
  3. data: Uint8Array,
  4. opts: Partial<KvPutOptions> = {},
  5. ): Promise<number>
  6. create(k: string, data: Uint8Array): Promise<number>
  7. update(k: string, data: Uint8Array, version: number): Promise<number>

Python

  1. async def put(self, key: str, value: bytes) -> int:
  2. """
  3. put will place the new value for the key into the store
  4. and return the revision number.
  5. """
  6. async def update(self, key: str, value: bytes, last: int) -> int:
  7. """
  8. update will update the value iff the latest revision matches.
  9. """

C

  1. NATS_EXTERN natsStatus kvStore_Put (uint64_t *rev, kvStore *kv, const char *key, const void *data, int len)
  2. Places the new value for the key into the store.
  3. NATS_EXTERN natsStatus kvStore_PutString (uint64_t *rev, kvStore *kv, const char *key, const char *data)
  4. Places the new value (as a string) for the key into the store.
  5. NATS_EXTERN natsStatus kvStore_Create (uint64_t *rev, kvStore *kv, const char *key, const void *data, int len)
  6. Places the value for the key into the store if and only if the key does not exist.
  7. NATS_EXTERN natsStatus kvStore_CreateString (uint64_t *rev, kvStore *kv, const char *key, const char *data)
  8. Places the value (as a string) for the key into the store if and only if the key does not exist.
  9. NATS_EXTERN natsStatus kvStore_Update (uint64_t *rev, kvStore *kv, const char *key, const void *data, int len, uint64_t last)
  10. Updates the value for the key into the store if and only if the latest revision matches.
  11. NATS_EXTERN natsStatus kvStore_UpdateString (uint64_t *rev, kvStore *kv, const char *key, const char *data, uint64_t last)
  12. Updates the value (as a string) for the key into the store if and only if the latest revision matches.

C#

  1. /// <summary>
  2. /// Put a byte[] as the value for a key
  3. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  4. /// </summary>
  5. /// <param name="key">the key</param>
  6. /// <param name="value">the bytes of the value</param>
  7. /// <returns>the revision number for the key</returns>
  8. ulong Put(string key, byte[] value);
  9. /// <summary>
  10. /// Put a string as the value for a key
  11. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  12. /// </summary>
  13. /// <param name="key">the key</param>
  14. /// <param name="value">the UTF-8 string</param>
  15. /// <returns>the revision number for the key</returns>
  16. ulong Put(string key, string value);
  17. /// <summary>
  18. ///Put a long as the value for a key
  19. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  20. /// </summary>
  21. /// <param name="key">the key</param>
  22. /// <param name="value">the number</param>
  23. /// <returns>the revision number for the key</returns>
  24. ulong Put(string key, long value);
  25. /// <summary>
  26. /// Put as the value for a key iff the key does not exist (there is no history)
  27. /// or is deleted (history shows the key is deleted)
  28. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  29. /// </summary>
  30. /// <param name="key">the key</param>
  31. /// <param name="value">the bytes of the value</param>
  32. /// <returns>the revision number for the key</returns>
  33. ulong Create(string key, byte[] value);
  34. /// <summary>
  35. /// Put as the value for a key iff the key exists and its last revision matches the expected
  36. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  37. /// </summary>
  38. /// <param name="key">the key</param>
  39. /// <param name="value">the bytes of the value</param>
  40. /// <param name="expectedRevision"></param>
  41. /// <returns>the revision number for the key</returns>
  42. ulong Update(string key, byte[] value, ulong expectedRevision);

{% endtabs %}

Deleting

You can delete a specific key, or purge the whole key/value bucket.

Go

  1. // Delete will place a delete marker and leave all revisions.
  2. Delete(key string) error
  3. // Purge will place a delete marker and remove all previous revisions.
  4. Purge(key string) error

Java

  1. /**
  2. * Soft deletes the key by placing a delete marker.
  3. * @param key the key
  4. * @throws IOException covers various communication issues with the NATS
  5. * server such as timeout or interruption
  6. * @throws JetStreamApiException the request had an error related to the data
  7. */
  8. void delete(String key) throws IOException, JetStreamApiException;
  9. /**
  10. * Purge all values/history from the specific key
  11. * @param key the key
  12. * @throws IOException covers various communication issues with the NATS
  13. * server such as timeout or interruption
  14. * @throws JetStreamApiException the request had an error related to the data
  15. */
  16. void purge(String key) throws IOException, JetStreamApiException;

JavaScript

  1. delete(k: string): Promise<void>
  2. purge(k: string): Promise<void>

Python

  1. async def delete(self, key: str) -> bool:
  2. """
  3. delete will place a delete marker and remove all previous revisions.
  4. """
  5. async def purge(self, key: str) -> bool:
  6. """
  7. purge will remove the key and all revisions.
  8. """

C

  1. NATS_EXTERN natsStatus kvStore_Delete (kvStore *kv, const char *key)
  2. Deletes a key by placing a delete marker and leaving all revisions.
  3. NATS_EXTERN natsStatus kvStore_Purge (kvStore *kv, const char *key, kvPurgeOptions *opts)
  4. Deletes a key by placing a purge marker and removing all revisions.
  5. NATS_EXTERN natsStatus kvStore_PurgeDeletes (kvStore *kv, kvPurgeOptions *opts)
  6. Purge and removes delete markers.

C#

  1. /// <summary>
  2. /// Soft deletes the key by placing a delete marker.
  3. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  4. /// </summary>
  5. /// <param name="key">the key</param>
  6. void Delete(string key);
  7. /// <summary>
  8. /// Purge all values/history from the specific key.
  9. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  10. /// </summary>
  11. /// <param name="key">the key</param>
  12. void Purge(string key);
  13. /// <summary>
  14. /// Remove history from all keys that currently are deleted or purged,
  15. /// using a default KeyValuePurgeOptions
  16. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  17. /// </summary>
  18. void PurgeDeletes();
  19. /// <summary>
  20. /// Remove history from all keys that currently are deleted or purged
  21. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  22. /// </summary>
  23. void PurgeDeletes(KeyValuePurgeOptions options);

{% endtabs %}

Getting all the keys

You can get the list of all the keys currently having a value associated using Keys()

Go

  1. // Keys will return all keys.
  2. Keys(opts ...WatchOpt) ([]string, error)

Java

  1. /**
  2. * Get a list of the keys in a bucket.
  3. * @return List of keys
  4. * @throws IOException covers various communication issues with the NATS
  5. * server such as timeout or interruption
  6. * @throws JetStreamApiException the request had an error related to the data
  7. * @throws InterruptedException if the thread is interrupted
  8. */
  9. List<String> keys() throws IOException, JetStreamApiException, InterruptedException;

JavaScript

  1. async keys(k = ">"): Promise<QueuedIterator<string>>

C

  1. NATS_EXTERN natsStatus kvStore_Keys (kvKeysList *list, kvStore *kv, kvWatchOptions *opts)
  2. Returns all keys in the bucket.
  3. NATS_EXTERN void kvKeysList_Destroy (kvKeysList *list)
  4. Destroys this list of KeyValue store key strings.

C#

  1. /// <summary>
  2. /// Get a list of the keys in a bucket.
  3. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  4. /// </summary>
  5. /// <returns>The list of keys</returns>
  6. IList<string> Keys();

{% endtabs %}

Getting the history for a key

The JetStream key/value store has a feature you don’t usually find in key/value stores: the ability to keep a history of the values associated with a key (rather than just the current value). The depth of the history is specified when the key/value bucket is created, and the default is a history depth of 1 (i.e. no history).

Go

  1. // History will return all historical values for the key.
  2. History(key string, opts ...WatchOpt) ([]KeyValueEntry, error)

Java

  1. /**
  2. * Get the history (list of KeyValueEntry) for a key
  3. * @param key the key
  4. * @return List of KvEntry
  5. * @throws IOException covers various communication issues with the NATS
  6. * server such as timeout or interruption
  7. * @throws JetStreamApiException the request had an error related to the data
  8. * @throws InterruptedException if the thread is interrupted
  9. */
  10. List<KeyValueEntry> history(String key) throws IOException, JetStreamApiException, InterruptedException;

JavaScript

  1. async history(
  2. opts: { key?: string; headers_only?: boolean } = {},
  3. ): Promise<QueuedIterator<KvEntry>>

C

  1. NATS_EXTERN natsStatus kvStore_History (kvEntryList *list, kvStore *kv, const char *key, kvWatchOptions *opts)
  2. Returns all historical entries for the key.
  3. NATS_EXTERN void kvEntryList_Destroy (kvEntryList *list)
  4. Destroys this list of KeyValue store entries.

C#

  1. /// <summary>
  2. /// Get the history (list of KeyValueEntry) for a key
  3. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  4. /// </summary>
  5. /// <param name="key">the key</param>
  6. /// <returns>The list of KeyValueEntry</returns>
  7. IList<KeyValueEntry> History(string key);

{% endtabs %}

Watching for changes

Watching a key/value bucket is like subscribing to updates: you provide a callback and you can watch all of the keys in the bucket or specify which specific key(s) you want to be kept updated about.

Go

  1. // Watch for any updates to keys that match the keys argument which could include wildcards.
  2. // Watch will send a nil entry when it has received all initial values.
  3. Watch(keys string, opts ...WatchOpt) (KeyWatcher, error)
  4. // WatchAll will invoke the callback for all updates.
  5. WatchAll(opts ...WatchOpt) (KeyWatcher, error)

Java

  1. /**
  2. * Watch updates for a specific key
  3. * @param key the key
  4. * @param watcher the watcher
  5. * @param watchOptions the watch options to apply. If multiple conflicting options are supplied, the last options wins.
  6. * @return The KeyValueWatchSubscription
  7. * @throws IOException covers various communication issues with the NATS
  8. * server such as timeout or interruption
  9. * @throws JetStreamApiException the request had an error related to the data
  10. * @throws InterruptedException if the thread is interrupted
  11. */
  12. NatsKeyValueWatchSubscription watch(String key, KeyValueWatcher watcher, KeyValueWatchOption... watchOptions) throws IOException, JetStreamApiException, InterruptedException;
  13. /**
  14. * Watch updates for all keys
  15. * @param watcher the watcher
  16. * @param watchOptions the watch options to apply. If multiple conflicting options are supplied, the last options wins.
  17. * @return The KeyValueWatchSubscription
  18. * @throws IOException covers various communication issues with the NATS
  19. * server such as timeout or interruption
  20. * @throws JetStreamApiException the request had an error related to the data
  21. * @throws InterruptedException if the thread is interrupted
  22. */
  23. NatsKeyValueWatchSubscription watchAll(KeyValueWatcher watcher, KeyValueWatchOption... watchOptions) throws IOException, JetStreamApiException, InterruptedException;

JavaScript

  1. async watch(
  2. opts: {
  3. key?: string;
  4. headers_only?: boolean;
  5. initializedFn?: callbackFn;
  6. } = {},
  7. ): Promise<QueuedIterator<KvEntry>>

C

  1. NATS_EXTERN natsStatus kvStore_Watch (kvWatcher **new_watcher, kvStore *kv, const char *keys, kvWatchOptions *opts)
  2. Returns a watcher for any updates to keys that match the keys argument.
  3. NATS_EXTERN natsStatus kvStore_WatchAll (kvWatcher **new_watcher, kvStore *kv, kvWatchOptions *opts)
  4. Returns a watcher for any updates to any keys of the KeyValue store bucket.

C#

  1. /// <summary>
  2. /// Watch updates for a specific key
  3. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  4. /// WARNING: This api requires an internal consumer the enforces ordering of messages.
  5. /// This portion of the implementation is not complete yet. If there was some sort of
  6. /// error from the server and messages were skipped or came out of order the data received
  7. /// would be incomplete. While this is an edge case, it can still technically happen.
  8. /// </summary>
  9. /// <param name="key">the key</param>
  10. /// <param name="watcher">the watcher</param>
  11. /// <param name="watchOptions">the watch options to apply. If multiple conflicting options are supplied, the last options wins.</param>
  12. /// <returns></returns>
  13. KeyValueWatchSubscription Watch(string key, IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions);
  14. /// <summary>
  15. /// Watch updates for all keys
  16. /// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
  17. /// WARNING: This api requires an internal consumer the enforces ordering of messages.
  18. /// This portion of the implementation is not complete yet. If there was some sort of
  19. /// error from the server and messages were skipped or came out of order the data received
  20. /// would be incomplete. While this is an edge case, it can still technically happen.
  21. /// </summary>
  22. /// <param name="watcher">the watcher</param>
  23. /// <param name="watchOptions">the watch options to apply. If multiple conflicting options are supplied, the last options wins.</param>
  24. /// <returns>The KeyValueWatchSubscription</returns>
  25. KeyValueWatchSubscription WatchAll(IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions);

{% endtabs %}