Prompt
How do I build a streaming architecture that supports backfills and event replay?
Latest observation
A good streaming architecture for backfills and event replay usually comes down to one core idea:
Treat the event log as the source of truth, and make every downstream system reproducible from it.
Below is a practical way to design it.
1. Start with the right data model
Use an append-only event log
Store every change as an immutable event:
user_createdorder_placedpayment_failedprofile_updated
Put these in a durable log like:
- Kafka / Redpanda / Pulsar
- Kinesis / Pub/Sub
- Or an event store / CDC log if that fits your system
Include enough metadata for replay
Every event should have:
event_idfor idempotencyentity_id/aggregate_idevent_typeevent_timeingest_timeversionor schema versionpartition key- optional
correlation_id/trace_id
This lets consumers:
- deduplicate
- reorder where needed
- replay by time range
- support schema evolution
2. Design consumers to be replay-safe
A replay-safe consumer is one that can process the same event more than once without corrupting state.
Make processing idempotent
Common strategies:
- Store last processed
event_id - Use upserts instead of inserts where possible
- Use transactional writes
- Maintain a dedupe table keyed by
event_id
Separate computation from side effects
If a consumer:
- reads an event
- updates state
- sends an email / calls an external API
Then replay becomes dangerous.
Better:
- Have one stage compute state
- Another stage handle side effects using explicit “already sent” tracking
Use checkpoints, but don’t rely on them alone
Offsets/checkpoints tell you where you left off, but for backfills you need the ability to intentionally ignore them and reprocess from earlier offsets or timestamps.
3. Build the architecture in layers
A common pattern is:
Layer A: Raw immutable event stream
This is your source of truth.
Layer B: Derived streams / materialized views
Consumers build:
- aggregates
- projections
- analytics tables
- search indexes
These can be rebuilt from Layer A.
Layer C: Serving systems
Databases, caches, dashboards, APIs.
These should be treated as reconstructable projections, not the source of truth.
4. Plan for backfills explicitly
Backfills are usually needed when:
- you add a new derived field
- bug fixes require recomputation
- a consumer was down
- historical data changes
- a new model needs training data
Backfill patterns
A. Replay from the log
If you have the events retained long enough, the simplest backfill is:
- create a new consumer group or job
- replay events from a chosen offset/time
- write to a fresh target or overwrite a projection
Best for:
- rebuilding state
- new projections
- correcting logic bugs
B. Batch + stream hybrid
If the event log retention is limited, use:
- historical batch data from a warehouse/lake
- current live stream for recent data
Pattern:
- backfill historical range from batch storage
- switch to streaming from a checkpoint
- reconcile overlap window
Best for:
- long history
- compliance
- cheap large-scale rebuilds
C. Dual-path backfill
Run a backfill job in parallel with live consumption:
- backfill old data
- live stream handles new data
- merge carefully with dedupe and watermarking
Best for:
- large systems with little downtime tolerance
5. Make replay deterministic where possible
Replay is easiest if the output for a given input sequence is deterministic.
Good practices
- Avoid using current time in business logic unless passed as event time
- Avoid non-deterministic calls inside core transforms
- Version your transformation logic
- Keep state transitions explicit
Handle schema evolution
Use:
- schema registry
- backward/forward-compatible schemas
- versioned event contracts
When replaying old events:
- old events may lack new fields
- transforms should default safely
- don’t assume newest schema everywhere
6. Use partitioning and ordering carefully
Partition by entity if ordering matters
If all events for a user/order/account must be processed in order, partition by that entity ID.
This gives:
- ordering within a partition
- parallelism across partitions
Know your ordering guarantees
Most streaming systems guarantee:
- ordering within a partition not:
- global ordering
If you need global ordering, that’s expensive and often unnecessary.
7. Support “replay from X” and “rebuild to Y”
A practical system should let operators say:
- replay all events since timestamp
T - replay offsets
A..B - rebuild a projection from scratch
- run a historical correction over date range
D1..D2
To do this, your platform should expose:
- offset/time-based seeking
- retention windows
- checkpoint reset
- job versioning
- isolated output targets for rebuilds
8. Handle late, duplicate, and out-of-order events
These happen in real systems.
Late events
Use event-time processing and watermarks if you need to account for tardy arrivals.
Duplicate events
Use idempotency keys.
Out-of-order events
Depending on the domain:
- buffer for reordering
- use version numbers per entity
- reject stale updates
- apply last-write-wins only if valid
If the event stream is the canonical source, define clear business rules for conflicts.
9. Keep backfill jobs isolated from live traffic
Backfills can be expensive and risky.
Best practice
- Run backfills in separate consumer groups or pipelines
- Write to a shadow table or shadow index first
- Validate output before cutover
- Use rate limiting to avoid overwhelming storage or downstream APIs
Cutover pattern
- build new projection in parallel
- compare with old projection
- fix mismatches
- switch read traffic
- keep old version for rollback
10. Add observability from day one
To operate replay/backfills safely, track:
- consumer lag
- replay progress
- offsets processed
- event throughput
- dedupe rate
- error counts
- schema version distribution
- data quality checks
- reconciliation diffs
Also log:
- job version
- checkpoint ranges
- source range replayed
- output destination
11. A reference architecture
A common setup:
- Producers write domain events to Kafka/Pulsar/Kinesis
- Raw topic retains immutable events
- Stream processors build projections and derived events
- Materialized views stored in OLTP DB / warehouse / search engine
- Backfill jobs replay from raw topic or historical lake
- Validation pipeline compares rebuilt outputs with expected results
- Checkpoint store tracks consumer progress
- Schema registry manages evolution
12. Example workflow for a backfill
Suppose you add a new field customer_lifetime_value to a user profile table.
Steps
- Create a new projection job version
- Spin up a new output table/index
- Replay all historical events from retained log or archive
- Run current live stream into the same projection with idempotent writes
- Reconcile overlapping range
- Validate counts and aggregates
- Switch reads to the new projection
- Decommission old version later
13. Common pitfalls
- Using mutable state as source of truth
- Not storing enough event history
- Consumers that aren’t idempotent
- Tight coupling between replay and side effects
- No schema versioning
- No clear retention policy
- Backfills overwriting live data without safeguards
- Assuming offset checkpoints are enough for recovery
- No reconciliation or verification step
14. Rule of thumb
If you want backfills and replay to be easy:
- keep an immutable event log
- make consumers idempotent
- version everything
- isolate side effects
- store checkpoints
- support replay by offset/time
- treat downstream data as rebuildable projections
If you want, I can also give you:
- a Kafka-based reference design,
- a cloud-native AWS/GCP/Azure version, or
- a sample implementation in Python/Flink/Spark.