You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] PostgreSQL: Concurrent publishConfig calls lead to UniqueIndexConflict instead of idempotent update #14981
In a Nacos cluster environment using PostgreSQL as the database backend, concurrent calls to the publishConfig API for the same dataId, group, and tenant occasionally result in a 500 Internal Server Error. The root cause is a SQL exception: duplicate key value violates unique constraint "uk_configinfo_datagrouptenant".
Expected behavior
The publishConfig operation should be idempotent. If the configuration already exists, it should be updated gracefully. Under high concurrency or network retries, the server should handle the race condition and ensure that one of the requests succeeds in updating the record rather than throwing a UniqueIndexConflict exception to the client.
Actual behavior
Nacos server returns a 500 error during the race condition. The logs show that PostgreSQL rejects the INSERT statement because a record with the same unique key was committed by another thread just milliseconds prior, and the current thread's "check-then-insert" logic failed to catch it.
How to Reproduce
Set up a Nacos 2.x cluster with PostgreSQL 14+ as the storage backend.
Use a stress testing tool (like JMeter or a custom script) to call the /nacos/v1/cs/configs (POST) API simultaneously with the exact same dataId, group, and namespace.
Observe that a small percentage of requests will fail with a 500 error and a PSQLException in the server logs.
Environment
Nacos Version: 2.3.2 or 2.4.0
OS: Linux / Docker
DB: PostgreSQL 15.x
Deployment: Cluster mode
Possible Solution
For MySQL, Nacos leverages ON DUPLICATE KEY UPDATE. For PostgreSQL, the implementation should be reviewed to ensure the INSERT ... ON CONFLICT (data_id, group_id, tenant_id) DO UPDATE ... syntax is correctly utilized, or the service layer should implement a more robust retry/locking mechanism to handle this specific PG constraint violation.
Thanks for the detailed report and clear reproduction steps. The root cause is confirmed in the source: ExternalConfigInfoPersistServiceImpl.insertOrUpdate() uses a check-then-insert pattern (findConfigInfoState → addConfigInfo), which is susceptible to a TOCTOU race under concurrent writes. The PostgreSQL plugin (ConfigInfoMapperByPostgresql) inherits the base insert SQL without an ON CONFLICT clause, so the second concurrent INSERT hits the unique constraint on (data_id, group_id, tenant_id).
A fix could either override the insert path in the PG mapper to use INSERT ... ON CONFLICT DO UPDATE, or catch DataIntegrityViolationException at the service layer and retry as an update. Contribution welcome.
The fix only handles Spring standard DuplicateKeyException semantics.
For ExternalConfigInfoPersistServiceImpl.insertOrUpdate, this method is the non-CAS upsert path. If the first query returns empty and the subsequent insert loses a concurrent race with a duplicate key, the operation falls back to update, preserving insert-or-update semantics.
For insertOrUpdateCas, the method must preserve CAS semantics. If the insert branch hits a duplicate key, it returns ConfigOperateResult(false) instead of updating, so the caller can treat it as a CAS conflict.
We intentionally do not catch all DataIntegrityViolationException as duplicate-key conflicts. DuplicateKeyException is a subclass of DataIntegrityViolationException and specifically represents primary-key or unique-constraint violations. DataIntegrityViolationException is broader and may also represent non-null, check, foreign-key, data mapping, length, or other integrity failures. Treating all of them as duplicate key would hide real database errors.
For different datasource plugins and JDBC drivers, the same database conflict may be translated differently by Spring, for example DuplicateKeyException, broader DataIntegrityViolationException, or lower-level SQLException chains. #15465 handles the safe standard case first. Broader database-specific classification should be discussed separately in the datasource plugin layer.
Activity
github-actions commentedon May 1, 2026
This issue has been open 7 days with no activity. This will be closed in 7 days.
shiyiyue1102 commentedon May 2, 2026
Thanks for the detailed report and clear reproduction steps. The root cause is confirmed in the source:
ExternalConfigInfoPersistServiceImpl.insertOrUpdate()uses a check-then-insert pattern (findConfigInfoState→addConfigInfo), which is susceptible to a TOCTOU race under concurrent writes. The PostgreSQL plugin (ConfigInfoMapperByPostgresql) inherits the base insert SQL without anON CONFLICTclause, so the second concurrent INSERT hits the unique constraint on(data_id, group_id, tenant_id).A fix could either override the insert path in the PG mapper to use
INSERT ... ON CONFLICT DO UPDATE, or catchDataIntegrityViolationExceptionat the service layer and retry as an update. Contribution welcome.感谢详细的报告和清晰的复现步骤。根因已在源码中确认:
ExternalConfigInfoPersistServiceImpl.insertOrUpdate()采用 check-then-insert 模式(先findConfigInfoState再addConfigInfo),并发写入时存在 TOCTOU 竞态。PostgreSQL 插件(ConfigInfoMapperByPostgresql)继承了基础 insert SQL,没有使用ON CONFLICT子句,因此第二个并发 INSERT 会触发(data_id, group_id, tenant_id)唯一约束冲突。修复方向可以是在 PG mapper 中覆写 insert 路径使用
INSERT ... ON CONFLICT DO UPDATE,或者在 service 层捕获DataIntegrityViolationException后重试为 update。欢迎社区贡献。fix: handle concurrent publishConfig race condition for PostgreSQL (a…
KomachiSion commentedon Jun 1, 2026
@shiyiyue1102 这是否又倒回去出现之前的问题了, 我记得之前是因为存在不同数据库对DataIntegrityViolationException异常的不同行为,才诞生了先查询再变更的处理。
PR #11560
[ISSUE alibaba#14981] Make config insertOrUpdate idempotent under con…
KomachiSion commentedon Jul 7, 2026
Current solution has been refined in #15465.
The fix only handles Spring standard
DuplicateKeyExceptionsemantics.For
ExternalConfigInfoPersistServiceImpl.insertOrUpdate, this method is the non-CAS upsert path. If the first query returns empty and the subsequent insert loses a concurrent race with a duplicate key, the operation falls back to update, preserving insert-or-update semantics.For
insertOrUpdateCas, the method must preserve CAS semantics. If the insert branch hits a duplicate key, it returnsConfigOperateResult(false)instead of updating, so the caller can treat it as a CAS conflict.We intentionally do not catch all
DataIntegrityViolationExceptionas duplicate-key conflicts.DuplicateKeyExceptionis a subclass ofDataIntegrityViolationExceptionand specifically represents primary-key or unique-constraint violations.DataIntegrityViolationExceptionis broader and may also represent non-null, check, foreign-key, data mapping, length, or other integrity failures. Treating all of them as duplicate key would hide real database errors.For different datasource plugins and JDBC drivers, the same database conflict may be translated differently by Spring, for example
DuplicateKeyException, broaderDataIntegrityViolationException, or lower-levelSQLExceptionchains. #15465 handles the safe standard case first. Broader database-specific classification should be discussed separately in the datasource plugin layer.[ISSUE #14981] Fix config duplicate key handling (#15465)
[ISSUE alibaba#14981] Make config insertOrUpdate idempotent under con…