-
Hello everyone, I am a beginner in databases. If I use Postgres as a checkpointer in a production environment, how should I correctly use Langgraph in my API when using a web framework? (I am using FastAPI)
If I have multiple different graphs for different API, should they use the same checkpointer? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Hi neither the graph nor the checkpointer keep any internal state so using one global instance or a new one per request makes no difference. What id definitely recommend doing is ensuring you create a global connection pool for postgres and use that to get a connection to pass to the checkpointer on each request. That will ensure connections are reused, which saves latency |
Beta Was this translation helpful? Give feedback.
-
@nfcampos Thank you for your reply. It seems we only need one instance and don't need to create a new graph and obtain a new connection for each request. Right? I read this official document I saw that the document used an If all my graphs share the same This brings me back to the initial question: do I need multiple If we only need one Is the purpose of using Sorry for asking so many questions; I'm not very familiar with database applications. |
Beta Was this translation helpful? Give feedback.
-
According to the official documentation, the way to create and use a PostgresSaver is as follows: DB_URI = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
with PostgresSaver.from_conn_string(DB_URI) as checkpointer:
# call .setup() the first time you're using the checkpointer
checkpointer.setup()
... However, I cannot do this within a route function because creating a new checkpointer instance on every request prevents retrieving the stored conversation history via the config. My question is: Can I create the checkpointer within a lifespan function instead? For example: @contextmanager
def lifespan(_: FastAPI):
with PostgresSaver.from_conn_string(DB_URI) as checkpointer:
checkpointer.setup()
yield {"checkpointer": checkpointer} In this case, would the entire FastAPI app use only a single connection, and would this connection remain unreleased until the app shuts down? I understand that using a connection pool is a good approach, but besides that, are there any other alternatives? |
Beta Was this translation helpful? Give feedback.
Hi neither the graph nor the checkpointer keep any internal state so using one global instance or a new one per request makes no difference. What id definitely recommend doing is ensuring you create a global connection pool for postgres and use that to get a connection to pass to the checkpointer on each request. That will ensure connections are reused, which saves latency