REST API serving with FastAPI in MLOps - Time & Space Complexity
When we serve a REST API using FastAPI, we want to know how the time to handle requests changes as more requests come in.
We ask: How does the server's work grow when the number of requests increases?
Analyze the time complexity of the following FastAPI endpoint code.
from fastapi import FastAPI
app = FastAPI()
items = {"foo": "The Foo item", "bar": "The Bar item"}
@app.get("/items/{item_id}")
async def read_item(item_id: str):
return {"item_id": item_id, "description": items.get(item_id, "Not found")}
This code defines a simple API that returns an item description by its ID from a dictionary.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Dictionary lookup for the requested item ID.
- How many times: Once per API request.
Each request looks up the item ID in the dictionary. The time to find the item does not grow much as the dictionary grows.
| Input Size (number of items) | Approx. Operations per request |
|---|---|
| 10 | About 1 lookup operation |
| 100 | Still about 1 lookup operation |
| 1000 | Still about 1 lookup operation |
Pattern observation: The lookup time stays almost the same no matter how many items are stored.
Time Complexity: O(1)
This means each request takes about the same time to process, no matter how many items exist.
[X] Wrong: "The time to find an item grows as the number of items grows."
[OK] Correct: Dictionary lookups in Python are very fast and do not slow down much as the dictionary gets bigger.
Understanding how API request handling scales helps you design fast and reliable services, a key skill in real-world software development.
What if we changed the dictionary to a list and searched items one by one? How would the time complexity change?