ShotGrid API: Configuring Idempotent Create Operations to Avoid Duplicate Writes
0 reputation · 26 Apr 2023, 19:39 UTC
0 reputation · 26 Apr 2023, 19:39 UTC
Ensure that a retry of a ShotGrid create request after a network timeout does not result in duplicate entity records.
The ShotGrid REST API does not provide server‑side idempotency keys for POST‑based create operations, and the Python wrapper does not expose a built‑in mechanism to suppress retries that would cause duplicates. A common client‑side pattern is to generate a unique identifier and store it in a custom field, then query for an existing record before issuing a create. However, this approach introduces race conditions, requires read permission on the custom field, and may add extra API calls that affect rate limits.
Because the ShotGrid REST API does not support native server-side idempotency keys or atomic "upsert" operations, you must implement a Find-or-Create pattern using a custom text field as a unique identifier. This requires generating a UUID client-side, storing it in a dedicated ShotGrid field, and querying for that ID before every creation attempt.
sg_client_id) on the target entity type. Ensure the API user has read and write permissions for this specific field.create(), execute a find() request filtering by the custom field and the UUID.
# Conceptual Python logic
existing = sg.find("Asset", [["sg_client_id", "is", "uuid-1234-5678"]])
if not existing:
sg.create("Asset", {"sg_client_id": "uuid-1234-5678", "code": "Asset_01"})
find() call will return the existing record, and the duplicate create() will be skipped.It is important to distinguish between confirmed API behavior and the inherent risks of this architectural pattern:
find() check before either create() call completes, resulting in duplicates.find, one create). In high-volume pipelines, this may accelerate the consumption of your API quota.find() call will return an empty list even if the record exists, triggering a duplicate write.To verify this implementation, perform the following test case:
create() request and manually terminate the network connection before the response is received.find() call that only one entity exists with the specified UUID.Diagnostic Requirement: Are you executing these creates in a highly concurrent environment (multiple workers) or a sequential pipeline? If concurrent, the race condition risk is significant and may require an external locking mechanism (e.g., Redis) to ensure true idempotency.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.