Skip to content

jainal09/fastapi-gracefulshutdown-websockets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b327a16 Β· Jun 25, 2024

History

21 Commits
Mar 18, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Mar 18, 2024
Mar 18, 2024
Jun 25, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 10, 2024
Apr 9, 2024

Repository files navigation

FastApi Graceful Shutdowns with Websockets

Code style: black Dependency Management: Poetry Formatter and Linter: Ruff License: MIT python: 3.11

Header Image

This project demonstrates how to implement graceful shutdowns using FastAPI and kubernetes, that uses websockets client connections and internal background queues.

Blog Post

I have written a blog post on this project. You can read it here. In this blog post, I have explained the importance of graceful shutdowns and how to implement them in FastAPI with websockets.

Overview

When deploying a new version of an application, it is essential to ensure that no tasks are lost during the deployment process. This is especially important for applications that use websockets to maintain real-time connections with clients. In such cases, abruptly terminating the application can lead to data loss and client disconnections.

Solution

Here we have a FastAPI server, deployed in a pod of Kubernetes with min-replicas set to 1 for simplicity. It has a rest API to send tasks, a WebSocket interface for sending task status events to the clients, and a signal listener that listens to pod termination signals sent by Kubernetes. Through the rest API, a task is created and enqueued to the inbuilt fast-api background queue and is later processed by the worker. Once, the task is processed the client is notified via WebSocket. The custom-implemented signal listener listens to all the SIGTERM signals send by the Kubernetes controller when a pod termination happens. This is the watcher that steers the entire graceful shutdown part.

Features

βœ… Ensure zero task loss during deployments

πŸ”— Keep existing clients connected while preventing new connections to a terminating pod

πŸ“£ Provide real-time feedback to clients through WebSocket notifications

🚦 Mitigate race conditions between pod termination and task creation

Installation

  1. Clone the repository:

    git clone https://github.com/jainal09/fastapi-gracefulshutdown-websockets.git
  2. Install the dependencies:

    You will need poetry to install the project dependencies.

    poetry install --no-dev # for production
    # or
    poetry install # for development

Usage

  1. Start the FastAPI application:

    python main.py
  2. Open your web browser and navigate to http://localhost:8000/docs to access the application.

Kubernetes

  1. Deploy the application to a Kubernetes cluster:

    kubectl apply -f k8s/deployment.yaml
  2. Access the application at http://localhost:30000/docs.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

Linting and Formatting

  1. Lint the code using ruff:

    ruff check --fix .

Type Checking

  1. Run the type checker:

    mypy .

Docker

  1. Image available on Docker Hub: jainal09/grs-fast-api

  2. Build the Docker imag locally:

docker build -t jainal09/grs-fast-api:v9 .

Newsletter

Scale Bites Image

If you like my content, please subscribe to my newsletter Scale Bites. I will keep you updated on the latest articles, projects, and cool stuff I am working on. Subscribe here.

License

This project is licensed under the MIT License.