The free plan of Deno Deploy gives me 450,000 read units and 300,000 write units per month. One read unit is equivalent to 4 kB of read. If you read a 2 kB record this is 1 read unit. If you read 4.1 kB record then it’s 2 read units. If you use the list
function and read 10 records and their total size is 3 kB, then this is 1 read unit in total.
As I’m still building the app I think most of my activity should be set
, not get
. Yet I have used all the read quota for this month, while only used 479 write units. I think something is wrong here.
I try to track the used units via signals:
// signals.ts
import { signal } from "@preact/signals";
import sizeof from "npm:object-sizeof";
export const kvSignal = signal<Deno.Kv>(await Deno.openKv());
export const readUnitSignal = signal<number>(0);
export const writeUnitSignal = signal<number>(0);
export function increaseReadUnit(data: any) {
const kb = sizeof(data) / 1000;
readUnitSignal.value += Math.ceil(kb / 4);
}
export function increaseWriteUnit(data: any) {
const kb = sizeof(data) / 1000;
writeUnitSignal.value += Math.ceil(kb);
}
Replace the native kv.get
and kv.set
operations with these:
// kvUtils.ts
export async function kvGet(key: Deno.KvKey) {
const kv = kvSignal.value;
const result = await kv.get(key);
increaseReadUnit(result);
return result;
}
export async function kvSet(key: Deno.KvKey, value: any) {
const kv = kvSignal.value;
await kv.set(key, value);
increaseWriteUnit(value);
}
And run:
// main.ts
const env = await load();
Deno.env.set("DENO_KV_ACCESS_TOKEN", env["DENO_KV_ACCESS_TOKEN"]);
kvSignal.value = await Deno.openKv(env["API"]);
await runProgram();
console.log("Read unit used", readUnitSignal.value);
console.log("Write unit used", writeUnitSignal.value);
The account is new so the history is clean. Here is the result:
My method | Deno Deploy’s analytics | |
---|---|---|
Read units | 7506 | 1.53 K |
Write units | 7006 | 0 |
My questions are:
- Is my method correct?
- How come there is a huge gap between the real numbers of read and write units? How come the real write unit is literally zero?