{
    "url": "https://delaparra-services.nl/skills/concurrency-pitfalls",
    "locale": "en",
    "title": "Concurrency pitfalls | DeLaParra Services",
    "description": "Use when code runs things at the same time - threads, async/await, queues and workers, parallel requests, cron jobs, webhooks that retry - or when a bug only happens \"sometimes\" or under load. Covers race conditions, deadlocks, mutexes/locks, atomic operations, idempotency, message queues and backpressure, with Laravel and database examples.",
    "alternates": {
        "en": "https://delaparra-services.nl/skills/concurrency-pitfalls",
        "nl": "https://delaparra-services.nl/nl/skills/concurrency-pitfalls"
    },
    "formats": {
        "csv": "https://delaparra-services.nl/skills/concurrency-pitfalls.csv",
        "md": "https://delaparra-services.nl/skills/concurrency-pitfalls.md",
        "json": "https://delaparra-services.nl/skills/concurrency-pitfalls.json",
        "zip": "https://delaparra-services.nl/skills/concurrency-pitfalls.zip",
        "pdf": "https://delaparra-services.nl/skills/concurrency-pitfalls.pdf"
    },
    "downloads": [
        {
            "url": "https://delaparra-services.nl/skills/concurrency-pitfalls.csv",
            "encodingFormat": "text/csv",
            "name": "concurrency-pitfalls.csv"
        },
        {
            "url": "https://delaparra-services.nl/skills/concurrency-pitfalls.json",
            "encodingFormat": "application/json",
            "name": "concurrency-pitfalls.json"
        },
        {
            "url": "https://delaparra-services.nl/skills/concurrency-pitfalls.md",
            "encodingFormat": "text/markdown",
            "name": "concurrency-pitfalls.md"
        },
        {
            "url": "https://delaparra-services.nl/skills/concurrency-pitfalls.pdf",
            "encodingFormat": "application/pdf",
            "name": "concurrency-pitfalls.pdf"
        },
        {
            "url": "https://delaparra-services.nl/skills/concurrency-pitfalls.zip",
            "encodingFormat": "application/zip",
            "name": "concurrency-pitfalls.zip"
        },
        {
            "url": "https://delaparra-services.nl/skills/concurrency-pitfalls/download",
            "encodingFormat": "application/zip",
            "name": "concurrency-pitfalls-files.zip"
        }
    ],
    "markdown": "# Concurrency pitfalls\n\n \n\nUse when code runs things at the same time - threads, async/await, queues and workers, parallel requests, cron jobs, webhooks that retry - or when a bug only happens \"sometimes\" or under load. Covers race conditions, deadlocks, mutexes/locks, atomic operations, idempotency, message queues and backpressure, with Laravel and database examples.\n\n \n\n**Licence** MIT\n\n \n\n**Source** delaparra-original\n\n [Download zip](<https://delaparra-services.nl/skills/concurrency-pitfalls/download>)   \n\n# Concurrency pitfalls\n\n \n\nIf two things can happen at the same time, assume they will. Find the shared state, decide how access to it is coordinated, and prove it with a test or a reproduction under load.\n\n \n\n## Workflow\n\n \n\n1. **List the shared state:** database rows, cache keys, files, in-memory globals, external API resources. \n1. **List the concurrent actors:** web requests, queue workers, scheduled jobs, webhooks (providers retry!), multiple browser tabs, multiple server instances. \n1. **For each piece of shared state, pick one strategy** from the table. \n1. **Reproduce:** fire parallel requests (` xargs -P 10 curl … `, a load tool, or a test that dispatches jobs twice) before and after the fix.\n\n \n\n## Strategies\n\n    Problem Strategy Laravel / SQL example     Lost update (read-modify-write) Atomic operation ` Product::whereKey($id)->decrement('stock') `; SQL ` UPDATE … SET stock = stock - 1 WHERE stock > 0 ` and check affected rows   Two processes doing the same job Lock / mutex ` Cache::lock('order:'.$id, 10)->block(5, fn () => …) `; ` ShouldBeUnique ` jobs; ` ->withoutOverlapping() ` on scheduled tasks   Read then write in one transaction Row lock ` DB::transaction(fn () => Order::lockForUpdate()->find($id) …) `   Duplicate inserts Unique constraint Unique index + ` upsert() ` / ` firstOrCreate() ` inside a transaction, catch the duplicate-key exception   Retried webhooks / requests Idempotency Store the event or idempotency key with a unique index; skip if already processed   Deadlocks Consistent lock order, short transactions, retry ` DB::transaction(fn () => …, attempts: 3) `; lock rows in the same order everywhere   Producer faster than consumer Backpressure Bounded queues, rate limits (` RateLimiter `, ` Redis::throttle `), the ` RateLimited ` job middleware   Shared global / static state Avoid; pass data explicitly No mutable statics in long-running workers (Octane, queue workers)    \n\n## Async code (JavaScript / Python)\n\n \n\n- ` await ` inside a loop runs sequentially; ` Promise.all ` / ` asyncio.gather ` runs concurrently, so limit concurrency for external APIs. \n- A check-then-act across an ` await ` is a race: the state can change during the await. \n- Always handle rejections; one failed promise in ` Promise.all ` rejects the whole batch (use ` Promise.allSettled ` when partial success is fine).\n\n \n\n## Rules\n\n \n\n- Never \"fix\" a race with ` sleep() ` or a retry loop alone. \n- A fix needs a reproduction that failed before and passes after. \n- Explain in the summary which actor/state pair caused the bug.\n\n \n\n## Files\n\n \n\n- ` LICENSE ` \n- ` SKILL.md ` \n- ` SOURCE.md `\n\n \n\n## Related terms\n\n \n\n- [ACID](<https://delaparra-services.nl/knowledge-hub/acid>) \n- [Actor](<https://delaparra-services.nl/knowledge-hub/actor>) \n- [Actor Model](<https://delaparra-services.nl/knowledge-hub/actor-model>) \n- [Asynchronous](<https://delaparra-services.nl/knowledge-hub/asynchronous>) \n- [Atomic Operation](<https://delaparra-services.nl/knowledge-hub/atomic-operation>) \n- [Backpressure](<https://delaparra-services.nl/knowledge-hub/backpressure>) \n- [BEGIN / COMMIT / ROLLBACK](<https://delaparra-services.nl/knowledge-hub/begin-commit-rollback>) \n- [Concurrent Programming](<https://delaparra-services.nl/knowledge-hub/concurrent-programming>) \n- [Data Parallelism](<https://delaparra-services.nl/knowledge-hub/data-parallelism>) \n- [Deadlock](<https://delaparra-services.nl/knowledge-hub/deadlock>) \n- [DeadlockError](<https://delaparra-services.nl/knowledge-hub/deadlockerror>) \n- [dirty read](<https://delaparra-services.nl/knowledge-hub/dirty-read>) \n- [Event Loop](<https://delaparra-services.nl/knowledge-hub/event-loop>) \n- [Mailbox](<https://delaparra-services.nl/knowledge-hub/mailbox>) \n- [Message Passing](<https://delaparra-services.nl/knowledge-hub/message-passing>) \n- [Message Queue](<https://delaparra-services.nl/knowledge-hub/message-queue>) \n- [Mutex (Mutual Exclusion)](<https://delaparra-services.nl/knowledge-hub/mutex-mutual-exclusion>) \n- [Mutex / Lock](<https://delaparra-services.nl/knowledge-hub/mutex-lock>) \n- [Parallel Programming](<https://delaparra-services.nl/knowledge-hub/parallel-programming>) \n- [Payload](<https://delaparra-services.nl/knowledge-hub/payload>) \n- [phantom read](<https://delaparra-services.nl/knowledge-hub/phantom-read>) \n- [Queue](<https://delaparra-services.nl/knowledge-hub/queue>) \n- [Race condition](<https://delaparra-services.nl/knowledge-hub/race-condition>) \n- [Race Condition](<https://delaparra-services.nl/knowledge-hub/race-condition-paradigms>) \n- [READ COMMITTED](<https://delaparra-services.nl/knowledge-hub/read-committed>) \n- [READ UNCOMMITTED](<https://delaparra-services.nl/knowledge-hub/read-uncommitted>) \n- [REPEATABLE READ](<https://delaparra-services.nl/knowledge-hub/repeatable-read>) \n- [SERIALIZABLE](<https://delaparra-services.nl/knowledge-hub/serializable>) \n- [Supervision](<https://delaparra-services.nl/knowledge-hub/supervision>) \n- [Task Parallelism](<https://delaparra-services.nl/knowledge-hub/task-parallelism>) \n- [Thread](<https://delaparra-services.nl/knowledge-hub/thread>) \n- [TimeoutError](<https://delaparra-services.nl/knowledge-hub/timeouterror>) \n- [Webhook](<https://delaparra-services.nl/knowledge-hub/webhook>)\n\n \n\n[Installing an agent skill in Claude Code, Codex, Cursor and Copilot](<https://delaparra-services.nl/manuals/een-skill-installeren-in-je-ai-assistent>)\n\n[HTML version](https://delaparra-services.nl/skills/concurrency-pitfalls)\n",
    "structured_data": {
        "@context": "https://schema.org",
        "@graph": [
            {
                "@type": "Organization",
                "@id": "https://delaparra-services.nl/#practice",
                "logo": "https://delaparra-services.nl/logo.png",
                "name": "DeLaParra Services",
                "legalName": "DeLaParra Services",
                "url": "https://delaparra-services.nl/",
                "description": "Bespoke software, web applications and mobile apps, developed in Laravel by a single developer in Groningen. Fixed price agreed in advance, full source code provided upon delivery.",
                "email": "info@delaparra-services.nl",
                "address": {
                    "@type": "PostalAddress",
                    "addressLocality": "Groningen",
                    "addressRegion": "Groningen",
                    "addressCountry": "NL"
                },
                "areaServed": "Groningen, the Netherlands",
                "knowsLanguage": [
                    "en",
                    "nl"
                ],
                "knowsAbout": [
                    "Web design and development",
                    "App development",
                    "Custom software and web applications",
                    "OpenSpec",
                    "AI code review",
                    "Your own coding agent"
                ],
                "founder": {
                    "@id": "https://delaparra-services.nl/#person"
                }
            },
            {
                "@type": "WebPage",
                "@id": "https://delaparra-services.nl/skills/concurrency-pitfalls#webpage",
                "url": "https://delaparra-services.nl/skills/concurrency-pitfalls",
                "name": "Concurrency pitfalls | DeLaParra Services",
                "description": "Use when code runs things at the same time - threads, async/await, queues and workers, parallel requests, cron jobs, webhooks that retry - or when a bug only happens \"sometimes\" or under load. Covers race conditions, deadlocks, mutexes/locks, atomic operations, idempotency, message queues and backpressure, with Laravel and database examples.",
                "isPartOf": {
                    "@id": "https://delaparra-services.nl/#website"
                },
                "about": {
                    "@id": "https://delaparra-services.nl/#practice"
                },
                "inLanguage": "en",
                "breadcrumb": {
                    "@id": "https://delaparra-services.nl/skills/concurrency-pitfalls#breadcrumb"
                }
            },
            {
                "@type": "BreadcrumbList",
                "@id": "https://delaparra-services.nl/skills/concurrency-pitfalls#breadcrumb",
                "itemListElement": [
                    {
                        "@type": "ListItem",
                        "position": 1,
                        "name": "Home",
                        "item": "https://delaparra-services.nl/"
                    },
                    {
                        "@type": "ListItem",
                        "position": 2,
                        "name": "Concurrency pitfalls"
                    }
                ]
            },
            {
                "@type": "Person",
                "@id": "https://delaparra-services.nl/#person",
                "name": "Reinier de la Parra",
                "url": "https://delaparra-services.nl/about"
            },
            {
                "@type": "SoftwareSourceCode",
                "@id": "https://delaparra-services.nl/skills/concurrency-pitfalls#skill",
                "url": "https://delaparra-services.nl/skills/concurrency-pitfalls",
                "name": "Concurrency pitfalls",
                "description": "Use when code runs things at the same time - threads, async/await, queues and workers, parallel requests, cron jobs, webhooks that retry - or when a bug only happens \"sometimes\" or under load. Covers race conditions, deadlocks, mutexes/locks, atomic operations, idempotency, message queues and backpressure, with Laravel and database examples.",
                "inLanguage": "en",
                "license": "https://spdx.org/licenses/MIT.html",
                "subjectOf": {
                    "@id": "https://delaparra-services.nl/skills/concurrency-pitfalls#dataset"
                }
            },
            {
                "@type": "Dataset",
                "@id": "https://delaparra-services.nl/skills/concurrency-pitfalls#dataset",
                "name": "Concurrency pitfalls",
                "description": "Use when code runs things at the same time - threads, async/await, queues and workers, parallel requests, cron jobs, webhooks that retry - or when a bug only happens \"sometimes\" or under load. Covers race conditions, deadlocks, mutexes/locks, atomic operations, idempotency, message queues and backpressure, with Laravel and database examples.",
                "url": "https://delaparra-services.nl/skills/concurrency-pitfalls",
                "identifier": "https://delaparra-services.nl/skills/concurrency-pitfalls#dataset",
                "about": {
                    "@id": "https://delaparra-services.nl/skills/concurrency-pitfalls#skill"
                },
                "creditText": "delaparra-original",
                "publisher": {
                    "@id": "https://delaparra-services.nl/#practice"
                },
                "license": "https://spdx.org/licenses/MIT.html",
                "isAccessibleForFree": true,
                "inLanguage": "en",
                "dateModified": "2026-11-28T14:00:11+00:00",
                "isPartOf": {
                    "@type": "Dataset",
                    "@id": "https://delaparra-services.nl/knowledge-hub#skills",
                    "name": "Skills for AI coding assistants",
                    "url": "https://delaparra-services.nl/knowledge-hub"
                },
                "includedInDataCatalog": {
                    "@type": "DataCatalog",
                    "@id": "https://delaparra-services.nl/knowledge-hub#catalog",
                    "name": "Knowledge hub data catalog",
                    "url": "https://delaparra-services.nl/knowledge-hub"
                },
                "distribution": [
                    {
                        "@type": "DataDownload",
                        "name": "concurrency-pitfalls.csv",
                        "encodingFormat": "text/csv",
                        "contentUrl": "https://delaparra-services.nl/skills/concurrency-pitfalls.csv",
                        "inLanguage": "en",
                        "dateModified": "2026-11-28T14:00:11+00:00",
                        "license": "https://spdx.org/licenses/MIT.html"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "concurrency-pitfalls.json",
                        "encodingFormat": "application/json",
                        "contentUrl": "https://delaparra-services.nl/skills/concurrency-pitfalls.json",
                        "inLanguage": "en",
                        "dateModified": "2026-11-28T14:00:11+00:00",
                        "license": "https://spdx.org/licenses/MIT.html"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "concurrency-pitfalls.md",
                        "encodingFormat": "text/markdown",
                        "contentUrl": "https://delaparra-services.nl/skills/concurrency-pitfalls.md",
                        "inLanguage": "en",
                        "dateModified": "2026-11-28T14:00:11+00:00",
                        "license": "https://spdx.org/licenses/MIT.html"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "concurrency-pitfalls.pdf",
                        "encodingFormat": "application/pdf",
                        "contentUrl": "https://delaparra-services.nl/skills/concurrency-pitfalls.pdf",
                        "inLanguage": "en",
                        "dateModified": "2026-11-28T14:00:11+00:00",
                        "license": "https://spdx.org/licenses/MIT.html"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "concurrency-pitfalls.zip",
                        "encodingFormat": "application/zip",
                        "contentUrl": "https://delaparra-services.nl/skills/concurrency-pitfalls.zip",
                        "inLanguage": "en",
                        "dateModified": "2026-11-28T14:00:11+00:00",
                        "license": "https://spdx.org/licenses/MIT.html"
                    },
                    {
                        "@type": "DataDownload",
                        "name": "concurrency-pitfalls-files.zip",
                        "encodingFormat": "application/zip",
                        "contentUrl": "https://delaparra-services.nl/skills/concurrency-pitfalls/download",
                        "inLanguage": "en",
                        "dateModified": "2026-11-28T14:00:11+00:00",
                        "license": "https://spdx.org/licenses/MIT.html"
                    }
                ]
            }
        ]
    }
}