Key takeaways
- ✓With no check, the queue's triple delivery of 22 images made 66 thumbnail runs, 44 of them duplicates, because each delivery repeated the work.
- ✓Skip-if-seen, which marks an image as done before the work, cut the runs to 20 with zero duplicates, but the 2 images whose instance crashed after the claim were never done: the retry saw the claim and skipped, and the queue deleted the message as a success.
- ✓A lease, a claim that is in progress until a deadline (8 seconds here, with the queue retrying after 12), gave 22 runs for 22 images, with 0 duplicates and 0 lost, because the crashed claim expired and the retry took it over.
- ✓A broken file failed only its own message, thanks to a partial batch response, so the 95 good images were all done, with a median of 1.25 seconds from upload and a maximum of 2.62 seconds; the 5 broken files were each tried 3 times and then sat in the dead-letter queue, which held exactly those 5.
- ✓A 300-item job at 0.5 seconds an item needs 150 seconds, and one invocation with a 60-second limit stopped at 110 items saved; split into 5 chunks of 60 that Step Functions ran 3 at a time, it finished all 300 in 62.9 seconds, against a prediction of 60, with 7 state transitions at list price.
Every upload in a thumbnail pipeline was delivered three times on purpose. With no check, 22 images got 66 thumbnail runs. Skip-if-seen removed the duplicates and then left 2 images with no thumbnails at all, and nothing reported it. This chapter follows the fix that worked, a lease, and two other limits that a serverless function runs into: a broken file that must not hold up the rest, and a job longer than the function is allowed to run.
The examples come from Lab 16 (Serverless and Event-Driven Compute) of the system design labs: script 02_pipeline.py for the image pipeline, and 05_long_jobs.py for the long job. The pipeline ran on a real AWS account in us-east-1: a file upload to S3, Amazon's file storage, puts a message on an SQS queue; a Lambda function (AWS's "run this code when there is work" service) makes three thumbnails and writes a row to DynamoDB, a key-value table. The numbers are from one recorded run, on 4 November 2026. The timeline and the cost arithmetic are labelled where they are not measurements.
Why one upload arrives three times
A queue like SQS promises at-least-once delivery. A message can arrive once more than it should, and so can an event from S3. The lab did not wait for that to happen by chance. Its script sent each of 22 new images three times: the upload itself, the same bytes uploaded again (a new event with the same content fingerprint, the etag), and a replay of the first event.
Two of the 22 were named "crash". On their first attempt, the function's running copy, its instance, killed itself after it had claimed the image and before it made any thumbnails. Lambda runs each event on an instance, and an instance handles one event at a time.
With no check at all, 66 deliveries made 66 thumbnail runs, and 44 of those runs were duplicates. Here a duplicate only overwrote the same three thumbnail files. If the work had been charging a card, it would have been three charges. Doing the work once, however many times the event arrives, is what idempotency means. An event-driven function must be idempotent, or safe to run twice.
Key the work, not the message
The function keys the work by bucket, key and etag, the name of the upload and its content fingerprint. Before it does any work, it writes a claim for that key to a store, and checks what is already there. A second delivery of the same key finds the claim and does not repeat the work. Keying by message would not do: the same bytes uploaded again make a new event, so the same image would count as two messages.
The key is a product decision. A key by content hash would also catch the same bytes uploaded under a different name, and it would also skip a deliberate re-upload.
Skip-if-seen loses work after a crash
The obvious fix writes the key as "done" before the work. The lab ran this as its "skip if seen" mode:
- 20 thumbnail runs, 0 duplicates. Even the same file uploaded again was skipped.
- 2 images never done: both crash images.
The crash happened after the claim. The retry, 12 seconds later, found the key already marked, and skipped it. Skipping counts as success, so the queue deleted the message. It never reached the dead-letter queue, and the failure stayed silent. The fix for duplicates had lost work without a sound.
A lease: a claim that runs out
A lease is a claim with a deadline. The function writes "in progress, until a time 8 seconds from now", does the work, and then marks the key done. Three cases follow:
- A delivery that finds a done key skips it.
- A delivery that finds a live lease backs off, because another instance is working on it.
- A delivery that finds an expired lease takes it over.
After a crash, the lease is left behind, and it expires after 8 seconds. The queue offers the message again after 12, so the retry finds the lease expired and does the work. The rule has two sides: a lease must be longer than the longest the work can run, so a slow instance is not overtaken, and shorter than the queue's retry delay, so a crashed claim has expired when the message comes back.
The claim is one conditional write, so two instances cannot both win. A lease gives one result, not always one run. If an instance dies after writing the thumbnails but before marking the key done, the next instance makes them again. A second thumbnail is harmless. A second charge is not, and the lab did not test one.
The lab's lease gave 22 runs for 22 images, with 0 duplicates and 0 lost. The difference is in the claim itself: "skip if seen" writes done before the work, and the lease writes in progress with a deadline.
Broken files and the dead-letter queue
The queue is set to hide a message for 12 seconds after it is received, and to move it to a dead-letter queue after three receives. A dead-letter queue is a second queue for messages that keep failing, where someone can look at them. It is not a retry mechanism. Its job is to keep one bad message from being retried forever.
The lab uploaded 100 files, 5 of which were not images at all, just bytes with an image's name. Two things kept the bad ones from holding up the good ones:
- A partial batch response. The function takes up to 10 messages at a time. Each one is tried on its own, and if one fails, the function reports only that one back. The rest of the batch is deleted as done. Without this, one bad message would send the whole batch back, and the good images would be done again.
- A retry limit. The 5 broken files were each tried 3 times, then moved to the dead-letter queue, which held exactly those 5.
All 95 good images were done. The median was 1.25 seconds from upload to result row, the 99th percentile 2.51 seconds, and the slowest 2.62 seconds, so no good image waited for a retry.
A job longer than the limit
Lambda allows 15 minutes per invocation at most. The lab used a stand-in: a job of 300 items at 0.5 seconds each, which is 150 seconds of work, against a 60-second limit.
As one invocation, the job was stopped: "Task timed out after 60.00 seconds". It had saved its progress at 110 of 300 items, and was billed 60.5 seconds. The function writes progress down but never reads it back. A retry would start at item one, and stop at the same place every time. A timeout is a hard stop, and a retry cannot get past a job that does not fit.
The fix is to split the work into chunks that each fit with room to spare: five chunks of 60 items, each taking 30 seconds, half the limit. Step Functions, AWS's workflow service, ran them three at a time and retried a chunk that failed. The lab made the chunk starting at item 60 fail halfway through its first try, on purpose.
The workflow and the results
The image pipeline (02_pipeline.py, exercise 2): 100 uploads, with 5 broken, then 22 images delivered three times each, under each of three modes.
| Idempotency mode | Deliveries | Thumbnail runs | Duplicate runs | Images never done |
|---|---|---|---|---|
| No check | 66 | 66 | 44 | 0 |
| Skip if seen | 66 | 20 | 0 | 2 |
| Lease | 66 | 22 | 0 | 0 |
The key is marked done before the work, so the crashed claims are skipped on retry and never finished.
Bars are out of 66 deliveries (images never done: out of 22). Recorded values; no number here is computed by the widget.
Good images done: 95 of 95. Upload to result row: median 1.25 s, p99 2.51 s, max 2.62 s. Each broken file was tried 3 times, and the dead-letter queue held exactly the 5 broken files.
The long job (05_long_jobs.py, exercise 5): 300 items at 0.5 s each, 60-second limit.
| Design | Result | Items saved or done | Wall time | Failed attempts | State transitions |
|---|---|---|---|---|---|
| One invocation | Timed out at 60 s | 110 of 300 (saved every 10) | 61 s, billed 60.5 s | none to retry | none |
| Step Functions, 5 chunks of 60, 3 at a time | SUCCEEDED | 300 of 300 | 62.9 s (predicted 60) | 1, retried | 7 |
The workflow's cost is 7 state transitions at list price, $0.000175 by the lab's count. That is arithmetic from the transition count and the list price, not a bill. The failed chunk did its first 30 items twice. Each item in the lab is a timed sleep standing in for real work, so repeating one had no visible effect beyond the progress record. The lab did not test a step with a real side effect.
Caveats
- The duplicates were made by the test script, not by SQS redelivering on its own. The lab's point was to check the code path under duplicates. It did not measure how often AWS redelivers a message.
- The overlapping case was not tested. The duplicates were sent after the first run had finished, so the lease's "busy" path, a second instance finding a live lease while the first is still working, never ran.
- The lease is shorter than the function's time limit. The lease is 8 seconds and the thumbnail function's limit is 10. A slow but live instance could lose its lease, and another instance would then do the same work. It worked here because a good image was typically done 1.25 seconds after upload. The lease, the work and the 12-second retry were chosen together, and a slower work item would need a longer lease.
- One run per mode, on one account. The numbers come from a single recording, with each delivery sent by the script.
- The long job used a 60-second stand-in for Lambda's 15-minute limit. The chunks were not run on a real 15-minute job, and the retry of the long job itself was never run.
- The cost is computed, not billed. The lab's estimate is seven transitions at list price.
- Not covered: the lab does not test an external side effect, such as a charge or an email, that cannot be repeated. A payment provider needs its own idempotency key for that.
The checklist
- Delivery is at least once. Key the work by what it produces, such as bucket, key and etag, not by the message.
- Claim with a lease, not a flag. A flag written before the work loses the work after a crash. A lease with a deadline recovers it.
- Report only the messages that failed. Use a partial batch response, and a retry limit that moves the message to a dead-letter queue.
- Keep the lease longer than the work, shorter than the retry. Check it against the function's time limit, not just the work you expect.
- Split a job longer than the limit into chunks. Run them with an orchestrator, and make every chunk safe to run twice.
Check your understanding
Try each question before opening its answer.
Skip-if-seen lost two images and reported nothing. Why did the queue not send them to the dead-letter queue?
Because the retry did not fail. It found the key already marked done, skipped the image, and returned success. The queue treats a success as finished and deletes the message, so the message never came back enough times to reach the three-try limit. The failure is silent because nothing in the path returns an error.
In the lease run, why did the retry take over the crashed claim rather than skip it?
Because the claim carried a deadline of 8 seconds. The crashed instance never marked the image done, and the claim expired. The retry, 12 seconds later, found an expired lease and took it over. A live lease would have made the retry back off instead.
Why did the 95 good images finish in about 1.25 seconds, with 5 broken files in the same run?
Because the function reported back only the failed messages, through a partial batch response. The good messages in each batch were deleted as done straight away. The broken ones came back after the 12-second hide, were tried again, and after three tries were moved to the dead-letter queue.
The chunked job finished in 62.9 seconds, not the 60 it was predicted to take. Why is that acceptable?
The prediction was a plan, not a measurement. The retried chunk still finished before the last two chunks by the plan, and the extra few seconds were probably start-up time. The lab measured only the total, 62.9 seconds, and it stayed well under the 150 seconds of work a single invocation would have needed.
Next up: Chapter 39, 500 functions against one database, and a burst against a concurrency limit. The next lab test sends 500 instances at one Postgres database, and then a burst at a limit of ten instances, to see which one runs out first.