Show HN:Nkv – a tiny key-value store with pub/sub and keyspaces for shared state

19 minutes ago 1

demo

nkv lets you share state between your services using client server topology. it provides you with following API:

  • get the state for the given key
  • put the state for the given key
  • delete the state for the given key
  • subscribe to the updates for the given key
  • unsubscribe from the updated for the given key

Note that nkv supports keyspace, more info you can find here.

Also note that nkv clients recieve latest state and transitional states might be lost due to load. For more information reffer to this

When you have some shared state between services/processes and you also want to be notified when the value is changed

You can directly pull docker containers for client and server. They are published with each release:

Make sure that you have docker installed

sudo mkdir /var/run/shared docker run --rm -it -v /var/run/shared:/tmp/nkv/ nkv ./nkv-client docker run --rm -it -v /var/run/shared:/tmp/nkv/ nkv ./nkv-server

when using network other than the host, the network implementation may impact network performance, unrelated to nkv itself. Also docker container builds nkv with musl, not glibc, which introduce slight perfomance degradation but gives way smaller container size, for more information refer to Design decisions doc

If you want latest version or you want to modify nkv to use different storage or notification policies, you can build and run nkv locally. In order to use it you should install Rust programming language. If you're running Linux or OSX you can do so via rustup

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

For Windows installation on and other methods see here

Then you can simply do following

git clone [email protected]:nkval/nkv.git cd nkv cargo build --release

And you'll see nkv-server and nkv-client binaries in target/release folder

Start a server by running the binary nkv-server. You can specify path to unix socket via --addr parameter, default value is /tmp/nkv/nkv.sock

You can specify log level, where to store logs and directory to store data, for more information run nkv-server --help to see available flags

./target/release/nkv-server --level debug

Then you can use a client to access the server.

You can use the client binary nkv-client to interact with the server, or any library that supports the protocol.

To run the client binary, you can use the following commands:

$ ./target/release/nkv-client get key1 put key1 value1 delete key1 quit help

Each line starts with the command, followed by the arguments. Enter the help command for a list of available commands. Each command ends with a newline character, after which the server responds.

You can specify path to UNIX socket via --addr argument, you can also run single shot commands, i.e.

./target/release/nkv-client get k1

depending on your shell, you might need to backslash * symbol. This could be useful if you're running nkv inside bash script.

For detailed info about design you can find here

What clients does nkv implement?

Apart from application you can use nkv in

However, underlying design principle is that nkv is using OS primitives, making it possible to write clients in any language. Just write marshalling and demarshalling in JSON and be sure that you can connect to Server and handle Notifier. For detailed specification on components reffer to this doc

Initially, idea for such design came up in discussions with @deitch talking about improving messaging system inside EVE project called PubSub. We clearly understood one thing: serverless distributed messaging between services did was not exactly the right way to approach the problem. What we actually needed was some kind of persistent key-value storage, but with a twist: we want it to notify us when there is a change in a value. So all the clients will be able to sync over the value. Such communication mechanism would be perfect for EVE. In terms of performance the important part for us was smaller footprint and we also kept in mind that number of connections will be hundreds maximum.

Read Entire Article