Deploying a model

From a trained pipeline to a prediction endpoint: batch versus real-time serving, versioned deployments and how to roll back.

Lesson 59 of 6610 minBeginner

You will learn to

  • Choose between batch scoring and real-time serving for a given use case
  • Explain why a deployment points at one fixed, versioned model
  • Plan a rollback before you need one

Before this lesson

Intuition

A trained model on your laptop is like a recipe in a notebook. Deploying it is opening the restaurant: other people and other programs can now order, and they expect the same dish every time.

Two decisions shape almost every deployment.

How will predictions be requested?

  • Batch: you hand over a whole file and get a file back. Good when the rows are known in advance and nobody is waiting, for example a weekly score for every customer. It is simple and cheap, because nothing runs between jobs.
  • Real time: another program sends one row (or a few) and needs the answer in a fraction of a second. Good when a person or a process is waiting, for example a card payment being approved. It needs a service that is always running, and you must watch its speed and errors.

What exactly is being served? A deployed model should be a fixed thing with a name and a version number. When you want to ship a better model, you create a new version and move the endpoint to it. You never edit the old one. That makes two things possible: you can always say which model made a given prediction, and you can go back in seconds.

Picture it

Your appEndpointchurn-prodv1v2v3activeRollback: point back at v2Each version is a fixed file. It never changes after it is created.
An endpoint is a stable address. Behind it sits one fixed version. Moving forward or back means pointing the endpoint at another version.

Your application only knows the endpoint name. It does not care that version 3 replaced version 2 last Tuesday, and it does not need to change when you roll back.

The math

Deployment has little algebra, but two numbers matter. The first is latency, the time for one request. Averages hide trouble, so we report percentiles. The p50 (median) is the time that half of the requests beat. The p95 is the time that 95% of requests beat. For a sorted list of nn latencies, take the value at position ⌊q n⌋\lfloor q\,n \rfloor (counting from 0), with q=0.5q = 0.5 for p50 and q=0.95q = 0.95 for p95.

The second is throughput, the number of rows scored per second. For batch jobs, run time is roughly

time≈rowsrows per second\text{time} \approx \frac{\text{rows}}{\text{rows per second}}

where rows per second depends on the model and the machine. Measure it on a sample before you promise a deadline.

Worked example

A telecom company has 120,000 customers. The retention team calls the 5,000 most likely to leave once a week. The model and its features change at most monthly.

QuestionAnswerWhat it implies
Who is waiting for the score?Nobody; the team reads a list on MondayBatch is enough
How many rows per job?120,000A single file
How fresh must inputs be?Up to one week oldNo need for per-request calls
Cost of an always-on servicePaid all week for one jobBatch is cheaper

Suppose a test on 1,000 rows shows the model scores 2,000 rows per second (an illustrative number; measure your own). Then the whole file takes about 120,000 / 2,000 = 60 seconds, so even a very small machine meets a Monday-morning deadline.

Now change the case: the same company wants to show a retention offer to a customer while they are on the cancellation page. Someone is now waiting, the inputs include what the customer just did, and the answer is needed in well under a second. That needs a real-time endpoint, and a latency budget, for example "p95 below 200 ms".

Finally, plan the way back. The team deploys version 3 on a Tuesday. By Wednesday the error rate has risen from near 0% to 4%. Because version 2 is still stored unchanged, the rollback is one action: point the endpoint back at version 2. No retraining, no waiting. The team then investigates version 3 calmly.

How mlflow does it

  • Endpoints and slugs. A deployment has an endpoint slug: 3 to 64 characters, lowercase letters, digits and dashes. Slugs must be unique on the installation.
  • Access modes. Choose who may call it: Workspace members, API key, or Public (any signed-in user). Only a workspace admin can choose Public.
  • Versions. A deployment is backed by a versioned pipeline or registered model version. The Versions tab lists each version with its SHA-256 hash and lets you roll back to an older one in the same family. The endpoint reloads the model on the next prediction.
  • Both serving styles. The Test tab has Test a prediction (real time, JSON rows) and Batch predict (upload a CSV, Excel or Parquet file and download the result).
  • Your own infrastructure. The deployment kit exports a container for Docker, AWS, Azure, Google Cloud or Kubernetes if you prefer to serve elsewhere.

The deploy and test guide, linked under Read in the docs, walks through each step.

Common mistakes

  • Using real-time serving when batch would do. An always-on service adds cost, latency targets and failure modes. Ask who is waiting before you choose.
  • Deploying a model without a way back. If the only copy of the old model is gone, a bad release becomes an outage. Keep earlier versions and know the rollback step before you ship.
  • Testing only with clean rows. Real requests have missing values and odd formats. Send a few messy rows to the Test tab before real traffic arrives.
  • Making an endpoint public by default. A model can leak information about its training data and costs compute. Use the narrowest access mode that works.
  • Treating deployment as the finish line. The world keeps changing after launch. Plan monitoring, covered in the next lessons, at the same time.

Try it in mlflow

Deploy a model and send it a test prediction

  1. Open Deployments and choose Deploy a model, or open a run and deploy its best model candidate.
  2. Pick an Endpoint slug (lowercase letters, digits and dashes) and an access mode, then choose Deploy.
  3. Open the endpoint, go to the Test tab and send a row in the Test a prediction box.
  4. Open the Versions tab to see which version is active.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. A retailer wants a score for each of its 200,000 customers once a week, to choose who gets an offer. Which serving style fits best?
  2. 2. Why does an mlflow deployment point at one specific model version instead of at 'the latest model'?
  3. 3. After a new version goes live, the error rate rises sharply. What is the fastest safe first step?