← build log
ml in productionpattern 11ML in production

Retraining as a saga, not a script

A five-step pipeline that dies two hours in shouldn't mean starting over from zero.

The problem

A full model retrain is five sequential steps, each with its own way of failing: materialize features from ClickHouse, build a training dataset, train the candidate, evaluate it, check the promotion gate. Run this as one script triggered by cron, and a failure at step three — a timed-out ClickHouse query, an out-of-memory training run — leaves no good answer for what happens next. Restarting from step one means redoing feature materialization that already succeeded, potentially hours of wasted work.

Approaches considered

One long script, cron-triggered. Every step runs inline, in order, in a single process. Simplest to write, and the failure mode is exactly the one above — no checkpointing, no partial progress, a crash means starting over.

Idempotent steps with a retry wrapper. Wrap the whole script in retry-with-backoff, and make each step safe to re-run. This helps with transient failures but still reprocesses every step on retry, and doesn’t track state anyone could inspect mid-run.

Saga orchestration with persisted per-step state. A dedicated orchestrator tracks progress through the five steps, persists state after each one, and can resume from the last successful checkpoint instead of the beginning.

Trade-offs

A saga means standing up state machine infrastructure and defining compensating actions for a pipeline that, most of the time, just runs start to finish without incident. It’s more moving parts than a cron job for the common case where nothing fails.

The decision

workflow-service runs the retrain pipeline as a saga: five steps, each a separate gRPC call into ml-service, with state persisted after every step and resumption from the last successful checkpoint on failure.

Why

The pipeline’s actual cost profile — hours of feature materialization and training — makes “start over from scratch” an expensive default for any transient failure. Checkpointed resumption turns a ClickHouse timeout at hour two into a retry of one step, not a redo of the whole run. This saga was also built last, after the registration saga, deliberately: meaningful compensation logic for “undo a half-built training dataset” only made sense to write once the pipeline existed and its real failure modes — the timeout, the OOM, the flaky evaluation run — were things that had actually happened, not ones imagined in advance.

Implementation

// workflow-service — the ML retraining saga. Each step persists
// state before moving on, so a crash resumes instead of restarting.
class MlRetrainingSaga(
    private val mlServiceClient: MlServiceClient,
    private val sagaStateStore: SagaStateStore,
) {
    fun run(sagaId: String) {
        val state = sagaStateStore.loadOrCreate(sagaId, MlRetrainingState())

        if (state.step < Step.FEATURES_MATERIALIZED) {
            mlServiceClient.materializeFeatures(sagaId)
            sagaStateStore.advance(sagaId, Step.FEATURES_MATERIALIZED)
        }
        if (state.step < Step.DATASET_BUILT) {
            mlServiceClient.buildDataset(sagaId)
            sagaStateStore.advance(sagaId, Step.DATASET_BUILT)
        }
        if (state.step < Step.MODEL_TRAINED) {
            val runId = mlServiceClient.trainModel(sagaId)
            sagaStateStore.advance(sagaId, Step.MODEL_TRAINED, runId = runId)
        }
        if (state.step < Step.EVALUATED) {
            val metrics = mlServiceClient.evaluate(state.runId!!)
            sagaStateStore.advance(sagaId, Step.EVALUATED, metrics = metrics)
        }
        if (state.step < Step.PROMOTION_CHECKED) {
            mlServiceClient.checkPromotionGate(state.runId!!)
            sagaStateStore.advance(sagaId, Step.PROMOTION_CHECKED)
        }
    }
}
// A scheduled retry picks up any saga that didn't reach the
// final step, resuming exactly where it stopped.
@Scheduled(cron = "0 */30 * * * *")
fun resumeStalledSagas() {
    sagaStateStore.findIncomplete(type = "ml-retraining")
        .forEach { state -> mlRetrainingSaga.run(state.sagaId) }
}