# rpc 错误：code = AlreadyExists desc = 资源已存在：id=123

- **ID:** `go/grpc-already-exists-duplicate`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

客户端尝试创建已存在的资源，通常是由于重复请求或竞态条件。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   // Client sends a unique idempotency key
// Server checks if key already processed
if exists, return existing resource
else create new
   ```
2. **** (85% 成功率)
   ```
   resp, err := client.Create(ctx, req)
if status.Code(err) == codes.AlreadyExists {
    // Fetch existing resource
    getResp, err := client.Get(ctx, &GetRequest{Id: req.Id})
    // use getResp
}
   ```

## 无效尝试

- **** — This may violate business logic if the ID is meaningful; also doesn't address the root cause of duplicate requests. (80% 失败率)
- **** — The operation didn't create a new resource, leading to inconsistent state. (70% 失败率)
