Skip to content
中文

Key-value databases

A key-value database is the second kind of Database: it stores strings, hashes, lists, sets and sorted sets by key, with optional expiry. It is compatible with the Upstash Redis REST interface, so existing @upstash/redis code only needs a new URL and token.

Key-value or table

The path is the type: the path a database is created under decides its type, and the type never changes.

TypePathStoresAccessFits
Table database/database/v1/tableTables and rows with typed fieldsSupabase-compatible interfaceCatalogs, itineraries and operating data you filter and link
Key-value database/database/v1/kvKeys and values, optionally expiringUpstash Redis-compatible interface, native key interfaceCaches, counters, sessions, rate limits, leaderboards, de-duplication sets

Reaching a database through the other type’s path, for example a table database’s id on /database/v1/kv/{database_id}, returns 404 DATABASE_NOT_FOUND, the same as an unknown id. The create body takes no kind; one that carries it returns 400 INVALID_PARAMS, and kind in a response is read-only. Deletion (recoverable for 7 days), permanent purge, labels and ownership are the same as for table databases; see Concepts.

Create a key-value database

Prerequisites: an API key in the PRAXIS_API_KEY environment variable, and key-value databases opened for your organization. When they are not, creation returns 400 INVALID_PARAMS with the message kind kv is not available.

终端窗口
curl -X POST "https://api.deepraxis.com/database/v1/kv" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "name": "Session cache" }'

Expect: the returned database has kind kv, and its id is the <database_id> used below. You can also create one from the Console database list by choosing the type “Key-value”.

Connect with @upstash/redis

Two values are enough:

Environment variableValue
UPSTASH_REDIS_REST_URLhttps://api.deepraxis.com/redis/<database_id>
UPSTASH_REDIS_REST_TOKENA platform API key
终端窗口
npm install @upstash/redis

Save the following as kv.mjs, set the two variables, and run node kv.mjs.

import { Redis } from "@upstash/redis";
const redis = Redis.fromEnv();
// Equivalent: new Redis({ url: process.env.UPSTASH_REDIS_REST_URL, token: process.env.UPSTASH_REDIS_REST_TOKEN })
console.log(await redis.set("greeting", "hello")); // OK
console.log(await redis.get("greeting")); // hello
console.log(await redis.incr("counter")); // 1
// Expiry: the key disappears after 100 seconds
await redis.set("session:1", "alice", { ex: 100 });
console.log(await redis.ttl("session:1")); // greater than 0
console.log(await redis.persist("session:1")); // 1, expiry removed
console.log(await redis.ttl("session:1")); // -1
// Hash, list, sorted set
await redis.hset("user:1", { name: "alice", plan: "pro" });
console.log(await redis.hgetall("user:1")); // { name: "alice", plan: "pro" }
await redis.rpush("queue", "a", "b", "c");
console.log(await redis.lrange("queue", 0, -1)); // [ "a", "b", "c" ]
await redis.zadd("board", { score: 1, member: "a" }, { score: 2, member: "b" });
console.log(await redis.zrange("board", 0, -1)); // [ "a", "b" ]
// Pipeline: several commands in one request
const results = await redis.pipeline().set("p", "1").incr("p").get("p").exec();
console.log(results); // [ "OK", 2, 2 ]
// Multi-exec: run as one unit
console.log(await redis.multi().set("m", "1").incr("m").exec()); // [ "OK", 2 ]

Expect: each line prints what its comment says. The SDK parses values it reads as JSON, so a string written as "1" may read back as the number 1; convert it yourself when you need a string.

Without the SDK, send HTTP directly:

终端窗口
curl -X POST "https://api.deepraxis.com/redis/$DATABASE_ID" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d '["SET","greeting","hello"]'

Expect: {"result":"OK"}.

HTTP entry points, all under /redis/<database_id>:

RequestEffect
POST /redis/<id>The body is one command array, such as ["GET","k"]
POST /redis/<id>/pipelineThe body is an array of command arrays, run in one request; returns [{ "result": … } or { "error": … }, …], one item per command
POST /redis/<id>/multi-execLike pipeline, but run as one unit; if any command cannot be parsed the whole request returns 400 EXECABORT and nothing runs
GET /redis/<id>/set/k/vPath style: the command name and arguments go in the path

With the header Upstash-Encoding: base64, string replies are base64-encoded. @upstash/redis sends it by default; you need not handle it.

Native key interface

Use the native interface when you do not want a Redis client, or to browse keys from a script. Fields are snake_case; authentication is the same as the other Database interfaces.

OperationHTTPSDK
List keysGET /database/v1/kv/{database_id}/keyspraxis.database.kv.keys.list
Read a keyGET /database/v1/kv/{database_id}/keys/{key}praxis.database.kv.keys.retrieve
Write a whole valuePUT /database/v1/kv/{database_id}/keys/{key}praxis.database.kv.keys.set
Change expiryPATCH /database/v1/kv/{database_id}/keys/{key}praxis.database.kv.keys.update
DeleteDELETE /database/v1/kv/{database_id}/keys/{key}praxis.database.kv.keys.delete
Run one commandPOST /database/v1/kv/{database_id}/commandspraxis.database.kv.commands.execute
  • List keys: limit defaults to 50, maximum 500; cursor pages; pattern is a Redis glob; type filters by type. Returns { "data": [{ "key", "type", "ttl_ms", "size_bytes" }], "next_cursor" }.
  • Read a key: container values (hash, list, set, sorted set) are paged; limit defaults to 100, maximum 1000. A missing key returns 404 KEY_NOT_FOUND.
  • Write a whole value: body { "type", "value", "ttl_ms" } replaces the whole value. Omit ttl_ms to keep the existing expiry; a number is milliseconds; null means no expiry.
  • Change expiry: body { "update_mask": "ttl_ms", "ttl_ms": 60000 }.
  • Delete: returns { "deleted": true }, or { "deleted": false } when the key did not exist.
  • Run a command: body { "command": ["HGETALL", "user:1"] }, returns { "result": ... }. When Redis rejects the command the response is 400 KV_COMMAND_ERROR and message is Redis’s own text, such as WRONGTYPE Operation against a key holding the wrong kind of value.
终端窗口
curl -X POST "https://api.deepraxis.com/database/v1/kv/$DATABASE_ID/commands" \
-H "Authorization: Bearer $PRAXIS_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "command": ["GET", "greeting"] }'

Expect: {"result":"hello"} (the key was written in the previous section).

Full fields: keys, commands and the Upstash-compatible API.

Supported commands

Command names are case-insensitive.

CategoryCommands
StringsGET SET (supports NX XX GET KEEPTTL EX PX EXAT PXAT) SETNX SETEX PSETEX GETSET GETDEL GETEX MGET MSET MSETNX INCR DECR INCRBY DECRBY INCRBYFLOAT APPEND STRLEN GETRANGE SETRANGE
Keys and expiryDEL UNLINK EXISTS EXPIRE PEXPIRE EXPIREAT PEXPIREAT TTL PTTL PERSIST TYPE RENAME RENAMENX KEYS RANDOMKEY DBSIZE FLUSHDB
HashesHSET HSETNX HGET HMGET HGETALL HDEL HEXISTS HLEN HKEYS HVALS HINCRBY HINCRBYFLOAT HSTRLEN
ListsLPUSH RPUSH LPUSHX RPUSHX LPOP RPOP LLEN LRANGE LINDEX LSET LREM LTRIM LPOS LMOVE
SetsSADD SREM SMEMBERS SISMEMBER SMISMEMBER SCARD SPOP SRANDMEMBER SINTER SUNION SDIFF SINTERSTORE SUNIONSTORE SDIFFSTORE SMOVE
Sorted setsZADD ZREM ZSCORE ZMSCORE ZINCRBY ZCARD ZCOUNT ZRANK ZREVRANK ZRANGE ZREVRANGE ZRANGEBYSCORE ZREVRANGEBYSCORE ZREMRANGEBYRANK ZREMRANGEBYSCORE ZPOPMIN ZPOPMAX
Incremental iterationSCAN (supports MATCH COUNT TYPE) HSCAN SSCAN ZSCAN
OtherPING ECHO

Not supported:

  • Lua scripts: EVAL, EVALSHA.
  • Blocking commands: BLPOP, BRPOP and the like.
  • Publish/subscribe (Pub/Sub).
  • Streams.
  • Multi-database SELECT: a key-value database is one keyspace.

A command outside the table above returns ERR unknown command '<name>'.

Limits

LimitMaximum
Key length1024 bytes
One value1 MiB
One request body1 MiB
Commands in one pipeline or multi-exec1000
Reply of one call (for a pipeline, all results added up)8 MiB
One database1 GiB

A reply above 8 MiB returns ERR reply exceeds 8 MiB; read it in ranges (SCAN/HSCAN/LRANGE/ZRANGE with limits): read in ranges with SCAN, HSCAN, LRANGE or ZRANGE. SPOP, LPOP, RPOP, ZPOPMIN and ZPOPMAX with a count are rejected when their reply is over the limit, and the data is unchanged. When the database is full, write commands return OOM command not allowed when used memory > 'maxmemory'.. Expiry is checked on read, so an expired key cannot be read; a background job reclaims its space on a schedule. Other errors are in Limits and errors.

Permissions

A read-write credential can run every supported command. A read-only credential (resource scope access of read) can run read commands only; a write command returns 403:

NOPERM this credential is read-only for <database_id>

A command that cannot be parsed is treated as a write, so it also returns 403 for a read-only credential. On the native key interface, a write with a read-only credential returns 403 BINDING_READ_ONLY. Give callers that only read a read-only credential.

Performance

Each call is one network round trip to Hong Kong; the read or write then completes where the database lives, with no further network hop. Measured hot requests take about 40 ms to first byte from a US client, TLS included; a 100-command pipeline takes about as long as one command.

It can replace a serverless Redis such as Upstash; it is not a sub-millisecond in-memory Redis. Batch frequent small commands into a pipeline so they share one round trip.

Inspect and troubleshoot

  • Console: a key-value database’s detail page has Keys (browse, filter by pattern, edit values and expiry, create keys), Command line (run commands one at a time), Connect (the REST URL and an @upstash/redis example) and Settings tabs.
  • From a script: praxis.database.kv.keys.list lists keys by pattern and type; praxis.database.kv.commands.execute runs one command and shows its result.
  • On an error: see Limits and errors.

Next steps