Laravel / Editorial solution guide
A queued job cannot find a newly created record
Diagnose a worker that observes missing or stale database state around transaction commit, then test commit, rollback and retry behavior.
Read-only knowledge base · No public comments
Recognize the symptoms
A request creates a record and dispatches a job, but a worker intermittently reports the record missing or reads an earlier state. The same job may succeed when manually retried after the request finishes. Capture a synthetic record identifier, connection name, transaction boundary and safe timestamps; do not copy production payloads or credentials into the report.
Understand the likely causes
- A separate worker can execute before the transaction that created the record commits. A rollback can also remove the intended record, and a worker connected to another database or a lagging replica may see different state. A missing model is therefore evidence to investigate, not proof of one particular queue defect.
1. Reproduce the visibility boundary
Use a disposable database and queue with a worker operating on a separate connection. Create a record within a transaction and dispatch a job that looks up its identifier. Coordinate the fixture so the worker can attempt its read before commit; compare a read after commit. Record whether the lookup uses a primary or replica and whether both processes point to the intended database. A synchronous test alone does not reproduce a separate worker’s visibility.
2. Distinguish timing from other causes
Inspect the transaction outcome and the job’s serialized identifier, then check the effective database and queue configuration in each process. A rolled-back transaction should not leave a durable record; a committed record absent on the worker may indicate a different connection, replica lag or a lookup scope. Do not remove an ownership scope or retry indefinitely until the evidence identifies the actual boundary.
3. Defer dispatch until commit
If the job requires committed state, configure the queue connection’s after_commit option or use afterCommit on the individual dispatch as appropriate for the application. Laravel documents that jobs deferred within a rolled-back transaction are discarded. This ordering resolves the early-read race, but it does not guarantee delivery through every crash window or make an external side effect idempotent. Choose a durable outbox design when that stronger guarantee is required.
4. Verify commit, rollback and duplicate delivery
In the isolated environment, assert that a committed transaction makes the record available when the worker runs, and that rollback leaves neither the record nor the deferred job. Then deliver the same logical operation twice and check that the application’s own idempotency mechanism prevents an unintended duplicate effect. Inspect the actual queue driver and transaction configuration: a fake queue can check dispatch intent but does not prove cross-process timing.
5. Preserve operational evidence
Document which writes must precede the job and which component owns the stable operation identifier. Monitor safe failure categories and queue age without logging record contents or secrets. Include a recovery procedure for already failed jobs and review worker restart procedures when configuration changes. Test the failure path again after changing the queue driver, database topology or transaction placement.
Verify the fix
Verify commit, rollback, separate worker visibility and a duplicate delivery in a disposable environment. Inspect both the intended effect and durable database state; do not infer production delivery guarantees from a queue fake.
Keep it from recurring
- Keep transaction boundaries and dispatch timing explicit in code review. Use stable operation identifiers for retryable effects and preserve a safe, bounded recovery procedure for failed jobs.