ealeng

Episode 38 of 55 · Serverless

Duplicate deliveries: skip-if-seen silently lost 2 of 22 images, and a lease lost none

Every upload in a thumbnail pipeline was delivered three times on purpose. With no check, 22 images got 66 thumbnail runs and 44 of them were duplicates. Skip-if-seen brought the duplicates to zero, but 2 images, the ones whose function crashed after claiming them, were never thumbnailed, and nothing reported it. A lease, a claim that expires, gave 22 runs, none duplicated, none lost. Also: 5 broken files that went to a dead-letter queue after three tries without holding up 95 good ones, and a 150-second job that a 60-second limit stopped at 110 of 300 items.

Lab 16 · Serverless and Event-Driven Compute10 min read
Video transcript9:20

0:00Cold open

0:01

I built a photo pipeline: upload a picture, and a function makes its thumbnails.

0:08

Real queues sometimes deliver a message twice. So I sent every upload three times, on purpose. With no check, twenty-two images got sixty-six thumbnail runs.

0:20

So I added the obvious fix: skip anything already seen. Duplicates: zero.

0:27

But two images never got their thumbnails. No retry fixed them, and nothing flagged them as missing.

0:36

How did a fix for duplicates lose work, without a sound?

0:42Intro

0:43

Hands-on system design, part thirty-eight.

0:46

Today: a broken file, duplicate deliveries, and a job that runs longer than a function is allowed to.

0:55Meet Lucía

0:56

I’m Lucía, a platform engineer. Users upload photos, and each photo needs three thumbnails.

1:03

It runs on a real AWS account. The photo lands in S3, Amazon’s file storage, and S3 puts a message about it on a queue.

1:14

A function, code AWS runs only when there’s work, takes up to ten messages at a time, makes the thumbnails, and writes a row to a database table.

1:26

If a message fails, the queue hides it for twelve seconds, then offers it again. After three tries, it moves to a dead-letter queue: a pile of failed messages, for a person to look at.

1:39

Think of a print shop. The queue is a tray of order slips. A clerk takes a handful. A slip that goes wrong goes back in the tray, and after three tries, into the problem box.

1:5201 · What you need

1:54

To run it yourself, you need an AWS account. The quick tests run offline, in a couple of seconds.

2:02

You pay per request, so idle costs almost nothing. By my estimate, the whole run is a few cents.

2:1102 · A broken file

2:12

First, a hundred uploads. Five of them aren’t images at all: just bytes, with a photo’s name.

2:20

All ninety-five good images were done. Typically one and a quarter seconds after the upload finished.

2:28

Each broken file was tried three times, then moved to the dead-letter queue. It held exactly the broken files, and nothing else.

2:38

And no good image waited for a retry: the slowest was done in two point six seconds, not twelve.

2:4703 · In code

2:48

Why didn’t they hold anything up? Here’s the function’s loop. Each message in the batch is tried on its own.

2:56

If one fails, the function reports just that one back, and only it returns to the queue. That’s called a partial batch response.

3:05

Without it, one bad message sends the whole batch back, up to ten, and the good ones get done again. Which brings us to duplicates.

3:1604 · Duplicates

3:17

A queue like this one promises to deliver at least once, not exactly once. So I tested duplicates on purpose.

3:26

Twenty-two new images, two of them rigged to crash. Each arrives three times: the upload, the same file again, and a copy of the first message.

3:37

On a crash image’s first try, the function’s running copy, its instance, dies on purpose: after it claims the image, but before any thumbnails.

3:48

With no check at all: sixty-six deliveries sent, sixty-six thumbnail runs. Forty-four of them were duplicates.

3:57

Here, a duplicate just overwrites the same thumbnails. But if the work were charging a card, that’s three charges. Doing the work once, however often it arrives, is called idempotency.

4:1105 · Skip if seen

4:12

The obvious fix: before working, write the image down as seen, by its name and a fingerprint of its bytes. If it’s already there, skip it.

4:23

Twenty thumbnail runs, and zero duplicates: even the same file uploaded again was skipped. But there were twenty-two images.

4:32

Which two do you think never got their thumbnails?

4:38

The two that crashed. The claim was written, then the instance died. Twelve seconds later, the queue offered the message again. The retry saw the claim, and skipped.

4:49

Skipping counts as success, so the queue threw the message away. It never reached the dead-letter queue. The crash was one function error, and after that, everything looked fine.

5:0206 · A lease

5:04

The fix is a claim that runs out: a lease. In the print shop, not “taken”, but “taken until three-oh-eight”.

5:12

The function writes “in progress, until eight seconds from now”. When it finishes, it marks the image done.

5:20

A crash leaves the lease behind, and it runs out after eight seconds. The queue offers the message again after twelve, so the retry finds the lease expired, and takes over.

5:32

Twenty-two runs for twenty-two images. Zero duplicates, and none lost.

5:39

The rule: a lease must outlast the longest the work can run, the function’s time limit, and run out before the queue’s retry. Here, eight seconds is under the ten-second limit, which works only because the work is short.

5:5507 · The lease in code

5:57

In the claim itself, the difference is one word. “Skip if seen” writes “done”, before any work. The lease writes “in progress”, with a deadline.

6:07

You can claim an image only if nobody has, or if the last claim’s deadline has passed. It’s one conditional write, so the database guarantees two instances can’t both win.

6:20

A lease gives one result, not always one run. If an instance dies after the thumbnails but before “done”, the next one makes them again. A second thumbnail is harmless. A second charge isn’t.

6:3408 · A long job

6:36

Now the other limit. AWS Lambda, the service that runs these functions, allows fifteen minutes at most. What about a longer job?

6:46

My test job takes two and a half minutes. To keep the test short, its limit is sixty seconds, standing in for the fifteen minutes.

6:56

Run as one call, it was stopped at sixty seconds: “Task timed out”. Its last saved progress said a hundred and ten of three hundred.

7:06

It writes its progress down, but never reads it back. So a retry would start at item one, and stop at the same place.

7:1609 · Chunks

7:18

The fix: split the job into pieces that each fit. Five chunks of sixty items, each taking half the limit, to leave room for slow items.

7:28

Step Functions, AWS’s workflow service, runs the chunks three at a time, and retries a chunk that fails. I made the second chunk fail halfway through its first try.

7:41

It finished: three hundred of three hundred, in sixty-three seconds. Faster than two and a half minutes, because three chunks ran at once.

7:51

By the plan, the failure costs nothing: the retried chunk still finishes before the last two chunks. The few extra seconds were probably start-up time.

8:02

What the workflow costs at list price: seven state transitions, one per step it runs, retries included: the map, five chunks, one retry. About two hundredths of a cent.

8:15

But the failed chunk did thirty items twice. If those items were charges, they’d need the same lease as the thumbnails.

8:24Back to Lucía

8:26

So, three lessons.

8:29

Report only the messages that failed, and give the queue a dead-letter queue. Then one broken file can’t hold up the rest.

8:38

Delivery is at least once. So key the work, not the message, and claim it with a lease, not a flag. “Skip if seen” loses work, silently.

8:48

A job longer than the limit becomes chunks, run by a workflow. And every chunk must be safe to run twice.

8:57

Next time: five hundred functions against one database, and a burst against a limit of ten.

9:04

For today: claim it, with a deadline.Lucía: Claim it, with a deadline.

9:08Summary

9:10

Fail one message, not the batch. Claim work with a lease. Split long jobs into chunks that are safe to redo.

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
Try it · Same deliveries, three idempotency modesRecorded result · lab 16, exercise 2B · one run on AWS

The key is marked done before the work, so the crashed claims are skipped on retry and never finished.

Deliveries sent
66
Thumbnail runs
20
Duplicate runs
0
Images never done
2
Images
22
Most runs for one image
1
Duplicates
0
Lost (silent)
2

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

  1. Delivery is at least once. Key the work by what it produces, such as bucket, key and etag, not by the message.
  2. 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.
  3. Report only the messages that failed. Use a partial batch response, and a retry limit that moves the message to a dead-letter queue.
  4. 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.
  5. 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.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.