This sample shows how to have Sticky Execution: using a unique task queue per Worker to have certain activities only run on that specific Worker.
The strategy is:
- Create a
getUniqueTaskQueueactivity that generates a unique task queue name,uniqueWorkerTaskQueue. - It doesn't matter where this activity is run, so it can be "non sticky" as per Temporal default behavior.
- In this demo,
uniqueWorkerTaskQueueis simply auuidinitialized in the Worker, but you can inject smart logic here to uniquely identify the Worker, as Netflix did. - For activities intended to be "sticky", only register them in one Worker, and have that be the only Worker listening on that
uniqueWorkerTaskQueue. - Execute workflows from the Client like normal.
Activities have been artificially slowed with activity.Context().sleep(3000) to simulate slow activities.
temporal server start-devto start Temporal Server.npm installto install dependencies.npm run start.watchto start the Worker.- In another shell,
npm run workflowto run the Workflow.
Example output:
Downloading https://temporal.io and saving to /tmp/b15036de-dbc7-4bc9-b2c7-7c48635c5797
Did some work on /tmp/b15036de-dbc7-4bc9-b2c7-7c48635c5797, checksum: b3fc767460efa514753a75e6f3d7af97
Removing /tmp/b15036de-dbc7-4bc9-b2c7-7c48635c5797You can try to intentionally crash Workers while they are doing work to see what happens when work gets "stuck" in a unique queue: currently the Workflow will scheduleToCloseTimeout without a Worker, and retry when a Worker comes back online.
After the 5th attempt, it logs Final attempt 5 failed, giving up and exits. But you may wish to implement compensatory logic, including notifying you.

