Taubyte provides key-value databases that you can access directly from your serverless functions. Like storage, databases are instantiated on-the-fly when first used—enabling dynamic, multi-tenant data isolation without manually creating each database instance.

Creating a Database

From the sidebar, navigate to Databases and click the + button.

Creating a database

Configure your database:

FieldDescriptionExample
NameUnique identifierexample_kv_store
MatcherPath pattern/example/kv
Min ReplicationMinimum copies1
Max ReplicationMaximum copies2
SizeMaximum capacity100MB

Click Validate to save.

Understanding Matchers

Just like storage, the matcher can be any string or regular expression. Using a path-like format (e.g., /example/kv) keeps things organized and readable.

Note: This configuration only exists locally in your browser’s virtual file system. Click the green button to push it to GitHub.

How Databases Work

Taubyte databases are instantiated on-the-fly the first time you use them. This enables powerful patterns:

Static matcher:

/example/kv

Dynamic matcher (regex):

profile/history/[a-zA-Z0-9]+

With the regex matcher:

  • profile/history/userA → Creates a database for User A
  • profile/history/userB → Creates a separate database for User B

This is incredibly useful for multi-user applications—you can isolate data per user without manually creating each database.

Building Database Functions

Let’s create two functions to interact with our database:

  1. KV Set: Store a key-value pair
  2. KV Get: Retrieve a value by key

Set Function

Navigate to Functions and click +:

FieldValue
Namekv_set
Memory10MB
MethodPOST
Path/api/kv
Entry Pointset

Switch to the Code tab and paste:

package lib

import (
    "encoding/json"

    "github.com/taubyte/go-sdk/database"
    "github.com/taubyte/go-sdk/event"
    http "github.com/taubyte/go-sdk/http/event"
)

func fail(h http.Event, err error, code int) uint32 {
    h.Write([]byte(err.Error()))
    h.Return(code)
    return 1
}

type Req struct {
    Key   string `json:"key"`
    Value string `json:"value"`
}

//export set
func set(e event.Event) uint32 {
    h, err := e.HTTP()
    if err != nil {
        return 1
    }

    // (Create) & Open the database
    db, err := database.New("/example/kv")
    if err != nil {
        return fail(h, err, 500)
    }

    // Decode the request body
    reqDec := json.NewDecoder(h.Body())
    defer h.Body().Close()

    // Decode the request body
    var req Req
    err = reqDec.Decode(&req)
    if err != nil {
        return fail(h, err, 500)
    }

    // Put the key/value into the database
    err = db.Put(req.Key, []byte(req.Value))
    if err != nil {
        return fail(h, err, 500)
    }

    return 0
}

Validate and push the function.

Get Function

Clone the kv_set function and modify:

FieldValue
Namekv_get
MethodGET
Entry Pointget

Update the code:

package lib

import (
    "github.com/taubyte/go-sdk/database"
    "github.com/taubyte/go-sdk/event"
    http "github.com/taubyte/go-sdk/http/event"
)

func fail(h http.Event, err error, code int) uint32 {
    h.Write([]byte(err.Error()))
    h.Return(code)
    return 1
}

//export get
func get(e event.Event) uint32 {
    h, err := e.HTTP()
    if err != nil {
        return 1
    }

    key, err := h.Query().Get("key")
    if err != nil {
        return fail(h, err, 400)
    }

    db, err := database.New("/example/kv")
    if err != nil {
        return fail(h, err, 500)
    }

    value, err := db.Get(key)
    if err != nil {
        return fail(h, err, 500)
    }

    h.Write(value)
    h.Return(200)

    return 0
}

Validate and push.

Building and Testing

Trigger the build:

dream inject push-all

Test Set

Once the build is done, you can test the function by sending a POST request to the endpoint:

curl -X POST http://your-domain.blackhole.localtau:14529/api/kv -H "Content-Type: application/json" -d '{
  "key": "message",
  "value": "hello world!"
}'

Note: Replace your-domain.blackhole.localtau with your own domain and 14529 with your own port.

Unless the curl fails, we now have a key message that contains hello world! in our database.

Test Get

Wait for the build to finish, then test the function by sending a GET request to the endpoint:

curl http://your-domain.blackhole.localtau:14529/api/kv?key=message

Output:

hello world!

Database Operations

OperationMethodDescription
Open(path)-Open or create a database
Put(key, value)-Store a key-value pair
Get(key)-Retrieve value by key
Delete(key)-Remove a key-value pair
List(prefix)-List keys with prefix
Close()-Close database connection

Replication

The replication settings control data durability:

SettingDescription
Min ReplicationMinimum copies across nodes
Max ReplicationMaximum copies for redundancy

Higher replication = more durability, more storage used.

Conclusion

You’ve learned how to:

  1. Create databases with configurable replication
  2. Build set/get functions for key-value operations
  3. Use dynamic matchers for per-user data isolation

Taubyte’s on-the-fly database creation eliminates the need to pre-provision databases, making it perfect for dynamic, multi-tenant applications.

Next, explore Messaging with Pub/Sub for real-time communication.