Locking shared resources with Temporal: when concurrent workflows need to take turns
Several workflows may need to change the same account, device or record, and an interleaved update can leave that record in a state no single workflow intended. Temporal publishes a guide to locking shared resources so that workflows needing the same resource take turns. The core idea is to route every change to one resource through a single owner that accepts one operation at a time, so concurrent workflows queue behind each other instead of colliding. The guide is the natural starting point for the pattern, and the design decisions that follow are yours to make against your own resource model.
The pattern fits when the cost of two writes overlapping is higher than the cost of waiting. Typical cases include a payment and a balance adjustment on the same account, two provisioning jobs for one device, or a batch update and a customer edit to the same record. Before you adopt it, list the resources that need exclusive access, identify which workflows touch each one, and decide what a waiting workflow should do: wait indefinitely, time out, or report a conflict to its caller. Those answers shape the lock's lifetime, its timeout settings and the user-facing behavior when a lock is held.
Confirm the exact mechanics against the guide before you design, including how the lock is acquired and released, how a crashed holder is handled, and what happens to queued requests. Test those failure paths in a non-production namespace, since a lock that is never released can stall every workflow waiting on that resource. Also plan for the operational cost: a single owner per resource concentrates load, so ask how many resources will be locked at once and how contention will be monitored. Document the decision so security and compliance reviewers can see which records are protected from concurrent change and how that protection is verified.
Lock design options and when each fits
This table compares three ways to build a lock in Temporal: a dedicated lock workflow per resource, a lock held inside an entity workflow, and a lock built on activities with external state. Read each column as one design and each row as a decision point. The main takeaway is that the choice depends on who already owns the resource's state and how waiters should be notified, so confirm the exact signal and update patterns against Temporal's guide before you build.
| Decision point | Dedicated lock workflow | Lock inside entity workflow | Lock via activities and external state |
|---|---|---|---|
| Who owns the lock | A separate workflow per resource, keyed by the resource ID, that exists only to grant and release the lock | The workflow that already holds the resource's state, so the lock is part of that entity's own state | An external store, such as a database row, that records the holder, with activities reading and writing it |
| How waiters are signaled | Waiters send an acquire request to the lock workflow and wait until it signals that access is granted | Waiters send requests to the entity workflow, which queues them and answers when the lock is free | Waiters retry or poll the external record through activity retries, so no push notification is involved |
| Main trade-off | Clear ownership and a single point of coordination, but an extra workflow to run, monitor and clean up | No extra workflow, but the lock is tied to the entity's lifecycle and to its history growth | Uses infrastructure you already run, but Temporal does not hold lock state, so crash and timeout handling falls to your code |
| When it fits | A resource needs strict serialization across many unrelated callers and the lock must be visible on its own | Changes to the resource already flow through one workflow, so serializing them there costs little extra | Waits are short, the external store already provides atomic compare-and-set, and polling load is acceptable |
| What to confirm | How the lock workflow ID is derived, how release is triggered if a holder fails, and how stale locks are detected | Whether the entity's history will stay bounded, and how queued requests are handled during Continue-As-New | How lease expiry is enforced, whether the store's writes are atomic, and how activity retries interact with the lock release |
Setting up a lock workflow on Temporal
This sequence turns a shared resource key into a single Temporal workflow that grants one holder at a time, with signals for acquire and release, timeouts for stuck holders, and tests that prove serialization under concurrent callers.
Define the resource key and lock scope
Your team decides what a lock protects, such as one customer account, one device serial or one ledger, and maps that resource to a single workflow ID. The workflow ID should be derived deterministically from the resource key so every caller addresses the same lock. Write down the key format, who owns it and which operations need the lock. The step is done when a reviewer can name, for any request, the exact workflow ID it will use and confirm that two requests for the same resource always produce the same ID.
Create the lock workflow and its signals
Implement the lock workflow with a state that records the current holder, a queue of waiting requests and handlers for an acquire signal and a release signal. Each acquire signal carries a request ID and the caller identity so the holder can be identified later. The workflow must keep its state in workflow variables only, not in activities or external stores, so Temporal can replay it. Confirm the design by running the workflow in a development environment and sending a test acquire and release, then checking the recorded history shows the signals in the order they were received.
Grant and release the lock through signals
Wire the calling services so that each caller sends an acquire signal to the lock workflow, waits until the workflow grants the lock, performs its protected work and then sends a release signal with the same request ID. The workflow grants the lock to the next waiting request only after the current holder releases. Callers should start the lock workflow with Signal-With-Start where the lock may not exist yet, so the start and first signal happen as one operation. The step is verified when a single holder's release lets exactly one waiting request proceed, and the history shows no grant before the prior release.
Add timeouts for stuck holders
A caller that crashes between acquire and release would otherwise block the resource forever. Add a lease duration to each grant, set by your team, after which the workflow reclaims the lock and grants it to the next waiter. Choose the lease length from how long the protected work normally takes, and record the value in your design document rather than copying a default. Confirm the behavior by forcing a holder to skip its release in a test environment and checking that the lease expires and the next waiter receives the grant.
Handle late releases and duplicate signals
After a lease expires, the original holder may still send a release, and a retried network call may deliver the same acquire twice. The workflow should compare the request ID on each release with the current holder and ignore releases from holders that no longer own the lock. Duplicate acquire signals from the same request ID should not enqueue the caller twice. The step is complete when tests show that a stale release does not free a lock now held by someone else and that a duplicated acquire yields one grant.
Test concurrent callers against the lock
Start many callers at the same time against one resource key and record every moment a caller holds the lock. Your test harness should assert that at most one holder exists at any point, that every waiter eventually receives a grant and that the workflow completes or continues as designed. Include runs where workers restart mid-hold and where signals arrive out of step with activities. The step is done when the concurrent test passes repeatedly and the team can show the recorded holder timeline for a run.
Plan the lock's long-running history
A lock workflow that serves a busy resource accumulates history with every acquire and release. Decide how the workflow will bound its history, for example by carrying the holder and waiting queue into a new run before limits are reached, and confirm the approach with the Temporal documentation for your SDK. Record the expected signal volume per resource so operators can see when a lock is hot. The step is verified when the team has a tested rollover path and a documented signal volume estimate for the most active resources.
How concurrent workflows queue behind a lock
The diagram starts with two business workflows, each asking to use the same account or device. Both send their request to a lock workflow, which grants the resource to the first requester and holds the second in a queue. When the first workflow releases the lock, the lock workflow hands it to the next waiter. Check that every path back to the lock includes a release, including failure paths, because a missed release leaves every later waiter stuck.
Two workflows take turns on one shared resource through a lock workflow.
Security and operational controls for a self-hosted lock
A lock built on Temporal is only as trustworthy as the service that holds it. The controls below decide who can send lock signals, who can read workflow data, and whether traffic between services can be intercepted. Review them together before any lock workflow carries production resources.
Authorizer before first use
If no Authorizer is configured, self-hosted Temporal uses the default noopAuthorizer, which allows every API request with no authentication or access control. Any client that can reach the frontend could start, signal or terminate a lock workflow and free a resource that another process still holds. Confirm that an Authorizer is configured in every environment, including development clones that later get promoted.
Temporal security docsMutual TLS on both traffic paths
Temporal supports mTLS with separate settings for internode traffic between services and frontend traffic from SDK clients. Lock correctness depends on workers reaching the right namespace, so a spoofed client or service node is a real threat. Configure both settings, issue certificates from your internal authority, and plan rotation before certificates expire.
ClaimMapper for namespace roles
A ClaimMapper maps JWT or certificate claims to namespace roles, so the team that runs the lock workflows gets write access to its own namespace and nothing else. Map roles from your identity provider's groups rather than from individual user accounts. Test one denied call per role during rollout, such as a reader attempting to signal a lock.
OIDC sign-in for the Web UI
The Temporal Web UI can use OIDC single sign-on, which lets operators inspect and terminate lock workflows under their own identities instead of a shared login. Confirm that the Web UI is reachable only from your internal network and that session lifetimes match your access policy. Audit who terminated a stuck lock, since manual termination is the usual way a lock is released early.
Codec server for payload encryption
Lock workflows often carry account or device identifiers in their inputs and signal payloads. A custom codec encrypts payloads before they are stored, and a codec server lets authorized users decrypt them in the Web UI. Decide key ownership and rotation before go-live, because losing keys makes stored histories unreadable.
Network placement of services
The Temporal deployment docs say services should run on hosts not reachable from the public internet, with access limited to trusted internal networks. Place the frontend, internode ports and persistence behind private addressing, and confirm that worker networks can reach the frontend without a public route. Verify exposure with an external scan from outside your network.
Deployment docsQuestions teams ask before building a lock on Temporal
What drives the cost of building a lock on Temporal?
The cost depends mostly on how many resource types need locking, how many workflows contend for each one, and how much of the surrounding system must change. A lock workflow itself is small, so the larger effort is usually in mapping every code path that touches the shared resource, deciding how lock holders release on failure, and wiring the lock into existing services. Cost has two parts: a one-time build and test effort, and recurring operation of the Temporal Service and workers, whether self-hosted or on Temporal Cloud. Temporal Cloud pricing is published on the Temporal pricing page, where usage is billed per million actions and storage per GB-hour. DeployOpen does not publish prices. Ask any quote to itemize scope, the number of resource types, the hosting model and the test plan.
How long does it take to build and test a Temporal lock?
Duration depends on the number of resources to lock, the existing deployment, and how many failure cases the team wants tested. A single lock on a well-understood resource with a Temporal Service already running is a much smaller job than locking many resource types across several services. Testing usually takes as long as building, because the lock must be checked under concurrency, worker restarts, timeouts and workflows that never release. Ask for a written timeline that separates design, build, failure testing and handover, and confirm which dependencies, such as a running Temporal Service or access to a test namespace, must be ready before the clock starts.
What do our engineers need to provide for the build?
Your engineers provide the knowledge of the resource and the systems that touch it. Expect to supply:
- A list of every service, job or workflow that reads or changes the shared resource
- The identifiers that define the resource, such as account or device IDs, and how they are generated
- Access to a non-production Temporal namespace and the Temporal Service that will host the lock
- A named owner who can approve decisions about timeouts, release behavior and failure handling
Without these, the build team has to discover the access paths by reading code, which lengthens the work.
Can our team build a Temporal lock without outside help?
A team with Temporal experience can build a basic lock alone, especially for one resource with few callers. The harder parts are the ones teams underestimate: handling a lock holder that crashes, setting timeouts that match real work, keeping workflow code deterministic when it changes, and proving under concurrent load that two workflows never hold the same lock. Outside help is most useful when several resource types are involved, when the lock sits in a regulated system, or when the team has not run Temporal in production before. Teams that build alone should still write failure tests and document the release rules.
When is a lock the wrong tool for shared resource access?
A lock is the wrong tool when the work can be modeled as a queue, when callers can tolerate eventual consistency, or when the shared resource can be changed atomically at the database level. Serializing every update through a lock adds waiting time and a new failure mode, so teams should check whether a single entity workflow that receives signals, or an idempotent write with optimistic concurrency, solves the collision more simply. Temporal's guide on locking shared resources describes the pattern in its lock shared resources guide, which is a useful check before committing to a design.
What breaks most often in a Temporal lock?
The most common failures are a lock holder that never releases, a timeout shorter than the real work, and a release that fails after the protected work has already finished. Each produces either a stuck resource or two workflows that believe they hold the lock. Teams should test worker crashes during the locked section, server-side timeouts, and retries of the release step. Ask any implementation partner to show which of these cases the test suite covers and how a stuck lock is detected and cleared by an operator.
How do we keep a lock secure and running after go-live?
Security starts with the Temporal Service itself. Self-hosted Temporal with no Authorizer configured uses a default that allows every API request with no authentication or access control, so authorization, mTLS and network restriction must be configured before any production lock runs. Temporal's self-hosted security guide describes the available controls. After go-live, plan for Temporal Server upgrades, worker version changes and a runbook for stuck locks. Ask any quote to name who operates these tasks after handover and whether that work is included or offered as ongoing support.
What should a quote for this work include?
A quote should follow from a written scope that lists the resources to lock, the workflows that acquire and release each lock, the hosting model, the test cases for failure and concurrency, and the handover materials. Ask for deliverables in writing, the assumptions about your environment, and what is excluded, such as Temporal Cloud subscription costs or ongoing operations. DeployOpen agrees scope, deliverables and timelines in writing before work starts, and most engagements are fixed-scope. Ongoing support and continuous testing are available as retainers.
Operating and upgrading the lock after go-live
Once the lock is in production, the ongoing work is watching the workflows that wait on it, deciding what happens when a lock holder stops making progress, and keeping the lock workflow code and the Temporal Server it runs on current. Start by defining the signals you will monitor: the number of workflows queued behind each resource, how long the oldest waiter has been waiting, and how many lock holders have been open longer than the longest normal business operation. Those numbers show whether the lock is serving its purpose or whether a holder has stalled and is blocking everyone behind it. Confirm with your own team which thresholds trigger a page and which trigger a review, because the right values depend on how long your business operations normally take.
Stuck holders need a written procedure before the first incident. Decide who may release a lock manually, whether that means cancelling the holding workflow, sending a release signal, or resetting it, and what evidence must be recorded first. Check that the lock's timeouts and the holder's heartbeat or retry settings match what you expect, since a holder with no timeout can hold a resource indefinitely. Upgrades carry their own risk. Temporal Server should be upgraded one minor version at a time, moving to the latest patch of the current minor first, because backward compatibility is guaranteed only between two successive minor versions. Where the release notes call for a schema upgrade, run it before the server upgrade. Check the current release notes in the Temporal GitHub releases page and the upgrade guide before scheduling each step, and test the sequence in a non-production namespace first.
Workflow code that holds locks needs its own versioning discipline. A lock held by a running workflow keeps the code version it started with, so a change to the acquire, wait or release logic can alter behavior for holders that are already in flight. Use Temporal's versioning mechanisms, and confirm with your SDK documentation which approach fits your language, before deploying changes to lock logic. Keep old worker versions running until every workflow that started under them has finished or been migrated, and test replay of in-flight lock workflows against the new code. Record in your change process which resources each lock workflow guards, so that a change to one lock is reviewed for effects on every holder and waiter that depends on it.
How DeployOpen scopes and delivers a Temporal lock implementation
DeployOpen approaches locking shared resources with Temporal as a scoped implementation inside infrastructure you control. We start from the resources that must not be accessed concurrently, then design the lock workflow, the self-hosted or Temporal Cloud service it runs on, and the verification that proves concurrent workflows take turns.
How we scope
- Inventory which shared resources need serialized access, such as accounts or devices, and which workflows contend for each one.
- Agree in writing whether the lock runs on a self-hosted Temporal Service or Temporal Cloud, with the deployment and access decisions that follow.
- Define acceptance tests for contention, failure during a held lock and release, so the fixed scope has measurable completion criteria.
How we build
- Design the lock workflow, its identifiers and signals, and the workflow code that acquires and releases access to each resource.
- Configure workers, task queues and the Temporal Server security settings the lock depends on, including authorization and network exposure.
- Test concurrent start, duplicate requests, worker restarts and timeouts against the design before any production traffic uses the lock.
How we hand over
- Deliver the lock workflow code, configuration, deployment artifacts and runbooks written for your platform and operations teams.
- Document the locking rules, expected behavior on timeouts and the steps to inspect or release a stuck lock.
- Walk your engineers through the test results and the upgrade approach for Temporal Server and SDK versions.
How we support
- Ongoing support, upgrades and continuous testing are available as retainers after handover.
- Retesting the lock after Temporal Server upgrades, SDK changes or new resource types is included in the support scope when agreed.
- Ask for a written quote that lists each scoped deliverable, the environments covered and the support terms before work begins.

