liveBook · Manning

Wasi:keyvalue

As explained in Server-Side WebAssembly

wasi:keyvalue is a standardized WebAssembly interface for accessing key-value storage from WebAssembly components.

wasi:keyvalue is a portable interface for giving a WebAssembly component persistent key-value storage without binding its business logic to one storage product. It covers familiar get and set operations, plus atomics for safe concurrent updates and batch operations for eventually consistent work. Hosts can supply providers such as NATS, Redis, or HashiCorp Vault behind the same interface. section 6.2 section 6.7

A component imports only the interfaces it needs. The store interface opens a host-provided bucket, a collection of key-value pairs; atomics supports operations such as increment and compare-and-swap. In wasmCloud, a manifest links those imports to a key-value capability provider and can configure a default bucket and automatic bucket creation. section 6.2.1

Listing 6.6 wadm.yaml (template comments/annotations hidden)
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: rust-hello-world
  # ...
spec:
  components:
    - name: http-component
      type: component
      properties:
        image: file://./build/http_hello_world_s.wasm
      traits:
        - type: spreadscaler
          properties:
            instances: 1
        - type: link #1
          properties: 
            namespace: wasi 
            package: keyvalue 
            interfaces: [store, atomics] 
            target: #2
              name: kvnats
              config: #3
                - name: wasi-keyvalue-config  #3
                  properties:  #3
                    bucket: default  #3
                    enable_bucket_auto_create:  #3
                    ➥'true'  #3
 #3
    - name: httpserver
      type: capability
      properties:
        image: ghcr.io/wasmcloud/http-server:0.23.2
      traits:
        - type: link
          properties:
            target: http-component
            namespace: wasi
            package: http
            interfaces: [incoming-handler]
            source_config:
              - name: default-http
                properties:
                  address: 127.0.0.1:8000

    - name: kvnats #4
      type: capability  #4
      properties:  #4
        image: ghcr.io/wasmcloud/keyvalue-nats:0.3.1  #4

The manifest link connects the component’s wasi:keyvalue store and atomics imports to the kvnats provider, with bucket configuration kept outside application code. section 6.2.1

Persistent request counter

An HTTP handler opens the default bucket, atomically increments the counter key, and returns the updated count. This allows concurrent requests to update persistent state while the handler remains focused on its response logic. section 6.2.2

Store and retrieve SmartCMS stories

A create route reads a story name and content from an HTTP request, then stores the content as bytes under that name. A retrieve route reads the name, gets the value from the bucket, and returns either the stored story or a not-found response. section 6.6.1

How Server-Side WebAssembly covers it

How deep it goes
Explains WIT imports, bucket access, atomics, provider configuration, deployment, and HTTP handlers, while noting minimal production error handling.
Examples
Uses a concurrent HTTP counter and SmartCMS create/retrieve routes that store named story content in a default bucket.
What it assumes
Builds from a custom key-value interface and assumes familiarity with WIT, wasmCloud components, manifests, and HTTP handlers.
What you can do after
Apply it via wasmCloud by importing store and atomics, linking a provider, and reading or updating bucket values.
How it connects
Connects WebAssembly components with WIT, wasmCloud, wadm, persistent storage, HTTP handlers, atomics, NATS, Redis, and HashiCorp Vault.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.