brandonmtaylor.dev
Subject
Software
Date
Read
12 min

Event-driven vs batch: when to use which

Two ways to process data on AWS: when to use each, why I default to events, how I handle an event so timing stops mattering, and what to do when things fail.

Contents
  1. The real question: how fresh does the answer need to be?
  2. Why I lean toward events
  3. Use cases for each
  4. What they look like on AWS
  5. Handling an event: call back for the current state
  6. When it fails: the whole batch, or one item?
  7. Retries, redrive and reprocessing
  8. Reconciliation: proving nothing was lost
  9. The common answer is both
  10. The takeaway
  11. Test yourself
  12. Further reading

There are two basic ways to process data.

Batch: collect the work, then process it all together on a schedule. Every night at 2 a.m., read yesterday’s orders and build the sales report.

Event-driven: process each piece of work as it happens. An order is placed, an event fires, and something reacts to it within seconds.

I default to event-driven. Most work arrives one piece at a time, and handling it the same way keeps systems fast and simple. But batch is the right tool more often than people who like events admit, and the two fail in very different ways.

This post covers how to choose, what each looks like on AWS, how I handle an event once it arrives, and what to do when something fails, has to be reprocessed, or goes missing.

The real question: how fresh does the answer need to be?

Start with what the result is for.

If someone is waiting on it, or something bad happens while it’s late, it’s an event. A payment should be confirmed while the customer is still on the page. A fraud check has to happen before the money moves. An alert about a full disk is useless the next morning.

If the result is read later, or only makes sense over a whole set of data, it’s a batch. A monthly invoice covers a month. A daily report is read once a day. Training a model needs all the data at once.

There’s a middle ground, too: small batches every few seconds or minutes, sometimes called micro-batching. It’s what you get when Lambda reads from a queue in groups of records, and it gets most of the freshness of events with some of the efficiency of batch.

Why I lean toward events

Freshness is the obvious advantage. The others matter just as much day to day:

  • Processing time stays small. Each event is a few seconds of work, spread across the day as it arrives. A batch job does the whole day’s work at once, and its run time grows with the data. A nightly job that took minutes at launch can take hours a few years later, until one day it’s still running when people need the results. On AWS it also outgrows Lambda, which stops a single invocation at 15 minutes, and moves to Step Functions, Glue or AWS Batch.
  • Load is spread out. There’s no nightly spike against a database other systems also use.
  • Failures are small and found early. One bad event affects one item, and you can know within minutes, not the next morning when the report is missing.
  • Producers and consumers are decoupled. The service that publishes “order placed” doesn’t need to know who’s listening. A new consumer subscribes without changing the producer.

The trade-off is visibility. The work is spread across many small pieces in many services, so there’s no single job to check. You need tracing, good logs and, as covered below, reconciliation.

Use cases for each

Event-driven fits Batch fits
Order and payment processing Reports and dashboards refreshed daily
Notifications and alerts Billing and invoicing runs
Reacting to file uploads (resize an image, scan a document) Loading data into a warehouse (ETL)
Keeping two systems in sync as things change Reprocessing history after a bug fix
IoT and device telemetry Machine learning training
Fan-out: one event, several independent reactions Bulk jobs: re-encrypt every file, email every user
Reconciliation (more on this below)

Some signals point one way or the other:

  • Work arrives unevenly (quiet nights, busy lunch hours): events scale with it and cost nothing when idle.
  • Each item is independent and should succeed or fail on its own: events.
  • The result depends on the whole set (totals, rankings, deduplication across a day): batch.
  • The work is heavy but not urgent: batch, which can run off-peak on cheaper capacity like Spot instances.
  • The source only delivers in batches anyway (a daily file from a partner, a bank’s overnight export): batch. Pretending it’s a stream adds complexity for nothing.

What they look like on AWS

Event-driven systems are built from something that carries events and something that reacts:

  • Amazon SQS: a queue. Producers send messages, consumers pull and delete them.
  • Amazon EventBridge: routes events to targets by rules, so one event can reach several consumers.
  • Amazon SNS: fan-out, often into several SQS queues.
  • Amazon Kinesis Data Streams and DynamoDB Streams: ordered streams of records, read in order per shard.
  • AWS Lambda as the usual consumer, triggered by any of the above.

Batch systems are built from a schedule and something that runs the job:

  • EventBridge Scheduler to start the job.
  • AWS Step Functions to orchestrate the steps, with Distributed Map to fan a big job out over many items in parallel.
  • AWS Glue for ETL jobs over data in S3.
  • AWS Batch for long-running or compute-heavy jobs on containers.
  • S3 Batch Operations for one action over millions of S3 objects.
  • Athena or Redshift for SQL over the whole dataset.

Handling an event: call back for the current state

When I consume an event, my first choice is to treat it as a nudge, not as the data. The event says “order 123 changed.” The handler calls the owning team’s API and asks for order 123 as it is now, then acts on that.

That takes time out of the equation. Events arrive late, out of order, twice, or days later when someone redrives a dead-letter queue. If the handler acts on what the event carried, every one of those is a bug waiting to happen: an old “pending” event lands after “shipped” and rolls the order back. If the handler fetches the current state, it doesn’t matter when or how often the event shows up. The answer is always the latest one, and handling it twice gives the same result.

Diagram. 1: the orders team’s events say order 123 changed, and your handler treats that as a nudge. 2: the handler asks the Orders API for order 123 as it is now. 3: it acts on that answer, so late or repeated events don’t matter.

It has costs. Every event becomes an API call, so the source has to handle the load. And you get the state now, not the state when the event happened, which is wrong if you need a history of every change.

When there’s no API to call, the event has to carry the data, and the handler has to be more careful:

  • Ignore stale events. Use a version number or timestamp on the record, and skip any event older than what you already have.
  • Keep the logic as simple as possible. Validate it, map it, store it. The less a handler decides, the less there is to get wrong on a retry.
  • Don’t rebuild another team’s business rules. If you need to know whether an order can be refunded, don’t work it out from their raw fields. Ask them to put the answer in the event or the API. Otherwise their rule changes, yours doesn’t, and your system is quietly wrong with no error to tell you. Copy their logic only when there’s truly no other way, and then write down that you own a copy.

When it fails: the whole batch, or one item?

This is the decision that shapes the rest of the design.

Fail the whole batch when the output only makes sense complete. A financial close where the totals must balance, a report someone will act on, a file you hand to a partner: a version with 3% of the rows missing is worse than no version, because it looks right. Stop, fix, rerun.

Fail one item when the items are independent. If one image out of a million won’t resize, there’s no reason to throw away the other 999,999. Set the bad one aside, record why, and carry on.

Most real jobs want something in between: fail one item, but stop the batch if too many fail. One bad record is a data problem. Ten percent bad records is probably a bug, and continuing just produces more garbage. Step Functions Distributed Map has this built in, with a tolerated failure count or percentage; past that, the whole run fails.

Event-driven systems face the same choice, at a smaller scale. When Lambda reads from SQS it gets messages in batches. By default, if your function throws, the whole batch goes back on the queue, including the messages that succeeded, and they’ll be processed again. Turn on partial batch responses (ReportBatchItemFailures) and the function returns just the IDs that failed; only those are retried.

// Lambda reading from SQS with ReportBatchItemFailures turned on:
// only the messages listed here go back on the queue.
export const handler = async (event: SQSEvent): Promise<SQSBatchResponse> => {
  const batchItemFailures: { itemIdentifier: string }[] = [];
  for (const record of event.Records) {
    try {
      await processOrder(JSON.parse(record.body));
    } catch {
      batchItemFailures.push({ itemIdentifier: record.messageId });
    }
  }
  return { batchItemFailures };
};

Either way, a retried message runs twice, so the handler has to be safe to repeat. That’s idempotency, and in event-driven systems it isn’t optional.

Retries, redrive and reprocessing

There are two kinds of failure to recover from: items that failed, and items that succeeded with the wrong output.

Failed items, on SQS: a failed message becomes visible again after the visibility timeout and is retried. A redrive policy sets a limit (maxReceiveCount), after which SQS moves the message to a dead-letter queue (DLQ), where it stops blocking anything and waits for someone to look. Once the bug is fixed, you redrive: move the messages back to the source queue, from the console or the StartMessageMoveTask API, and they run again with the fixed code. Three things catch people out:

  • A DLQ nobody watches is slow data loss. Alarm on its message count. Anything above zero means real work didn’t happen.
  • Messages expire, even in the DLQ. SQS keeps messages 4 days by default, 14 at most. On a standard queue a message keeps its original timestamp when it moves, so give the DLQ a longer retention than the source queue.
  • Redriving before the fix just refills the DLQ. Fix first, and try a handful before moving everything.

Other sources differ. Async Lambda (from S3 or SNS) retries twice, then sends the event to an on-failure destination or DLQ. EventBridge retries a target for up to 24 hours, and each target can have its own DLQ. Kinesis and DynamoDB Streams are ordered, so a failing record blocks its shard until it succeeds or expires; set a retry limit, let Lambda split the batch, and send failures to an on-failure destination.

Wrong output is the harder case. A bug shipped Monday and was fixed Thursday, and three days of results need redoing. A DLQ doesn’t help, because nothing failed.

In batch, you rerun the job over those days. That’s one of batch’s biggest strengths, but only if the data is laid out for it. If the job reads and writes by date, you rerun Monday to Wednesday. If it doesn’t, you may have to reprocess everything. So partition by time, and make the job overwrite its output instead of appending, or a rerun gives you duplicates. If the job tracks what it already processed (Glue job bookmarks do), the rerun has to account for that, or it will skip the very data you want redone.

With events, you can only replay what you kept. A message deleted from SQS is gone. EventBridge archives can replay a time range of events. Kinesis keeps records 24 hours by default and up to a year, and a consumer can start again from an earlier point. Otherwise, write raw events to S3 as they arrive, and reprocessing becomes a batch job over the saved events. If your handlers call back for the current state, a replay is even simpler: each event just says “look again.”

Reconciliation: proving nothing was lost

Retries and DLQs handle failures you know about. Reconciliation catches the ones you don’t: the event that was never sent, the consumer that was quietly disabled, the message that expired in an unwatched DLQ.

Reconciliation compares two sources that should agree, and reports the differences. For example:

  • Orders in the order database vs orders in the warehouse table: same count, same total, for yesterday?
  • Payments recorded in your system vs the payment provider’s settlement report.
  • Files uploaded to S3 vs files the pipeline says it processed.

It’s a batch job by nature: it needs the whole day’s data from both sides. It also covers the blind spot of event-driven systems. An event system that stops receiving events looks exactly like one with nothing to do. No errors, no DLQ messages, just quiet. Only a check against the source of truth notices that yesterday had 4,000 orders and the downstream system saw none.

A few things make it work:

  • Compare counts and totals first. They’re cheap and catch most problems. Drill down to individual records only when they don’t match.
  • Allow for lag. Events in flight at midnight belong to neither day yet. Reconcile with a delay, or with a tolerance window.
  • Make the fix boring. When reconciliation finds missing records, the repair should be the normal path, like replaying those events or rerunning that partition, not a hand-written script.

The common answer is both

Plenty of systems use both, each for what it’s good at:

  • Events for the live path: react to each order, payment or upload as it happens.
  • Raw events saved to S3 as a record of everything that arrived.
  • A nightly batch that builds reports, reconciles against the source of truth, and repairs anything that was missed.

The events keep it fast; the batch keeps it correct.

The takeaway

  • Pick by freshness. If someone is waiting or harm grows with delay, use events. If the result is read later or needs the whole set, use batch.
  • Treat events as a nudge. When you can, call back for the current state so timing, order and duplicates stop mattering. When you can’t, keep the handler simple and leave other teams’ rules with them.
  • Decide failure scope on purpose: the whole batch for output that must be complete, one item for independent work, and a threshold for most things in between.
  • Give failures somewhere to go. On SQS, that’s a DLQ with an alarm and a redrive once the bug is fixed.
  • Keep what you need to reprocess. For batch, partition by time and overwrite. For events, archive them.
  • Reconcile. Retries handle the failures you see; reconciliation finds the ones you don’t.

Test yourself

1. A Lambda function reads from SQS in batches of 10. Message 7 throws an error, and partial batch responses are off. What happens?

All 10 messages go back on the queue and are processed again, including the 9 that succeeded. Turn on ReportBatchItemFailures and return only message 7’s ID, so only it is retried, and make the handler idempotent either way.

2. A nightly job builds the invoices you send to customers. One customer's data is malformed. Fail the job, or skip the customer?

Skip that customer and flag them, if each invoice stands alone: there’s no reason to delay everyone else’s. But set a limit. If many customers fail, it’s probably a bug, and the job should stop before sending a batch of wrong invoices.

3. A bug corrupted the output of an event-driven pipeline for three days. Consumers deleted the SQS messages after processing. Can you reprocess?

Not from SQS. Deleted messages are gone. You can reprocess only if the events were also kept somewhere: an EventBridge archive you can replay, a Kinesis stream with enough retention, or raw events saved to S3. That’s why many teams save every event as it arrives.

4. An event-driven system has had zero errors and an empty DLQ for a week. Is it healthy?

You can’t tell. A producer that stopped sending looks exactly like a quiet week. Reconcile against the source of truth, or alarm when the number of events processed drops below what’s normal.

Further reading