Back to KB
Difficulty
Intermediate
Read Time
5 min

Per-account task concurrency without a lock service

By Codcompass TeamΒ·Β·5 min read

Current Situation Analysis

Background job systems frequently orchestrate calls to external APIs on behalf of distinct accounts, tenants, or installations. While these external systems typically permit parallel requests across different accounts, they strictly enforce serial execution for the same account identifier. This is not a global rate limit; it is a concurrency-by-key constraint where the key represents a shared quota, state boundary, or session lock.

Without explicit per-key serialization, worker pools inevitably dispatch concurrent invocations for the same account_id. This triggers predictable failure modes:

  • External Throttling/Rejection: The provider returns 429 Too Many Requests, rejects concurrent writes, or returns inconsistent partial updates.
  • State Corruption: Overlapping mutations leave downstream systems in unreconciled states, requiring complex compensation logic.
  • Operational Overhead: Traditional mitigations (distributed locks like Redis/etcd, per-account queues, or manual retry/backoff loops) introduce external coordination layers, increase latency, create single points of failure, and complicate observability.

These approaches fail because they decouple concurrency control from the job orchestrator, forcing developers to manage race conditions, lock contention, and queue routing manually.

WOW Moment: Key Findings

By leveraging in-process concurrency tracking instead of external locks, the orchestrator can enforce strict per-key serialization while maximizing parallelism across different accounts. The following experimental comparison demonstrates the trade-offs between execution safety, latency, and throughput across four concurrency strategies.

ApproachCollisionsExecution TimeInvocations ProcessedQueue Churn / Overhead
Unsafe (Baseline)91.42s12/12High (uncontrolled parallel dispatch)
Keyed + Reroute02.14s12/12Medium (blocked tasks re-queued until slot frees)
Keyed + Drop00.67s3/12Low (9 tasks terminated immediately)
Registration + Keyed0~0.85s3/12 (collapsed)Minimal (duplicates deduplicated at enqueue time)

Key Findings:

  • running_concurrency=KEYS eliminates collisions entirely by indexing active invocations agains

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back