Skip to content

Shopify Dude Fix

Shopify Metafield Updates Overwriting Each Other? Use compareDigest

Prevent lost metafield updates when multiple Shopify integrations write to the same resource.

Quick answer: Use compareDigest with metafieldsSet when a write depends on a previously read value. On conflict, re-read and recompute instead of retrying the stale payload.
On this page

Why this happens

Two workers can read the same value and independently calculate a replacement. Without a conditional write, the last successful request wins. Shopify supports compare-and-set in metafieldsSet starting with API version 2024-07.

How to fix it

Read the owner ID, metafield value and compareDigest together. Include that digest in the mutation input. If creating a field only when absent, explicitly send null for compareDigest. Omitting the property instead opts out of that protection. On a conflict, inspect userErrors, fetch the current value and apply your business rule again with a bounded retry policy. For fields owned by a single authoritative system, consider eliminating competing writers altogether.

Practical example

Example: two workers append different identifiers to a JSON metafield. Each reads the same list and digest. The first succeeds; the second must re-read and merge its identifier into the updated list. A mutation can accept up to 25 metafields and is atomic, so a failed conditional write can prevent the rest of the batch from saving.

Common misunderstanding: compareDigest is not a lock and does not automatically merge values. A successful HTTP response is not proof that the mutation succeeded.
How to test this:

  1. Create a disposable product metafield
  2. read its digest twice
  3. write with the first digest
  4. attempt a different value using the stale digest
  5. inspect userErrors and verify the original successful write remains
  6. re-read and retry with a freshly computed value
  7. test explicit null against an existing field
  8. verify an unrelated field in a failing batch remains unchanged.