0
0
MLOpsdevops~5 mins

REST API serving with FastAPI in MLOps - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: REST API serving with FastAPI
O(1)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Dictionary lookup for the requested item ID.
  • How many times: Once per API request.
How Execution Grows With Input

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
10About 1 lookup operation
100Still about 1 lookup operation
1000Still about 1 lookup operation

Pattern observation: The lookup time stays almost the same no matter how many items are stored.

Final Time Complexity

Time Complexity: O(1)

This means each request takes about the same time to process, no matter how many items exist.

Common Mistake

[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.

Interview Connect

Understanding how API request handling scales helps you design fast and reliable services, a key skill in real-world software development.

Self-Check

What if we changed the dictionary to a list and searched items one by one? How would the time complexity change?