MySQL Unique Key Violation: Concurrency Race Conditions, ETL Ingestion Duplicates & Idempotent Patterns
Diagnosing error 1062 triggered by app race conditions and bulk insert duplicates, remediation with INSERT IGNORE and ON DUPLICATE KEY UPDATE.
Operational Verification & Reliability Metadata
The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.
▸Symptom
User registration or batch data loading fails with:
ERROR 1062 (23000): Duplicate entry 'test@example.com' for key 'users.email_UNIQUE'▸What You See in Production
Double-clicking submit buttons or parallel worker threads attempting simultaneous inserts yield an unhandled 500 Internal Server Error.
▸Root Cause Matrix
| Probability | Root Cause | Description |
|---|---|---|
| %60 | Concurrent Race Condition | Application issues SELECT before INSERT; interleaved transactions produce duplicate inserts. |
| %30 | ETL Re-runs | Reprocessing data ingestion batches without idempotent insert logic. |
| %10 | Collation Case Insensitivity | Under ci collations, uppercase and lowercase strings collide on unique indexes. |
▸EMERGENCY RUNBOOK (0-5 min)
-- 1. Inspect existing duplicate row
SELECT id, email, created_at FROM users WHERE email = 'test@example.com';
-- 2. Idempotent insert pattern
INSERT INTO users (email, full_name, updated_at)
VALUES ('test@example.com', 'Test User', NOW())
ON DUPLICATE KEY UPDATE full_name = VALUES(full_name), updated_at = NOW();▸PERMANENT BLUEPRINT
- 1Eliminate Check-Then-Act: Rely on atomic unique constraints with upsert patterns.
- 2Capacity Protection: Use
BIGINT UNSIGNEDfor primary keys to prevent auto_increment exhaustion.
▸Verification
SHOW INDEX FROM users WHERE Key_name = 'email_UNIQUE';Other Database Error Triage Playbooks
Other battle-tested diagnostic guides for high-severity database errors.
MySQL ERROR 1045 Access denied: root Password Reset Guide
Four causes of MySQL 1045, meaning of using password: YES/NO, and safe credential recovery via --init-file.
MySQL Deadlock ERROR 1213: Graph Analysis and Permanent Prevention
Root causes of MySQL 1213 deadlock, parsing LATEST DETECTED DEADLOCK output, gap lock concurrency issues, and resilient application retry design.
MySQL Lock wait timeout exceeded: Finding Blocking Sessions
Finding the blocking transaction using performance_schema, safe KILL decision matrices, and permanent locking reduction.