Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Large number of database connections #264

Open
YaBatman opened this issue Jan 18, 2025 · 8 comments
Open

Large number of database connections #264

YaBatman opened this issue Jan 18, 2025 · 8 comments

Comments

@YaBatman
Copy link

Edgy version: 0.24.2
Python version: 3.12

Hi there!
I'm using EDGE in conjunction with FastAPI and PostgreSQL, as well as pgbouncer for database connection pooling. When initializing the Registry, we specify poolclass=NullPool, since we're using pgbouncer to manage connections.

However, during production and load testing, we've noticed that EDGY is generating a high number of database connections, which is causing some issues. We'd like to find a way to reduce the number of connections so that we can optimize our system's performance.

We were wondering if there are any solutions available that could help us achieve this goal. For example, could we use decorators to ensure that all EDGY calls within an endpoint are executed within the same session? This would allow us to minimize the number of connection requests and improve the overall performance of our application.

@tarsil
Copy link
Collaborator

tarsil commented Jan 18, 2025

@devkral I believe you are better to handle this scenario

@devkral
Copy link
Contributor

devkral commented Jan 20, 2025

If you don't get warnings it seems your Database objects are referenced and dereferenced all the time.
you may want to use the lifespan asgi wrapper for flask, in case you don't use it.

Otherwise check the ref count of the Database objects. It should not always drop to 0.

Please ignore this. This was wrong. It assumed that the destruction of pools causing new connections what is not the case with NullPool

@devkral
Copy link
Contributor

devkral commented Jan 20, 2025

Sry I was wrong. You use the NullPool that implied something other what I was suggesting.

You can maybe reduce the connections by using a normal Pool, databasez makes them async and threadsafe.

You probably trigger implicit loads of values by accessing uninitialized attributes on your models.

@YaBatman
Copy link
Author

But each edge query brings a new flavor to the pool of databases. However, I would like to limit this behavior and have the API request use only one connection at a time. With a large amount of traffic, this can cause too many connections to be opened.

In any case, I will try to review all our code to see if there is any unobvious usage of edge cases.

@devkral
Copy link
Contributor

devkral commented Jan 21, 2025

then eliminate the implicit loads when non-existing fields are accessed (sry we need this for deferred loading).

You can use the contextvariable MODEL_GETATTR_BEHAVIOR for this.
Set it to "passdown" and iron out the now appearing errors.

If you check a model instance for an attribute check it's dict instead of using hasattr/getatttr. This prevents triggering the load.

You can also set the model instance variable _loaded_or_deleted to True for archiving the same like MODEL_GETATTR_BEHAVIOR but you sacrifices deferred loading this way.

@devkral
Copy link
Contributor

devkral commented Jan 21, 2025

I will add a debugging guide

@devkral
Copy link
Contributor

devkral commented Jan 23, 2025

the debugging guide is here:

https://edgy.dymmond.com/debugging

Could you solve the problem?

@devkral
Copy link
Contributor

devkral commented Jan 24, 2025

can you try main? I fixed a bug which prevented subloops to close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants