Mon-Fri 9:00-17:00

Allegro + Amazon + eBay from one panel — stock sync without race conditions

Stock and orders across 3 marketplaces without overselling: one source of truth, idempotent jobs, priority queues and three mistakes we made.

The problem

The client sells 5,000 SKUs on Allegro, Amazon and eBay at the same time. The wholesaler updates stock every 30 seconds, the client changes prices in the panel every few minutes. Each marketplace has its own API, its own limits and its own timeouts.

First attempt, chained:

wholesaler → shop → allegro
                  → amazon
                  → ebay

Two hours in, stock had drifted: Allegro 5, Amazon 7, eBay 3, shop 4. With parallel updates, all it takes is one request stuck on a timeout while the next one arrives first — the channel is left with the older value. Result: we sold more than the warehouse had.

Architecture that works

1. One source of truth

Stock lives only in the shop. Marketplaces don’t compute it; they only receive updates:

wholesaler → SHOP (changes only)
SHOP → queue → [allegro_job, amazon_job, ebay_job]

An order from any channel decrements stock in the shop with a single atomic query that can’t go below zero:

UPDATE products SET stock = stock - :qty
WHERE id = :id AND stock >= :qty;

If zero rows changed, the item is gone and the order goes to manual handling instead of packing.

2. Idempotent jobs

A job run five times must give the same result as one run. So it doesn’t carry a stock value, only “this SKU changed” — and reads the current stock from the database when it executes:

class SyncStockToAllegroJob implements ShouldQueue
{
    public function __construct(
        public int $productId,
        public Carbon $dispatchedAt,
    ) {}

    public function handle(): void
    {
        $product = Product::find($this->productId);

        // The product changed after this job was dispatched — a newer one is queued.
        if ($product->updated_at->gt($this->dispatchedAt)) {
            return;
        }

        Allegro::updateStock($product->allegro_offer_id, $product->stock);
    }
}

3. Priority queues

  • high — price and availability changes. Zero stock has to reach the channel first.
  • normal — routine stock sync.
  • low — descriptions, images, attributes.

A worker started with --queue=high,normal,low only takes from a lower queue once the higher one is empty.

4. Per-marketplace limits

Each channel counts limits differently:

  • Allegro — 9,000 requests per minute per application (Client ID), 429 when exceeded.
  • Amazon SP-API — a separate limit for each operation (token bucket); the current value comes in the x-amzn-RateLimit-Limit header.
  • eBay — daily call limits per API (Trading API defaults to 5,000 per day).

We keep the limiter below the channel’s limit, with headroom:

RateLimiter::for('allegro', fn () => Limit::perMinute(8000));
RateLimiter::for('amazon',  fn () => Limit::perMinute(800));

5. Retries with growing backoff

[60, 180, 600, 1800, 3600] — 1 min, 3 min, 10 min, 30 min, 1 h.

What went wrong for us

Version 1: synchronous Http::post() to the marketplaces straight from the shop controller. With 30 orders at once, PHP processes sat waiting on API responses and the shop stopped responding. Fix: a queue, 3 workers per marketplace, retries.

Version 2: a worker died mid-batch; after restart the same orders were imported a second time. Fix: a unique index on (marketplace, external_id), import via INSERT IGNORE, and a check before dispatch.

Version 3: each worker had its own request counter and each stayed within the limit. Allegro counts the limit per application, so together we exceeded it and got 429s. Fix: one shared RateLimiter in Redis for all workers, falling back to the database when Redis is unavailable.

Result

A full recalculation of 5,000 SKUs across 3 channels is 15,000 updates per cycle. That’s why we only send SKUs that actually changed. In 6 months of running there hasn’t been a single oversold item.

Another marketplace means a new job, a new queue and a new limiter. The rest of the system stays as it is.

More on this kind of work: wholesaler and marketplace integrations.

Dealing with something similar?

Tell us how it looks in your store or warehouse. The quote is free, we sign an NDA by default and reply within 24 business hours.

Let's talk →