S3 Event - SQS - worker architecture: How to replay old files

, ,

I have a setup in AWS like this:
S3 event on create → SNS fan-out → SQS queue
Workers poll the queue, and process files as they come in.

This all works well, but my question is this: How can I replay / reprocess old files?

My first thought was to make a script that fills the queue with “fake” s3 events – as long as they have the same schema who cares – for existing files, and let the workers process them.

However, the issue with that is the message retention time – I’m talking about tens of thousands of files, and it’ll likely take more than the maximum 14 days to get through the whole set.

The alternative, which is how i’ve done it before, is to remove / ignore the sqs layer, and build a reprocessing harness that’s like “for each file in s3, download it, run the thing, repeat in parallel * N”. This works, but I’m trying to explore nicer alternatives. SQS gives us nice stuff in production, and I was hoping I could reuse the prod architecture for this.

One thing I could do is fill the queue at more or less the same rate as the consumers can drain it – this would add a fair bit of complexity to the setup though – I’d have to do impedance matching between the two. It would be nice to be able to just dump all the messages in there and let it drain at whatever rate it can.

Does anyone have any experience with this sort of thing?

You could consider writing a new job type your workers know how to process, an “enqueue process files” job. This job starts up, scans S3 for 1000 blobs (for example) and emits a new job for each. Then, it enqueues itself again, passing along the continuation token from the S3 directory listing operation it just did, for its next job.

This should mean you can kick off a single job that then immediately enqueues processing jobs, and reschedules itself to the end of the queue, so that it’s asked to re-queue more jobs when the workers get to it.

You may need to read up on how valid a continuation token is between s3 directory enumerations in the face of changes, but hopefully gives you some ideas.

Very cool idea, thanks! I hadn’t thought of having different job types in the queue.

I really like having two queues:

  • real-time queue with its own SNS/SQS messages
  • non-real-time queue with its own SNS/SQS messages

And then they can each have different prioritization, so when I backfill I just generate messages for the non-real-time queue and the jobs just handle that in the background.

If there’s a concern about messages on the non-real-time queue timing out, then I’d suggest just adding a small subset at a time.

I was planning on having a separate queue for this yeah. Adding a small subset at a time works, but it gets tricky right: what rate do you add them at? now you have to keep this out-of-band process running continuously / on a cron instead of as a one-off to seed the queue. etc.

Making some generous assumptions about the nature of your workload - you could think about decoupling your “worker” layer (business logic processing files) from the delivery mechanism (Event->SNS->SQS), and add on a new batch mode to ingest events to your workers. You could do this by factoring out your business logic behind controllers that handle the specifics of the input source.

For example, if the worker in question is on AWS Lambda, then this factors out nicely - you could have a function that could either be invoked by SQS (EventSourceMappings) or S3 Batch Operations. Some controller code could inspect the request format to determine what request type it is, and parse it accordingly for the worker code.

This is the approach I’ve used in the past. The file processing logic is fairly decoupled from the sqs stuff, but I need to replace the sqs stuff with a whole s3 list -> parallel => process file * N thing. I’ve written that harness several times and never been satisfied with the result. I’m thinking about how to improve on that, and leveraging the existing sqs setup (or a copy of it) seemed like a good direction. If it weren’t for the message retention thing it would have been almost free.

What about just looking at file sizes and coming up with an algorithm to divide them evenly based on number of nodes.

Now just pass this to the nodes via a json config generated and stored in s3 or and database.

The other way would be to use kinesis to pump your file data in their sequentially I chunks on partitions by file name and process them in the cluster or eks and write the processesed output to s3 . If that makes sense at all. This can be done at any time and can be stored for more than 7days. Once it’s done. Delete the kinesis stream. And data. It makes it convenient that you can process it any time and can replay. This is if cost is no concern

I’ve created an S3 antivirus time ago and I had the same problem.
To do so, I’ve created a Lambda function to scan the bucket periodicly and push notifications into the same queue. It will ended up to increase the queued messages, but in my case I have an ECS cluster that scale in/out based on CloudWatch metrics feeded by SQS queue total object numbers.
The scanned files are tagged as ‘scanned’ so I can filter and set limits for the Lambda script on how many file retrieve per time. The Lambda function is triggered by Cloud Watch Alarm every day at midnight where we don’t don’t have too much uploading . Also my ECS cluster is running on top of EC2 spot instances AutoScaligGroup to reduce costs.

Hope this solution can help you.

interesting.. so you dont really care if the queued messages get dropped due to retention time - as you’re constantly re-sending those messages to make sure nothing gets lost. And you’re using s3 object tags as state.

Normally I wouldn’t do this since I wouldn’t have messages aging off the queue or want to extend a backfill over weeks - so this isn’t how I would normally build this.

But given this scenario, I suppose you could easily have a process that runs off a simple scheduler like cron to run say hourly, examines the queue to identify the most recent message, gets the current date and then creates a few days of messages if warranted.

This is a relatively simple pattern that works well in replicating data, aggregating data, etc - when you might have way too much data to process all at once.

That could work – some reservations:

you can only see the front of the queue and the approximate number of messages. you could then guess based on some heuristic what to do. also, what’s to stop it from creating overlapping events? this isnt a huge deal if the processing is idempotent (it is), but it’s wasted effort.

in order to examine a message, you increment its receivecount without actually processing it. this effectively decreases it’s retries_left count. which is probably fine, but odd.

but I need to replace the sqs stuff with a whole s3 list -> parallel => process file * N thing.

That’s fair… this was why I was highlighting S3 Batch Operations as an option to help simplify that, but again that makes a number of assumptions on your workflow.

Yeah. My workflow isnt expressed as a Lambda, which is what that would require right. It could conceivably be made into a lambda, though. Something to think about for sure.

Great points. Overlapping events are probably normally not a big deal - other than if the process crashes while copying them up and doesn’t leave something like a checkpoint to restart from. Even then probably not a super common problem.

I forgot about the cost of inspecting the aws messages. It might be worth saving the last date sent somewhere else to pick up from.

I think this is what I’m going to go with, thanks for the suggestion. Any tips/tricks/gotchas you’ve run into with this sort of system?

No problem, I’d double check a few things:

  1. Between iterations of S3 List of 1000 blobs (for eg), when you pass the continuation token (to resume the iteration next job) some time later, what happens if blobs were added/removed during that time? Did we “miss” some blobs? Is that OK? S3 docs should help describe what happens.
  2. Do an end to end sanity check, for example, perhaps your S3 List Blob job also keeps track between jobs how many blobs it has processed, so you can get a total at the end, and see if that matches the total s3 bucket size (for audit purposes).

I’d use a trust/verify approach, give it a go, write some auditing code, check it works as you expect :slight_smile:

I actually happen to know that my files will be prefix/timestamp.gz, so I was thinking about paging using startafter and list_objects’ inherent key ordering. Rather than mess about with continuation tokens.

Auditing code’s not a bad idea.

Thanks!

Exactly. Also my massages are not dropped as my cluster scale to face the growing queue adding workers. Anyway, tags help me to scan files that have not scanned yet.