Files
triggerdotdev--trigger.dev/docs/idempotency.mdx
T
James Ritchie 6270abaab8 Remove v2 docs pages (#1246)
* Make the tooltip text color grey so it’s readable again the primary color

* Setup and styling for the Guides section

* Better Guides icon

* Improved the prerequisites

* WIP Nextjs guide and new folder structures

* Copy updates

* Useful next steps is now a snippet

* Better icons for the next steps section

* Renamed the “prerequisites” snippet

* Node.js guide

* Added a “Creating a project” guide

* New snippet for prerequisites

* New Remix guide

* Added Remix to the side bar

* Tweaked icons for creating a project page

* Added the hello world step to the onboarding steps

* Better “useful next steps” snippet card links

* Moved prerequisites

* Fixing links to images

* Moved v2 migration page to guides

* Removed dead link

* Attempt fix for redirect

* Fixed redirects

* Getting started section includes link to roadmap

* Removed icon from side menu

* Deleted all v2 pages (excluding updating mint.json)

* Removed v2 pages, redirects and versions from mint.json

* Deleted v2 snippets

* Deleted un-used pages

* Set of more useful coming soon snippets

* All snippets use the updated format

* updated folder “v3/“ with “/pages”

* Moved all main docs files to the route and updated the redirect

* Fixed URLs in the mdx pages to the new route path

* URL goes to the proper pricing page

* Better delayed runs image

* Attempt fix for self hosting page not redirecting
2024-08-07 11:13:45 +01:00

118 lines
4.5 KiB
Plaintext

---
title: "Idempotency"
description: "An API call or operation is “idempotent” if it has the same result when called more than once."
---
We currently support idempotency at the task level, meaning that if you trigger a task with the same `idempotencyKey` twice, the second request will not create a new task run.
## `idempotencyKey` option
You can provide an `idempotencyKey` to ensure that a task is only triggered once with the same key. This is useful if you are triggering a task within another task that might be retried:
```typescript
import { idempotencyKeys, task } from "@trigger.dev/sdk/v3";
export const myTask = task({
id: "my-task",
retry: {
maxAttempts: 4,
},
run: async (payload: any) => {
// By default, idempotency keys generated are unique to the run, to prevent retries from duplicating child tasks
const idempotencyKey = await idempotencyKeys.create("my-task-key");
// childTask will only be triggered once with the same idempotency key
await childTask.triggerAndWait(payload, { idempotencyKey });
// Do something else, that may throw an error and cause the task to be retried
},
});
```
You can use the `idempotencyKeys.create` SDK function to create an idempotency key before passing it to the `options` object.
We automatically inject the run ID when generating the idempotency key when running inside a task by default. You can turn it off by passing the `scope` option to `idempotencyKeys.create`:
```typescript
import { idempotencyKeys, task } from "@trigger.dev/sdk/v3";
export const myTask = task({
id: "my-task",
retry: {
maxAttempts: 4,
},
run: async (payload: any) => {
// This idempotency key will be the same for all runs of this task
const idempotencyKey = await idempotencyKeys.create("my-task-key", { scope: "global" });
// childTask will only be triggered once with the same idempotency key
await childTask.triggerAndWait(payload, { idempotencyKey });
// This is the same as the above
await childTask.triggerAndWait(payload, { idempotencyKey: "my-task-key" });
},
});
```
If you are triggering a task from your backend code, you can use the `idempotencyKeys.create` SDK function to create an idempotency key.
```typescript
import { idempotencyKeys, tasks } from "@trigger.dev/sdk/v3";
// You can also pass an array of strings to create a idempotency key
const idempotencyKey = await idempotenceKeys.create([myUser.id, "my-task"]);
await tasks.trigger("my-task", { some: "data" }, { idempotencyKey });
```
You can also pass a string to the `idempotencyKey` option, without first creating it with `idempotencyKeys.create`.
```typescript
import { myTask } from "./trigger/myTasks";
// You can also pass an array of strings to create a idempotency key
await myTask.trigger({ some: "data" }, { idempotencyKey: myUser.id });
```
<Note>Make sure you provide sufficiently unique keys to avoid collisions.</Note>
You can pass the `idempotencyKey` when calling `batchTrigger` as well:
```typescript
import { tasks } from "@trigger.dev/sdk/v3";
await tasks.batchTrigger("my-task", [
{
payload: { some: "data" },
options: { idempotencyKey: await idempotenceKeys.create(myUser.id) },
},
]);
```
## Payload-based idempotency
We don't currently support payload-based idempotency, but you can implement it yourself by hashing the payload and using the hash as the idempotency key.
```typescript
import { idempotencyKeys, task } from "@trigger.dev/sdk/v3";
import { createHash } from "node:crypto";
// Somewhere in your code
const idempotencyKey = await idempotencyKeys.create(hash(childPayload));
// childTask will only be triggered once with the same idempotency key
await tasks.trigger("child-task", { some: "payload" }, { idempotencyKey });
// Create a hash of the payload using Node.js crypto
// Ideally, you'd do a stable serialization of the payload before hashing, to ensure the same payload always results in the same hash
function hash(payload: any): string {
const hash = createHash("sha256");
hash.update(JSON.stringify(payload));
return hash.digest("hex");
}
```
## Important notes
Idempotency keys, even the ones scoped globally, are actually scoped to the task and the environment. This means that you cannot collide with keys from other environments (e.g. dev will never collide with prod), or to other projects and orgs.
If you use the same idempotency key for triggering different tasks, the tasks will not be idempotent, and both tasks will be triggered. There's currently no way to make multiple tasks idempotent with the same key.