Storage von Cloudflare
By default, the runtime retries a call up to four times, and starts no retry more than 10 seconds after the initial call starts. It does not retry errors thrown by your Durable Object code, or errors with .overloaded set to true.
If the connection drops and the runtime cannot tell whether the Durable Object received the call, it still retries, but the Durable Object rejects the retry if the first attempt arrived. A method never runs twice because of a retry, unless you mark it with retryable() from cloudflare:durable-objects:
src/index.jsjs
import { DurableObject } from "cloudflare:workers";
import { retryable } from "cloudflare:durable-objects";
export class Cart extends DurableObject {
static {
retryable(this.prototype.setQuantity);
}
// Idempotent: a second run sets the same quantity.
async setQuantity(itemId, quantity) {
await this.ctx.storage.put(itemId, quantity);
}
// Not retryable: a second run would add another item.
async addOne(itemId) {
const quantity = (await this.ctx.storage.get(itemId)) ?? 0;
await this.ctx.storage.put(itemId, quantity + 1);
}
}
src/index.tsts
import { DurableObject } from "cloudflare:workers";
import { retryable } from "cloudflare:durable-objects";
export class Cart extends DurableObject {
static {
retryable(this.prototype.setQuantity);
}
// Idempotent: a second run sets the same quantity.
async setQuantity(itemId: string, quantity: number): Promise<void> {
await this.ctx.storage.put(itemId, quantity);
}
// Not retryable: a second run would add another item.
async addOne(itemId: string): Promise<void> {
const quantity = (await this.ctx.storage.get<number>(itemId)) ?? 0; …