← build log
orchestrationpattern 13Orchestration & concurrency

A saga for something as small as signing up

Retrying registration after a failed step risks a second OTP email or a half-activated account.

The problem

Registration spans three services: create a pending account in user-service, send an OTP through notification-service, wait for the client to verify it, then activate the profile. Any step can fail or time out independently of the others. Retrying the entire flow from the top risks sending a second OTP email to someone who already received one; not retrying risks leaving an account permanently stuck between “pending” and “active.”

Approaches considered

Synchronous call chain with retry-with-backoff. api-gateway calls user-service, which calls notification-service inline, retrying the whole chain on any failure. Simple to reason about linearly, but a retry after the OTP step succeeded re-sends the email, and there’s no persisted record of which step actually completed.

Client-driven multi-step flow. Push responsibility for retrying individual steps to the client — if verification fails, the client re-requests just that step. This assumes a well-behaved, currently-connected client, which isn’t guaranteed across a multi-minute OTP flow with app backgrounding and network drops.

Saga orchestration with compensating actions. A dedicated orchestrator persists state per step and, on a failure that invalidates prior steps, runs compensating actions in reverse — rolling back a pending account rather than leaving it stranded.

Trade-offs

A saga is more infrastructure than a flow this size strictly needs — most registration failures are transient and would be handled just as well by a simpler retry policy scoped to the failing step alone. It’s deliberate overhead for a process that, most of the time, succeeds in one pass.

The decision

workflow-service runs registration as a saga: create pending account → send OTP → verify → activate profile → complete, with per-step state persistence and reverse-order compensation on failure.

Why

The actual justification isn’t that registration is complex — it’s that Vitrin already needed saga infrastructure for the ML retraining pipeline, and running registration through a second, different orchestration mechanism (retry-with-backoff, client-driven, whatever) would mean the system has two ways of handling multi-step failure instead of one. Consistency won over minimalism here: one saga engine, one mental model for “what happens when step three fails,” applied to both the flow that’s genuinely hard to get right and the one that’s small enough it almost didn’t need it.

Implementation

// workflow-service — the registration saga. Each step persists
// before moving on; failures trigger compensation, not a blind retry.
class RegistrationSaga(
    private val userServiceClient: UserServiceClient,
    private val notificationClient: NotificationClient,
    private val sagaStateStore: SagaStateStore,
) {
    fun run(sagaId: String, request: RegisterRequest) {
        val state = sagaStateStore.loadOrCreate(sagaId, RegistrationState())

        try {
            if (state.step < Step.ACCOUNT_CREATED) {
                val accountId = userServiceClient.createPendingAccount(request)
                sagaStateStore.advance(sagaId, Step.ACCOUNT_CREATED, accountId = accountId)
            }
            if (state.step < Step.OTP_SENT) {
                notificationClient.sendOtp(state.accountId!!, request.email)
                sagaStateStore.advance(sagaId, Step.OTP_SENT)
            }
            // verification is a separate, client-triggered call into
            // this same saga, keyed by sagaId, advancing it to VERIFIED
            if (state.step < Step.ACTIVATED && state.step >= Step.VERIFIED) {
                userServiceClient.activateProfile(state.accountId!!)
                sagaStateStore.advance(sagaId, Step.ACTIVATED)
            }
        } catch (e: SagaStepException) {
            compensate(sagaId, state, upTo = state.step)
        }
    }

    private fun compensate(sagaId: String, state: RegistrationState, upTo: Step) {
        if (upTo >= Step.ACCOUNT_CREATED) {
            userServiceClient.deletePendingAccount(state.accountId!!)
        }
        sagaStateStore.markCompensated(sagaId)
    }
}