Prev Next

Python / Uvicorn Fundamentals Interview Questions

What are best practices for deploying Uvicorn in a Kubernetes environment?

Kubernetes brings its own deployment patterns that intersect with Uvicorn's configuration. Getting the combination right ensures zero-downtime deployments, proper health checking, and correct signal handling.

Kubernetes + Uvicorn checklist
ConcernRecommendation
Host bindingAlways --host 0.0.0.0 so Kubernetes can route traffic to pods
WorkersSet workers based on pod CPU limits - usually 1-2 per vCPU for I/O-bound apps
Health checksImplement /health and /ready endpoints; Uvicorn has no built-in health endpoint
Graceful shutdownSIGTERM triggers Uvicorn graceful shutdown; set terminationGracePeriodSeconds > timeout-graceful-shutdown
Preemptive SIGKILLIf Uvicorn doesn't finish within terminationGracePeriodSeconds, Kubernetes force-kills
Environment configAll Uvicorn settings via UVICORN_* env vars in ConfigMap/Secret
Liveness vs readinessLiveness: is the server running? Readiness: is it ready to serve traffic?
# Kubernetes Deployment snippet
spec:
  containers:
    - name: api
      image: myapp:latest
      command: ["uvicorn", "main:app",
               "--host", "0.0.0.0",
               "--port", "8000",
               "--workers", "2",
               "--timeout-graceful-shutdown", "25"]
      ports:
        - containerPort: 8000
      env:
        - name: UVICORN_LOG_LEVEL
          value: "warning"
      livenessProbe:
        httpGet:
          path: /health
          port: 8000
        initialDelaySeconds: 5
        periodSeconds: 10
      readinessProbe:
        httpGet:
          path: /ready
          port: 8000
        initialDelaySeconds: 2
        periodSeconds: 5
      lifecycle:
        preStop:
          exec:
            command: ["sleep", "5"]  # allow load balancer to de-register

Pre-stop hook: adding a preStop sleep of 5 seconds gives Kubernetes time to remove the pod from the service endpoint list before SIGTERM is sent. This prevents new requests from being routed to a pod that is already shutting down.

Why is the Kubernetes preStop sleep hook recommended for Uvicorn pods?
What should the Kubernetes terminationGracePeriodSeconds be set relative to Uvicorn's --timeout-graceful-shutdown?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Uvicorn and what problem does it solve for Python web development? What is ASGI and how does it differ from WSGI? How do you install Uvicorn and what is the difference between the minimal and standard installations? How do you run a basic Uvicorn server from the command line? What are Uvicorn's default host, port, and log level settings? What is uvloop and how does it improve Uvicorn's performance? What are httptools and h11, and when would you choose each? How does Uvicorn's --reload flag work and what does it require? How do you run Uvicorn programmatically using uvicorn.run() and uvicorn.Config/Server? What are Uvicorn workers and how do you configure multi-process deployments? How do you deploy Uvicorn with Gunicorn for production? How do you configure SSL/TLS (HTTPS) in Uvicorn? What is the ASGI lifespan protocol and how does Uvicorn support it? How do you configure Uvicorn logging and access logs? What are proxy headers in Uvicorn and how do you configure trusted proxies? How does Uvicorn handle WebSocket connections? What is the --root-path setting in Uvicorn and when do you need it? How do you use Uvicorn with a Unix domain socket instead of a TCP port? What is the --factory flag in Uvicorn and when is it useful? How do you configure Uvicorn with environment variables? What is Uvicorn's --interface flag and what application types does it support? How do you run Uvicorn inside a Docker container? What is the difference between Uvicorn, Hypercorn, and Daphne as ASGI servers? How do you handle graceful shutdown in Uvicorn? What are the key Uvicorn settings for a production-hardened deployment? How do you run Uvicorn with systemd for automatic restarts and log rotation? What is the relationship between Uvicorn and FastAPI? How do you configure Uvicorn's timeout settings? How do you write a minimal ASGI application that works with Uvicorn without any framework? How do you configure Uvicorn's log config file for custom logging formats? What is the --app-dir flag in Uvicorn and when do you need it? How do you test a Uvicorn-served ASGI application without starting the server? What is Granian and how does it compare to Uvicorn as an ASGI server alternative? What is the scope dictionary in the ASGI protocol and what does it contain? What are best practices for deploying Uvicorn in a Kubernetes environment?
Show more question and Answers...

Tools

Comments & Discussions