Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Retry On Duplicate Key Laravel Package

mpyw/laravel-retry-on-duplicate-key

Automatically retries database operations when a duplicate key error occurs in Laravel. Wrap Eloquent firstOrCreate, updateOrCreate, or insert calls in a retryable transaction to safely handle race conditions and unique constraint conflicts under concurrent writes.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Build vs. buy for write-path reliability. This package exists to answer one narrow question: when a "create-if-missing" or upsert write loses a race and the database throws a unique-constraint violation, do we retry in the ORM layer, the repository layer, the job queue, or not at all? Evaluating it is really an exercise in deciding where retry logic belongs in your architecture.
  • Roadmap prioritization for concurrent-write features. It's relevant to any upcoming work where two requests can plausibly create the same row at the same instant: checkout/order creation, inventory decrement, wallet/ledger entries, quota and usage counters, webhook and payment-callback ingestion, "ensure default settings row exists" bootstrap paths, and dedupe-by-external-id flows.
  • Idempotency strategy. If the product roadmap includes payment callbacks, third-party webhooks, or at-least-once delivery from a queue, you need a documented answer for duplicate inserts. This package represents one (ORM-level retry) answer among several (idempotency-key table, upsert(), queue-level retry with backoff).
  • Technical-debt and dependency-risk decisions. With 12 stars, an archived repo, and a last release of March 2023, adopting it is a deliberate decision to own vendored code. That's a legitimate decision — but it should be recorded as such, not absorbed silently.
  • Incident-driven investment. If postmortems show SQLSTATE[23000]/duplicate-key 500s as a recurring cause of failed signups or failed checkouts, this is a candidate mitigation to size against alternatives.
  • Not a differentiator. Nothing here is customer-visible. It should never compete for roadmap space against features; it competes for engineering hygiene budget only.

When to Consider This Package

Consider it when:

  • You have a legacy or fast-growing codebase with non-atomic check-then-insert logic (firstOrCreate-style flows, SELECT then INSERT, or INSERT ... ON DUPLICATE KEY UPDATE under contention) spread across many repositories, and you want a low-touch, drop-in mitigation while a proper refactor is scheduled.
  • You are pinned to the Laravel/PHP versions the package was built against and have no near-term upgrade on the calendar. An archived dependency is only a liability when you need it to move with you.
  • You want a compact, MIT-licensed reference implementation of the retry-on-unique-violation pattern to copy into your own codebase rather than install.
  • Write volume is modest, the operation is already inside a transaction, and you want belt-and-braces rather than a structural fix.
  • You are prototyping and explicitly deferring production hardening.

Look elsewhere, or build it yourself, when:

  • You are on or moving to a newer Laravel major than the last release supports. An archived repo gives you no compatibility guarantee and no security patching.
  • You need observability. This style of transparent retry hides contention; you want retry counts, contention rates, and alerting, which means instrumenting the retry yourself anyway.
  • The duplicates cross a process or request boundary — retried HTTP calls, queue jobs, webhook redelivery. That's an idempotency-key problem; use $job->release() with backoff and a dedupe table, not an ORM wrapper.
  • Your database already gives you the atomic primitive: upsert(), insertOrIgnore() plus a follow-up update, or ON CONFLICT DO NOTHING with a conditional UPDATE. Prefer the atomic operation over retrying a non-atomic one.
  • The real defect is a missing unique index, an over-broad unique index, or an inappropriate isolation level. Retrying treats the symptom; the schema is the cause.
  • Procurement, security review, or an SLA requires maintained dependencies with a named owner. Here the bus factor is effectively zero.

A useful gate before adopting: grep production logs for unique-constraint exceptions over the last 30 days. If the count is near zero, this is a theoretical problem — close the ticket and revisit if it becomes real. If the count is material and concentrated in specific endpoints, fix those endpoints properly; a package-wide retry wrapper is a blunt instrument for a surgical problem.

How to Pitch It (Stakeholders)

For executives (one paragraph): This is a very small, free, MIT-licensed utility that papers over a specific class of database race condition — two simultaneous requests trying to create the same record, where one fails with a duplicate-key error. It is not a platform investment and it should not be on the roadmap. My recommendation is not to adopt it as a dependency: the repository is archived, has a single-digit user base and no maintainer, and taking it on creates an upgrade blocker and a supply-chain review item in exchange for roughly thirty lines of code we can write and own ourselves. The value it points at is real — failed signups and failed checkouts caused by concurrency are expensive and embarrassing — but the fix belongs in our own schema and retry logic, where we can measure it. Cost to replicate in-house: under a day of engineering. Cost to adopt and later unwind: a dependency audit, a framework-upgrade blocker, and an unclear owner.

For engineering leads: The package wraps upsert-style operations and retries them when a unique-key constraint is violated, up to a configurable limit. It does not make the underlying operation atomic, does not resolve deadlocks, does not deduplicate on business keys, and does not emit telemetry — so retries silently absorb contention signals you probably want to see. Before we consider it, I want a short spike: (1) quantify current duplicate-key exception volume from logs, (2) identify the top three offending write paths, (3) compare the three real options — atomic upsert(), an idempotency-key table, or an in-house retry decorator with metrics and a documented backoff. My expectation is that option three, written and owned by us, wins on every axis except time-to-first-commit, and even that gap is small. If the spike shows the problem is concentrated and low-volume, we schedule targeted fixes and skip the dependency entirely.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
frankyso/ipaymu
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core