[Python] How can I reduce the startup time of FastAPI

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm building a Retrieval-Augmented Generation (RAG) application using Python + FastAPI. I'm also running a Kafka consumer alongside the FastAPI app to process incoming text and generate embeddings (stored in FAISS). However, I'm facing a major issue — the startup time for both the Kafka worker and FastAPI app is very high (90–100 seconds). I tried optimizing it using FastAPI’s lifespan event to offload warm-up tasks (model loading, vector store loading, etc.) into the background, which did reduce it slightly to ~85–88 seconds — but it's still too slow for practical use. Here's my question:

    what should i do to reduce the startup time of my kafka worker and fastapi app

    here's the code snippet for reference:

    I'm trying to defer heavy initialization during FastAPI startup using lifespan like this:

    @asynccontextmanager
    async def lifespan(app: FastAPI):
    logger.info("Starting up RecallAI...")
    start = time.time()

    async def warmup():
    logger.info("Warming up models and vector store...")
    get_embeddings_model()
    get_llm()
    get_vectorstore()
    logger.info(f"Warmup complete in {time.time() - start:.2f} seconds")

    asyncio.create_task(warmup())
    yield
    logger.info("Shutting down RecallAI...")


    This helps, but not enough — the app still takes a long time to become responsive.

    I think the possible reasons are:


    1. Most of the startup delay seems to come from store_embeddings() and model/vectorstore loading.


    2. Even importing store_embeddings in the Kafka worker takes multiple seconds.


    3. I'm using langchain, FAISS, and HuggingFace for embeddings.

    FastAPI app + lifespan logic:

    @cache
    def get_embeddings_model():
    return HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")

    @cache
    def get_llm():
    return ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model_name="llama-3-70b")

    def get_vectorstore():
    embeddings = get_embeddings_model()
    return FAISS.load_local("img_vector_store", embeddings, allow_dangerous_deserialization=True)


    Here's the Kafka worker:

    from recall_ai.vector_embeddings.store_vector_embedding import store_embeddings

    def insert_batch(batch):
    try:
    res = store_embeddings()
    ...


    Even importing store_embeddings takes 90 seconds. This function internally loads models and creates FAISS indexes, which contributes to the delay.

    Kafka worker still taking upto 90sec to start

    Continue reading...

Compartilhe esta Página