Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Dealing with Race Conditions in GraphQL Product Creation: Need Advice on SKU Validation Approach

Dealing with Race Conditions in GraphQL Product Creation: Need Advice on SKU Validation Approach

ShaneOH
Shopify Partner
21 1 8

 

We are using the admin GraphQL API to create products, and everything works well except for occasional race conditions that result in multiple products sharing the same SKU. To address this, we attempted to execute a query before creation to check for an existing variant with the same SKU. Here is the query we are using:

 

query ($sku: String){
  productVariants(first: 1, query: $sku) {
    edges {
      node {
        title
        id
        sku
      }
    }
  }
}

However, we've encountered an issue where the API does not return the recently created product if it was created in less than 10 seconds (as tested). Unfortunately, this approach is not effective for our use case.

 

Questions:

  • Has anyone encountered similar race condition issues with product creation using the GraphQL API?
  • Are there alternative approaches to avoid creating multiple products with the same SKU in a race condition scenario?

Any insights or suggestions would be greatly appreciated!

Replies 2 (2)

Jclewis1989
Shopify Partner
18 1 8

Hey ShaneOH!

 

The race condition you're experiencing when creating products using the GraphQL API can be a common challenge in concurrent programming. I've made a note of some insights and suggestions to address this challenge below:

 

 

  1. Alternative Approaches to Avoid Race Conditions:

    • One common approach to avoiding race conditions in scenarios like this is to use database transactions or locking mechanisms.
    • Unfortunately, Shopify's GraphQL API does not support transactions (I believe), and locking is typically not practical in a distributed environment like Shopify.

    a. Retry Strategy:

    • Implement a retry mechanism in your code. If the SKU check query doesn't find a matching product, proceed with the creation. If it does, wait for a short period and then retry the creation.

    b. Unique SKU Enforcement:

    • Ensure SKU uniqueness at the application level before making GraphQL API requests.
    • Maintain a record of SKUs you've generated and validate that new SKUs are unique before sending a request to create a product.

    c. Webhooks and Event Driven:

    • Utilize Shopify webhooks to subscribe to product creation events. When a product is created, Shopify will notify your application via webhook.
    • Upon receiving the webhook, your application can perform additional checks and actions to enforce SKU uniqueness.

    d. Custom App Logic:

    • Consider implementing a custom Shopify app that enforces SKU uniqueness using Shopify's app infrastructure. Your app can listen for product creation events and apply your custom logic.
  2. Considerations...in my opinion / experience:

    • Keep in mind that retries and delays should be used cautiously to avoid overloading the API or creating significant delays in your application's workflow.

Curious what you end up proceeding with! Let me know how it goes!

James Lewis
ShaneOH
Shopify Partner
21 1 8

Hi @Jclewis1989 We are decided what to do.

Unfortunately none of your suggestions seems to fit our necessities

Thanks