5.3.6 · D1MLOps & Deployment

Foundations — Model serving (REST APIs, FastAPI)

2,349 words11 min readBack to topic

The parent note (Model serving) throws a lot of words at you at once: function, inference, request, REST, HTTP method, JSON, endpoint, Pydantic, ASGI, latency, throughput. If any of those made you blink, this page is for you. We define every single one from nothing, in the order that lets each rest on the previous.


1. A model is a function f(x) → y

The parent writes . Read the arrow as "produces". Here:

  • = the input, also called features — the numbers describing one thing you want a prediction about (e.g. a house's size and age).
  • = the output, the prediction (e.g. the predicted price, or a class label like 1).
Figure — Model serving (REST APIs, FastAPI)

Why the topic needs this: the whole point of serving is to let other programs call this . If you don't picture the model as a plain input→output box, the rest ("expose the function over a network") is fog. See also f.x1, f.x2 for how individual features sit inside .


2. Inference = "running the machine"

Why the topic needs this: serving is inference-as-a-service. Every request that arrives triggers exactly one inference.


3. Network, client, server, request, response

Before "REST" or "HTTP" mean anything, you need the cast of characters.

Figure — Model serving (REST APIs, FastAPI)

Picture a mailbox on a door (the parent's Feynman image): the client drops a note (request), the server reads it, solves it, and puts the answer back (response).

Why the topic needs this: "model serving" literally means being the server for a model. The word serve = "answer requests." Every later term (endpoint, status code, latency) describes some part of this send/answer loop.


4. HTTP and its "methods"

Every HTTP request carries a method — a single verb saying what kind of action you want:

Method Plain meaning Mailbox picture
GET "Read me something" Peeking at a poster already on the wall
POST "Here is data, please process it" Dropping a filled-in form to be worked on
PUT "Replace this thing" Swapping the poster for a new one
DELETE "Remove this thing" Tearing the poster down

Why the topic needs this: the method is the first word the server reads; it decides which handler runs. Pick the wrong verb and you fight the whole web's assumptions.


5. JSON — the shape of the message

Why the topic needs this: both the request body ({"x1":..., "x2":...}) and the response ({"pred":1, "prob":0.87}) are JSON. It is the common tongue of the mailbox.


6. Serialize / deserialize

These two words appear in the parent's latency formula, so we define them now.

Why the topic needs this: a network can only carry bytes, never live Python objects. So every request is deserialized on arrival and every response is serialized on the way out — two of the five stages in the parent's latency budget.


7. REST — the architectural style

Now that request/response/HTTP/JSON exist, "REST" is easy.

Why the topic needs this: REST is the convention FastAPI implements. "Endpoint," "status code," and "JSON body" are all REST vocabulary.


8. Endpoint, status code


9. FastAPI, Pydantic, Uvicorn, ASGI

The parent's tooling — each defined plainly.

Why the topic needs this: these four are the moving parts of the parent's HOW section. Knowing which does what stops you from, say, expecting FastAPI to open a socket (that's Uvicorn's job).


10. Latency and throughput — the two speed words

Figure — Model serving (REST APIs, FastAPI)

Worked check (parent's numbers): 4 workers, 50 ms each → req/s. Halve inference to 25 ms → req/s. Halving latency doubled throughput. ✓


Prerequisite map

Function f maps x to y

Inference runs the model

Network with client and server

Request and Response

HTTP and methods GET POST

JSON message format

Serialize and Deserialize

REST style stateless resources

Endpoint and Status codes

Model Serving

Latency pipeline sum

Throughput equals workers over latency

FastAPI Pydantic Uvicorn ASGI


Equipment checklist

Hide the answers; if you can state each without peeking, you're ready for the parent note and for Docker & Containerization, gRPC vs REST, CI-CD for ML, and Model Monitoring & Drift.

A model is fundamentally a …
function that maps an input to one output.
Inference means …
running a frozen, already-trained model to get a prediction (not learning).
A client is …
any program that sends a request asking the server to do something.
A server is …
the computer/program holding the model and waiting to answer requests.
A request/response pair is …
the message sent to the server and the answer sent back.
HTTP is …
the grammar computers use to phrase web requests and responses.
GET vs POST: …
GET reads a fixed thing (no big body); POST submits data to be processed.
JSON is …
plain text of key–value pairs every language can read, e.g. {"x1":2.0}.
Serialize / deserialize means …
turn an object into text to ship / turn text back into an object.
REST is …
a style using resources + HTTP methods + stateless requests.
Stateless means …
the server remembers nothing between requests; each carries all it needs.
An endpoint is …
a specific URL path + method the server answers, like POST /predict.
Status codes 2xx / 4xx / 5xx mean …
success / client's fault / server's fault.
FastAPI does …
maps Python functions to REST endpoints and handles JSON in/out.
Pydantic does …
validates incoming JSON against your declared schema, rejecting bad input.
Uvicorn does …
owns the network socket and actually speaks HTTP (the web server).
ASGI is …
the interface letting one Python app handle many requests concurrently.
Latency is …
time for ONE request (a pipeline sum of its stages).
Throughput is …
requests finished per second .