Why Deno KV Could Be Your Next Strongly Consistent Key‑Value Database
The article introduces Deno KV, a globally replicated, strongly consistent key‑value database that runs locally with SQLite or in Deno Deploy with FoundationDB, highlights its simple yet powerful JavaScript‑centric API, atomic transactions, consistency models, and provides practical code examples for developers.
Deno KV is a newly announced, strongly consistent key‑value database from the Deno team, replicated across 35 global regions to provide low‑latency reads.
Key features include:
Local or hosted operation : Run locally backed by SQLite for development and testing, or deploy as a zero‑configuration hosted service powered by FoundationDB.
Simple yet powerful : Stores any serializable JavaScript value such as objects, arrays, BigInts, dates, etc.
Basic usage example:
const kv = await Deno.openKv();
const key = ["users", crypto.randomUUID()];
const value = { name: "Alice", created: new Date() };
await kv.set(key, value);
const result = await kv.get(key);
console.log(result.value); // { name: "Alice", created: 2023-05-01T09:24:07.620Z }Listing keys with a prefix:
await kv.set(["users", "alice"], { birthday: "January 1, 1990" });
await kv.set(["users", "sam"], { birthday: "February 14, 1985" });
await kv.set(["users", "taylor"], { birthday: "December 25, 1970" });
const iter = kv.list({ prefix: ["users"] });
for await (const entry of iter) {
console.log(entry.key);
console.log(entry.value);
}Atomic transactions are created with kv.atomic(), allowing strong consistency across multiple keys:
const kv = await Deno.openKv();
const change = 10;
const bob = await kv.get(["balance", "bob"]);
const liz = await kv.get(["balance", "liz"]);
if (liz.value < change) { throw "not enough balance"; }
const success = await kv.atomic()
.check(bob, liz)
.set(["balance", "bob"], bob.value - change)
.set(["balance", "liz"], liz.value + change)
.commit();Deno KV supports two consistency models: serializability (the highest isolation level) and linearizability (immediate visibility of writes). Reads can be performed with eventual or strong consistency:
// Eventual consistency read
await db.get(["my-key"], { consistency: "eventual" });
// Strong consistency read
await db.get(["my-key"], { consistency: "strong" });Example of a globally consistent counter using Deno Deploy:
import { serve } from "/[email protected]/http/server.ts";
const db = await Deno.openKv();
serve(async (req) => {
await db.atomic().sum(["views"], 1n).commit();
const res = await db.get(["views"]);
const views = res.value.value;
return new Response(`Views: ${views}`);
});To try Deno KV, developers must join the waitlist at https://deno.com/kv.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
