Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

During a long data migration the service should stop receiving traffic but must not be restarted. How do you do this in code with Spring Boot availability states?

We publish an AvailabilityChangeEvent with ReadinessState.REFUSING_TRAFFIC before the migration starts and ReadinessState.ACCEPTING_TRAFFIC when it ends. The readiness probe then fails during the migration, so Kubernetes takes the pod out of the Service, while liveness stays CORRECT, so Kubernetes has no reason to restart it. Any bean can read the current state by injecting ApplicationAvailability.

Why this question is asked

Long data migrations, cache rebuilds and index reloads are common in real services, and they put the application in an awkward middle state: the process is healthy, but it should not answer user requests until the work is done. If we handle this badly, either users hit half-migrated data or Kubernetes decides the pod is dead and restarts it in the middle of the migration. Interviewers ask this to see whether we understand that readiness and liveness are separate levers, and whether we know Spring Boot's API for pulling them. In this answer, we will recap the availability states, write the code that switches readiness around the migration, make it safe when the migration fails, and look at how to read the state and test it.

1A Quick Recap of Availability States

Spring Boot keeps two availability states for every application. LivenessState says whether the internal state is working (CORRECT) or broken beyond repair (BROKEN). ReadinessState says whether the application should receive traffic (ACCEPTING_TRAFFIC) or not (REFUSING_TRAFFIC). Boot exposes them through /actuator/health/liveness and /actuator/health/readiness, which Kubernetes calls as its liveness and readiness probes.

The two probes cause different actions. A failing readiness probe removes the pod from the Service's endpoints, the list of addresses that get traffic, and nothing else happens; once the probe passes again, the pod is added back. A failing liveness probe makes the kubelet kill and restart the container. So for our migration we want exactly one of them to change: readiness off, liveness untouched.

The states change through application events. An AvailabilityChangeEvent carries a new state, and Boot's ApplicationAvailability bean listens for these events and remembers the latest state of each kind. The health indicators behind the probe endpoints read from that bean.

2Switching Readiness Around the Migration

The code is short. We inject the ApplicationEventPublisher, which every Spring context provides, and publish the two events around the work.

Java
@Service
class OrderDataMigration {
private static final Logger log = LoggerFactory.getLogger(OrderDataMigration.class);
private final ApplicationEventPublisher events;
private final MigrationSteps steps;
OrderDataMigration(ApplicationEventPublisher events, MigrationSteps steps) {
this.events = events;
this.steps = steps;
}
public void run() {
AvailabilityChangeEvent.publish(events, this, ReadinessState.REFUSING_TRAFFIC);
// GET /actuator/health/readiness -> 503 {"status":"OUT_OF_SERVICE"}
// GET /actuator/health/liveness -> 200 {"status":"UP"}
log.info("migration started, pod taken out of traffic");
steps.copyAndTransformRows(); // takes 20 minutes
AvailabilityChangeEvent.publish(events, this, ReadinessState.ACCEPTING_TRAFFIC);
// GET /actuator/health/readiness -> 200 {"status":"UP"}
log.info("migration finished, pod back in traffic");
}
}
Step throughWhat Kubernetes sees during the migration
KubeletSpring Boot appService endpoints
publish REFUSING_TRAFFIC, start migration
GET /actuator/health/readiness
503 OUT_OF_SERVICE
remove pod from endpoints
GET /actuator/health/liveness
200 UP, so no restart
migration done, publish ACCEPTING_TRAFFIC
GET /actuator/health/readiness
200 UP
add pod back to endpoints

10 messages in order. Play them, or step through them one at a time.

The static method publish(publisher, source, state) wraps the state in an event and publishes it. There is also a shorter form, AvailabilityChangeEvent.publish(context, state), that takes the ApplicationContext itself as both publisher and source; either works. The moment the first event is published, the readiness health indicator reports OUT_OF_SERVICE, which Boot maps to HTTP 503. After a couple of failed probes, Kubernetes removes the pod from the Service.

Notice that we never touched liveness. It stays CORRECT, so the liveness probe keeps returning 200 and the kubelet has no reason to restart the container, however long the migration takes. This is the whole point of keeping the two states separate.

3What If the Migration Fails?

The simple version has a gap. If copyAndTransformRows() throws an exception, the second event is never published, and the pod stays out of traffic forever while looking perfectly alive. Nobody restarts it and nobody routes to it, so the failure is silent. We should decide on purpose what a failed migration means.

Java
public void run() {
AvailabilityChangeEvent.publish(events, this, ReadinessState.REFUSING_TRAFFIC);
try {
steps.copyAndTransformRows();
AvailabilityChangeEvent.publish(events, this, ReadinessState.ACCEPTING_TRAFFIC);
} catch (RuntimeException e) {
log.error("migration failed", e);
if (steps.dataIsStillUsable()) {
AvailabilityChangeEvent.publish(events, this, ReadinessState.ACCEPTING_TRAFFIC); // serve the old data
} else {
AvailabilityChangeEvent.publish(events, this, LivenessState.BROKEN); // let Kubernetes restart us
}
throw e;
}
}

Here we either go back to serving when the old data is still fine, or we declare the application BROKEN so Kubernetes restarts it and the migration can be retried from the start. Which is right depends on the migration. What matters is that a failure does not leave the pod in a state that nobody notices. An alert on pods that stay not ready for longer than the expected migration time is a good safety net either way.

4Reading the Current State With ApplicationAvailability

Other parts of the application sometimes need to know the state, for example a scheduled job that should not run while the migration holds the data. We inject ApplicationAvailability, which Boot registers as a bean.

Java
@Component
class NightlyReport {
private final ApplicationAvailability availability;
NightlyReport(ApplicationAvailability availability) { this.availability = availability; }
@Scheduled(cron = "0 0 2 * * *")
void build() {
if (availability.getReadinessState() == ReadinessState.REFUSING_TRAFFIC) {
return; // skip while the migration runs
}
// ... build the report
}
}
// availability.getLivenessState() -> CORRECT
// availability.getReadinessState() -> REFUSING_TRAFFIC during the migration

We can also react to changes instead of polling, with an @EventListener method that takes AvailabilityChangeEvent<ReadinessState> as its parameter. Boot calls it every time the readiness state changes.

5Things to Check Before Relying on This

There is a catch with many replicas. If every pod runs the migration at the same moment, every pod refuses traffic and the Service has no endpoints at all, which is an outage. For a shared migration we should run it in one pod only, for example guarded by a database lock, or in a separate Kubernetes Job, and let the other pods keep serving.

Also, readiness only has an effect if the probe actually uses it. On Kubernetes Boot enables the probe groups automatically; elsewhere we need management.endpoint.health.probes.enabled=true, and the Kubernetes manifest must point the readinessProbe at /actuator/health/readiness. Finally, if the migration runs at startup inside an ApplicationRunner, we do not need any events: Boot only switches readiness to ACCEPTING_TRAFFIC after all runners have finished. In that case a startup probe with a long enough window protects the pod from liveness failures during a long start.

6Follow-up questions

How would you test this in a Spring Boot test?

In a @SpringBootTest with probes enabled, I would call run() on a migration whose step blocks on a latch, then call /actuator/health/readiness with MockMvc or the test client and expect 503, and /actuator/health/liveness and expect 200. After releasing the latch I would expect readiness to return 200. Asserting on ApplicationAvailability.getReadinessState() is a quicker unit-level check.

Why not just return 503 from our own controllers during the migration?

Because the pod would still receive traffic and every request would fail. Readiness takes the pod out of the Service, so other pods handle those requests, or callers get a clear 'no endpoints' signal instead of errors from a pod that knows it is not ready.

Does readiness affect a rolling deployment?

Yes. A Deployment rollout waits for new pods to become ready before removing old ones. A new pod that runs a 20 minute migration with readiness refused will hold the rollout for 20 minutes, so progressDeadlineSeconds must allow for it.

7Conclusion

In this answer, we saw that Spring Boot's availability states let us take a pod out of traffic without getting it restarted. We publish ReadinessState.REFUSING_TRAFFIC before the migration and ACCEPTING_TRAFFIC after it, liveness stays CORRECT the whole time, and ApplicationAvailability lets other beans read the current state. We also made the failure path explicit and noted that a migration shared by all replicas should run in one place, so the Service never loses every endpoint.

What to say in the interviewAbout 80 seconds aloud

Open with

I would use Spring Boot's availability states, because they separate the two things Kubernetes cares about. Before the migration I publish AvailabilityChangeEvent.publish with ReadinessState.REFUSING_TRAFFIC. The readiness probe at /actuator/health/readiness then returns 503 with OUT_OF_SERVICE, and Kubernetes removes the pod from the Service endpoints, so it stops getting traffic.

Then, if they want more

I do not touch liveness. It stays CORRECT, the liveness probe keeps returning 200, and the kubelet has no reason to restart the pod even if the migration takes twenty minutes. When the migration finishes, I publish ReadinessState.ACCEPTING_TRAFFIC and the pod goes back into rotation.

I would wrap it in a try block so a failure is a deliberate decision: either go back to accepting traffic if the old data is still usable, or publish LivenessState.BROKEN so the pod is restarted, plus an alert on pods that stay not ready too long.

Other beans that need the state, such as a scheduled job, can inject ApplicationAvailability and call getReadinessState. And if every replica would run the migration at once, I would move it to a single pod or a Kubernetes Job, otherwise the Service ends up with no endpoints at all.

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 documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference, Actuator
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 "During a long data migration the service should stop receiving traffic but must not be...": https://scrivio-interview-library.netlify.app/spring-boot/actuator/893af8719c/