LLD Guide · Module 2: Problem prep · Lesson 02.12
Prep: Deployment Pipeline Engine
Ordered stages, one active deployment per pipeline, rollback and cancel, and a history that stays in order.
By Cruscible Engineering · Published
Five people press "deploy" on the same pipeline at once. One deployment starts; the other four get a clear "already running". Everything else in this problem is a state machine.
This lesson prepares you for Build a Deployment Pipeline Engine. It doesn't solve it.
1. What the deployment pipeline contract asks for
A pipeline is a name and an ordered list of stages. A deployment of a version starts at the first stage and advances one stage at a time until it completes, or is rolled back or cancelled. A pipeline runs at most one deployment at a time. Everything is persisted (Postgres and Redis are recommended; you may use either or both).
| Method | Returns / does |
|---|---|
createPipeline(name, stages) | creates the pipeline, or overwrites one with the same name |
triggerDeployment(pipeline, version) | a new deployment id; throws IllegalArgumentException for an unknown pipeline, IllegalStateException if one is already running |
getDeploymentStatus(id) | id, pipelineName, version, currentStage, status, startedAt, completedAt; null if unknown |
advanceStage(id) | true and the next stage, or COMPLETED after the last one; false unless IN_PROGRESS |
rollback(id) | true and ROLLED_BACK; false unless IN_PROGRESS |
cancelDeployment(id) | true and CANCELLED; false once the deployment has finished |
getHistory(pipeline, limit) | up to limit deployments, most recent first |
Figure: One pipeline, four stages: a deployment that completes, one rolled back, and a second trigger
- triggerDeployment("prod", "v1.0.0") starts IN_PROGRESS at the first stage, build.
- Each advanceStage moves one stage. From the last stage it completes, and currentStage stays "deploy".
- rollback at staging: ROLLED_BACK, with completedAt set. The pipeline is free for a new deployment.
- While a deployment is IN_PROGRESS, a second trigger throws IllegalStateException.
2. Lessons to read before the deployment pipeline
- 01.05 State machines: the deployment lifecycle, and "one active deployment per pipeline" as an invariant.
- 01.04 Postgres for LLD or 01.03 Redis for LLD: whichever store holds the claim on a pipeline must make "check and claim" one step.
3. Deployment pipeline design decisions to make first
- Does a new deployment start
PENDINGorIN_PROGRESS? The lifecycle listsPENDING, but read the example walkthrough: what does the status show right aftertriggerDeployment? - What makes "at most one active" one step? Five threads trigger the same pipeline at once: exactly one returns an id and four get
IllegalStateException. Where does the claim live, and what makes taking it atomic? - When is the claim released? Completing, rolling back and cancelling all end a deployment. Missing one of the three blocks the pipeline forever.
- What is
currentStageafter the end? The contract says it's the last stage reached. - What orders the history? Deployments triggered in the same millisecond have the same
startedAt. What still tells you which came first? - What does overwriting a pipeline do?
createPipelineon an existing name replaces its stages. Decide what happens to its history and to a deployment already running. - If you use both stores, which one is the truth? Two copies of a deployment's state have to agree after every call.
4. Deployment pipeline pitfalls
⚠️ Trap: the exception types are part of the contract. An unknown pipeline is
IllegalArgumentException, a busy one isIllegalStateException. A different exception from a busy pipeline is counted as a wrong answer.
⚠️ Trap: checking "is anything running?" and then inserting a deployment in a second step lets two triggers both start.
⚠️ Trap:
advanceStageandrollbackreturnfalseunless the deployment isIN_PROGRESS, butcancelDeploymentreturnsfalseonly once it has finished.
⚠️ Trap:
completedAtisnullwhile a deployment is running, and set by all three ways it can end.
Before you submit
- A new deployment is
IN_PROGRESSat the first stage, withstartedAtset - An unknown pipeline throws
IllegalArgumentException; a running deployment makes the next trigger throwIllegalStateException - Concurrent triggers on one pipeline start exactly one deployment
advanceStagemoves one stage at a time, and completes after the last one withcurrentStagestill the last stage- A single-stage pipeline completes on the first
advanceStage advanceStageandrollbackreturnfalseunlessIN_PROGRESScancelDeploymentworks while running and returnsfalseonce finished- Completing, rolling back and cancelling all set
completedAtand free the pipeline - Deployments on different pipelines don't block each other
getHistoryis most recent first, respectslimit, and is empty for a pipeline with no deployments- All state is persisted
Further reading
- Partial indexes (PostgreSQL docs)
- Explicit locking (PostgreSQL docs)
SETwithNXandPX(Redis docs)
After you've tried it
Submit first. The full walkthrough of one solution: Building a Deployment Pipeline Engine with Rollback Support.