Quick revision
The short answer
Those snapshots are mementos of process state, and keeping every one forever is rarely needed. The usual redesign takes snapshots only at checkpoints and records an event or command log in between, as event sourcing with snapshots does, so recovery loads the latest snapshot and replays a few events. A retention policy keeps recent snapshots and archives or deletes old ones, ideally by dropping time partitions, and snapshot formats carry a version so old ones stay readable. For rollback across services, compensating actions in a saga are usually a better model than restoring old state.
What to remember
In this answer, we treated the workflow snapshots as mementos and saw that their uses, crash recovery and rollback, need only recent state. We redesigned storage as checkpoints plus an event log, which bounds replay and cuts storage, set a retention policy that keeps running and recent workflows and archives the rest through time partitions, questioned restoring state across services in favour of saga compensations, and versioned snapshot formats so old records stay readable.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Those snapshots are mementos of process state, and keeping every one forever is rarely needed. The usual redesign takes snapshots only at checkpoints and records an event or command log in between, as event sourcing with snapshots does, so recovery loads the latest snapshot and replays a few events. A retention policy keeps recent snapshots and archives or deletes old ones, ideally by dropping time partitions, and snapshot formats carry a version so old ones stay readable. For rollback across services, compensating actions in a saga are usually a better model than restoring old state.
Background to this question
A snapshot table that grows to billions of rows is a common sight in workflow, order and claims platforms that were designed for safety first. Every step wrote the whole process state so that the service could always go back, and nobody decided when that history could go away. Now the table dominates storage costs, slows queries and backups, and makes schema changes frightening. This scenario question checks whether we understand what the snapshots are for and can redesign without losing that purpose. In this answer, we will recap how these snapshots act as mementos, estimate the growth, redesign around checkpoints and a log, set a retention policy, question whether restoring state is the right rollback model at all, and plan for format versions.
1A Quick Recap: What the Snapshots Are Doing
Each snapshot is a memento: a copy of the workflow's state, such as current step, collected data and decisions, taken so the service can return to it. They are used for two things. Recovery after a crash means loading the latest state of each running workflow and continuing. Rollback after a failure means returning a workflow to an earlier good state, for example before a step that failed halfway.
Both uses need only a little of the history. Recovery needs the latest snapshot of running workflows. Rollback typically needs the state just before the failing step. A snapshot of a workflow that completed eight months ago serves neither purpose; at most it serves audit, which has different needs.
2Why the Table Reached Billions of Rows
The numbers explain themselves. Suppose the service runs 1.5 million workflows a day, each with 20 steps, and writes a full snapshot after every step. That is 30 million rows a day and about 11 billion rows a year. If each snapshot is a 4 KB JSON document, the table grows by roughly 120 GB a day before indexes. And because snapshots were written for safety, nothing ever deletes them.
Large tables are not only a storage bill. Index maintenance slows writes, vacuum or purge jobs fall behind, backups and restores take hours, and adding a column or changing an index becomes a project. So the question is not how to store billions of rows more cheaply, but how many of them we actually need.
3Checkpoints Plus a Log Instead of Every State
The first change is to separate what happened from where we are. Instead of a full snapshot per step, the service appends a small event for each step, such as StepCompleted with the step name and its output, and writes a full snapshot only at checkpoints: every 10 steps, at a long wait, or at the end. This is the event sourcing with snapshots design: the event log is the history, and snapshots are an optimisation that bounds how much must be replayed.
Recovery then loads the latest snapshot for a workflow and replays the events recorded after it. With a checkpoint every 10 steps, at most 9 small events are replayed, which takes milliseconds. Rollback to the state before step 14 means loading the checkpoint at step 10 and replaying steps 11 to 13. The events themselves are much smaller than full states, perhaps 300 bytes, so storage per workflow falls from 20 x 4 KB = 80 KB to 2 x 4 KB + 20 x 300 bytes = about 14 KB, roughly a sixth.
- Load the latest snapshotfor example taken after step 10
- Decode it by its format versionupcast old versions to the current model
- Read events after its sequence numbersteps 11 to 13
- Apply each event in orderdeterministic: state + event -> state
- Is the workflow still running?YesResume at the next stepNoDone, nothing to resume
5 steps in order. Play them, or step through them one at a time.
A checkpoint bounds the replay; the log keeps the steps in between.
Notice that state.apply(e) must be deterministic: replaying the same events must always rebuild the same state. That means the event records the outcome of a step, such as the credit score returned, not an instruction to call the credit service again.
4A Retention Policy That Matches the Purpose
Next we decide how long each kind of record lives, based on what it is used for. While a workflow runs, keep its latest checkpoint and the events after it, plus perhaps the previous checkpoint for safety. When a workflow completes, keep the final state and drop intermediate checkpoints. After a retention period agreed with the business, for example 90 days, move completed workflows' events to cheap archive storage such as compressed files in object storage, or delete them if no audit requirement exists. Audit and legal retention are real requirements, but they are better served by an archive designed for them than by the operational table.
How we delete matters as much as what. Deleting billions of rows with DELETE statements creates enormous write load and, in PostgreSQL, dead tuples for vacuum to clean up. Partitioning the tables by time, for example one partition per month, turns retention into dropping or detaching an old partition, which is nearly instant. For the existing table, the migration is to start writing into the new partitioned structure, backfill only the snapshots still needed for running workflows, and archive the rest in bulk.
5Is Restoring Old State the Right Rollback Model?
There is a deeper question hidden in the scenario. Restoring a snapshot rolls back only our own service's state. If step 12 charged a card in the payment service and step 13 failed, putting our workflow back to step 11 does not refund the card. The payment service has moved on, and our restored state now disagrees with the world.
That is why workflows that span services usually use the saga pattern: each step has a compensating action, an operation that semantically undoes it, such as refund payment for charge payment or release stock for reserve stock. On failure, the workflow runs the compensations of the completed steps in reverse order and records them as events too. Snapshots remain useful for crash recovery, but rollback becomes forward-moving compensation, which needs far less stored history than the ability to jump back to any earlier state.
6Versioning the Snapshot Format
Snapshots and events that live for months will outlive the code that wrote them. Every stored document should carry a format version, and the reading code should upgrade older versions to the current model before use, a technique often called upcasting. That lets the team rename a field or split a step without rewriting billions of stored records, and it is what makes the shorter retention safe: we only need upcasters for versions still inside the retention window, and can delete the old ones when the last record of that version expires.
7Follow-up questions
Could we just compress the snapshots instead?
Compression might cut storage by three to five times, which buys time but does not change the growth rate or the operational pain of an ever-growing table. It is a good complement, especially for the archive, but not a substitute for deciding what to keep.
How do you migrate without risking running workflows?
Run both models for a while: new workflows use checkpoints plus events, while existing ones continue on full snapshots until they complete. Recovery code reads either format. Once no running workflow uses the old model, the old table is archived in bulk and dropped.
What if the business wants to see the full history of a workflow?
The event log is a better history than snapshots, because each event says what happened and when, rather than forcing someone to diff states. For older workflows, an archive query or a reporting store fed from the events serves that need without keeping the operational table large.
8Conclusion
In this answer, we treated the workflow snapshots as mementos and saw that their uses, crash recovery and rollback, need only recent state. We redesigned storage as checkpoints plus an event log, which bounds replay and cuts storage, set a retention policy that keeps running and recent workflows and archives the rest through time partitions, questioned restoring state across services in favour of saga compensations, and versioned snapshot formats so old records stay readable.
Open with
First I would ask what the snapshots are for. They are mementos used for crash recovery and rollback, and both only need recent state, so keeping a full snapshot of every step forever is not required.
Then, if they want more
A quick estimate usually shows the problem: 1.5 million workflows a day with 20 steps is 30 million rows a day, around 11 billion a year. I would change the model to checkpoints plus a log, like event sourcing with snapshots.
Each step appends a small event with its outcome, and a full snapshot is taken every ten steps or at long waits. Recovery loads the latest snapshot and replays the few events after it, and storage per workflow drops to a fraction.
Then a retention policy: keep the latest checkpoint and events for running workflows, keep only the final state for completed ones, and archive or delete after an agreed period like 90 days. I would partition the tables by month so retention means dropping a partition, not running huge deletes.
I would also challenge rollback by restoring state, because if a step charged a card in another service, restoring our snapshot does not refund it. Sagas with compensating actions are the better model across services. Finally, every snapshot and event carries a format version with upcasters, so old records stay readable.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A workflow service saves snapshots of process state so it can roll back after failures...": https://scrivio-interview-library.netlify.app/java/behavioral-patterns/1fe8f210e3/