phantom read: Re-running a range query returns new rows that another committed transaction inserted in between. A phantom read happens when a transaction runs the same range query twice and the second time sees extra rows, because another transaction inserted and committed them in between. Counting orders above 100 euros returns 42, then 43 a moment later inside the same transaction. The existing rows did not change; a new one appeared.
Whether this can happen depends on the isolation level. The SERIALIZABLE level prevents it everywhere. PostgreSQL's REPEATABLE READ also prevents it, and MySQL's InnoDB avoids it for normal reads in its default level through snapshots and gap locks. For most applications this is rarely a problem. It matters for checks like "is this time slot still free", where a unique constraint or a lock is the reliable way to prevent double bookings.
What does an example of phantom read look like?
SELECT COUNT(*) FROM orders WHERE total > 100; -- run twice: count grows because a row was inserted
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.
Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically improve database performance and eliminate slow queries. Use when debugging slow queries, designing database schemas, or optimizing application performance.