Why scoring is a stateless service and training never touches it
Training is a batch job that wants memory and time. Scoring is a request that can't wait for either.
The problem
ml-service has two jobs that look related but behave nothing alike: scoring candidates for a live home feed request, and training/evaluating models offline from ClickHouse data. Scoring is in the critical path — it needs to return in milliseconds, every time. Training is a long-running batch job that wants to use as much memory and CPU as it can get, for as long as it needs. Running both in the same process means one can starve the other.
Approaches considered
One process, one model, both roles. ml-service handles scoring requests and kicks off training jobs itself. Least infrastructure, and the failure mode is direct: a memory-heavy dataset materialization job running in the same process as live scoring causes scoring latency to spike for reasons that have nothing to do with scoring.
Separate processes, shared codebase. Split into a scoring binary and a training binary from the same repo, deployed separately but still coupled in how they load models. Isolates the runtime but the model-loading and promotion logic still needs to be kept in sync by hand.
Fully separate roles: stateless scoring service, independent training pipeline. Scoring holds no state beyond the currently loaded champion pointer and never runs training code. Training runs as its own pipeline (the saga in the next post) and communicates a new champion only through MLflow.
Trade-offs
Full separation means champion promotion has to reach a running, separately-deployed scoring process without a restart — coordination that a single shared process wouldn’t need. It also means scoring can’t shortcut anything by reaching into training internals; every interaction goes through the MLflow model registry.
The decision
The online role of ml-service is stateless: receive hydrated features over gRPC, score with the currently loaded champion, return. It owns no database, and knows nothing about how the features it receives were computed or how the champion was trained.
Why
Isolating the two roles means a slow or memory-heavy training run can never tail-latency a live request — they simply don’t share a process to contend over. It also means the training saga (next post) can run for however long a multi-hour pipeline needs without any coupling to request-serving uptime. The cost — coordinating a hot-swap instead of just updating a variable in-process — is a fixed, well-understood cost, in exchange for eliminating an entire class of latency spikes that would otherwise be intermittent and hard to attribute.
Implementation
# ml-service — the online scoring server. No training imports,
# no ClickHouse client, no state beyond the loaded model pointer.
class ScoringServicer(scoring_pb2_grpc.ScoringServiceServicer):
def __init__(self):
self.champion_model = self._load_current_champion()
def ScoreCandidates(self, request, context):
features = np.array([
[c.social_distance, c.interaction_score, c.recency_hours]
for c in request.candidates
])
scores = self.champion_model.predict(features)
return scoring_pb2.ScoreResponse(scores=scores.tolist())
def _load_current_champion(self):
latest = mlflow.tracking.MlflowClient().get_latest_versions(
"home-feed-ranker", stages=["Production"]
)[0]
return mlflow.lightgbm.load_model(latest.source)
def reload_champion(self):
# called only via a promotion webhook — never by anything
# this process runs itself.
self.champion_model = self._load_current_champion()
# Deployed as its own process group, scaled independently of
# whatever training pipeline happens to be running that day.
kubectl scale deployment ml-service-scoring --replicas=6
kubectl scale deployment ml-service-training --replicas=1