Section 05 of 14
The Worker
What This Does
The worker is an AWS Lambda function that sits between the queue and the order processor. It reads messages from SQS, calls processOrder() for each one, and decides what to do with the result: success, permanent failure (drop it), or transient failure (put it back in the queue for retry).
Why It Matters
This is the bridge between 'order is queued' and 'order is processed.' It handles the messy real-world concerns: what if the message is malformed? What if processing fails due to a network error? What if ShipStation is temporarily down? The worker classifies errors and decides whether to retry or give up.
How It Works
- 1.Lambda polls SQS and receives a batch of messages (configured for batch size 1 for rate limit safety).
- 2.For each message, it parses the JSON body into an OrderQueueMessage.
- 3.It calls processOrder(shopId, shopifyOrderId, approved) — the main pipeline function.
- 4.If processOrder succeeds, the message is acknowledged (removed from queue).
- 5.If processOrder fails with a TRANSIENT error (network, rate limit, 5xx), the message is returned to the queue via batchItemFailures — SQS will re-deliver it after a visibility timeout.
- 6.If processOrder fails with a PERMANENT error (cancelled order, bad data), the message is acknowledged so it's not retried endlessly.
- 7.Malformed messages (invalid JSON) are silently dropped with a log warning.
The Code
Key Decisions
- ●Batch size of 1 ensures we process orders one at a time. This respects ShipStation's rate limits and simplifies error handling.
- ●The batchItemFailures return mechanism lets SQS handle retry logic — we don't have to implement our own retry loop.
- ●Error classification is pattern-based: if the error message contains 'rate limit', '429', '5xx', 'ECONNRESET', etc., it's transient and will be retried. Everything else is permanent.
What Could Go Wrong
- ●If we classified a permanent error as transient, the message would be retried forever (until it hits the dead-letter queue after max retries).
- ●If we classified a transient error as permanent, we'd lose the order — it would be marked FAILED and require manual retry.
- ●Lambda has a 120-second timeout. If ShipStation is very slow, processing could time out. This is treated as a transient error and retried.