ShopifyAppsCase study6 min· Alexey Bulatnikov

Our stock sync app stopped syncing. I wrote the sync between two Shopify stores myself.

We run the same fashion catalogue in two Shopify stores, one for Bulgaria and one for Romania. The goods are in one stockroom, so a size sold in one store has to disappear from the other. An app did that for us. On 7 November it stopped: its free plan now syncs only when somebody presses a button. Nobody was pressing the button.

I had inventory code from another app of mine, so I wrote the sync that week. These are the decisions that mattered.

Match by barcode, and do nothing else

The app syncs stock quantities and nothing more. It never creates a product, never edits a title or a price. Each store keeps its own language, prices and photos. That one limit removes most of the ways a sync app can damage a store.

Variants are matched by barcode. Product ids differ between two stores, titles are translated, and SKUs get edited by hand. The barcode is printed on the label and nobody retypes it.

What the first scan found

Before writing anything to either store, the app only read:

  • 73,371 variants in the main store, 59,196 in the second
  • 58,817 matched by barcode
  • 194 of those had a different quantity: the second store was higher in 149 cases and the main store in 45

So after a month without a working sync, 0.3% of the matched variants were wrong. That is a small number, and each of those 194 was a size that one store could sell without having it. I saved the list as a file before the first write, so that there was a record of what the app changed on day one.

The scan also showed what can never sync: 14,518 barcodes that exist only in the main store, 367 only in the second, and 4 variants with no barcode at all. A coverage page in the app lists them. A sync that reports "all good" while a fifth of the catalogue is outside it is not telling the truth.

Three loops

  1. A webhook. Shopify calls the app when a stock level changes, and the app writes the new quantity to the other store within seconds.
  2. A comparison every 10 minutes, in the app's own database, for anything a webhook missed. Webhooks are delivered at least once, and sometimes late.
  3. A full scan of each store every 6 hours, which rebuilds the picture from Shopify itself.

Each loop covers the failures of the one before it. The first is fast, the second fills its gaps, and the third does not depend on anything the app remembered.

The echo

When the app writes a quantity to the second store, that store sends a webhook of its own: stock changed. A naive sync answers it, writes back to the first store, and the two stores talk to each other until somebody stops them. The app records each write it makes, and when the matching webhook comes back it is counted as an echo, not as a change. On the first day 196 levels were written and every returning webhook was recognised.

Small rules that prevent large mistakes

  • Negative stock is sent as 0. One store allowing oversell should not make the other show minus three.
  • If one barcode is on several variants in the same store, the app skips it and reports it. It cannot know which variant is meant.
  • The default direction is one way, from the main store to the second. Two way is a setting, because with two way a mistake in either store spreads to both.
  • The sync started switched off. I looked at the list of differences first, and then switched it on from the app.

One thing about Shopify custom apps

A custom app can be installed on one store only. Two stores need two app records, each with its own keys. Both are served by the same program, which looks at the host name of the request to know which store is calling.

See Shopify app services and prices