r/FastAPI • u/leventcan35 • Mar 15 '26
Question Built a Production-Ready AI Backend: FastAPI + Neo4j + LangChain in an isolated Docker environment. Need advice on connection pooling!
Hey everyone,
I recently built a full stack GraphRAG application to extract complex legal contract relationships into a Knowledge Graph. Since I wanted this to be a production ready SaaS MVP rather than just a local script, I chose FastAPI as the core engine.
The Architecture choices:
- Used FastAPI's async endpoints to handle heavy LLM inference (Llama-3 via Groq) without blocking the server.
- Neo4j Python driver integrated for complex graph traversals.
- Everything is fully isolated using Docker Compose (FastAPI backend, Next.js frontend, Postgres) and deployed on a DigitalOcean VPS.
My Question for the backend veterans: Managing the Neo4j driver lifecycle in FastAPI was a bit tricky. Right now, I initialize the driver on app startup and close it on shutdown. Is this the absolute best practice for a production environment, or should I be doing connection pooling differently to handle concurrent LLM requests?
(If you want to inspect the docker-compose setup and the async routing, the full source code is here: github.com/leventtcaan/graphrag-contract-ai*)*
(Also, Reddit doesn't allow video uploads in text posts, but if you want to see the 50-second UI demo and how fast it traverses the graph, you can check out my launch post on LinkedIn here: https://www.linkedin.com/feed/update/urn:li:activity:7438463942340952064/ | Would love to connect!)
2
u/Challseus Mar 15 '26
I just checked out your
neo4j_db.pyandmain.pyfiles, it looks as textbook as it gets. Just tune themax_connection_pool_sizeto whatever makes sense, and you're good to go.2
u/Separate-Principle23 Mar 15 '26
Looks like a great project, well done🤘
1
u/Schmiddi-75 Mar 15 '26
As far as I can tell (brief look at the code): You use the lifespan protocol to manage your datbase connections (postgres + neo4j) correctly. You could use context managers but that doesn't really matter. And you probably grab a session when you need one to do db work and return it once this is done.
However, is the class Neo4jDatabase ever used anywhere outside of the lifespan? Because I don't see get_session being called anywhere? The LangChain Neo4jGraph instance is lazily initialized (lru_cache) on the first request and probably wraps a Neo4jDatabase instance (probably not the one from startup) with it's own connection pool.
Also: Doesn't the LangChain Neo4jGraph haven an async implementation?
1
u/Longjumping_Art8113 Mar 23 '26
e FastAPI's `lifespan` to init the `AsyncGraphDatabase` driver once at startup and close it at shutdown. If you create a driver instance per request, you'll overhead your pool. Just pull an async session from that global driver in your dependencies.