go data_error ai_generated true

rpc error: code = AlreadyExists desc = resource already exists: id=123

ID: go/grpc-already-exists-duplicate

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-10-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

The client attempted to create a resource that already exists, often due to a duplicate request or race condition.

generic

中文

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

Workarounds

  1. 90% success
    // Client sends a unique idempotency key
    // Server checks if key already processed
    if exists, return existing resource
    else create new
  2. 85% success
    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
    }

Dead Ends

Common approaches that don't work:

  1. 80% fail

    This may violate business logic if the ID is meaningful; also doesn't address the root cause of duplicate requests.

  2. 70% fail

    The operation didn't create a new resource, leading to inconsistent state.