channels_postgres icon indicating copy to clipboard operation
channels_postgres copied to clipboard

Refactor database layer: replace psycopg3 queries with Django ORM asy…

Open Demagalawrence opened this issue 2 months ago • 6 comments

…nc calls

Demagalawrence avatar Oct 07 '25 12:10 Demagalawrence

Just adding my 2c here, we recently re-implemented this library and we went with ORM queries as well in the first place, but they were significantly slower than have a dedication psycopg async connection, as with the ORM implementation you're constantly waiting on the main thread for queries.

This was just our experience though, YMMV.

rissson avatar Oct 29 '25 12:10 rissson

i have not understood!

Demagalawrence avatar Nov 02 '25 06:11 Demagalawrence

The Django ORM async interface relies on asgiref.sync_to_async, for example: https://github.com/django/django/blob/05ba1a9228128614fb3c475f1c4bdf0160f44dba/django/db/models/query.py#L1412. From the Django doc (https://docs.djangoproject.com/en/5.2/topics/async/#sync-to-async):

The reason [thread_sensitive=True] is needed in Django is that many libraries, specifically database adapters, require that they are accessed in the same thread that they were created in. Also a lot of existing Django code assumes it all runs in the same thread, e.g. middleware adding things to a request for later use in views.

This means that the main Django thread (usually started by an asgi server), which is a blocking thread, will be waited on to perform database queries. Hence, you reduce the throughput of channels-postgres because you're limited by the availability of the connection from the main thread.

rissson avatar Nov 03 '25 13:11 rissson

I assumed it would be slightly slower due to the translation from ORM to SQL but I wasn't aware of sync_to_async was such a bottleneck. This is a blocker @Demagalawrence

I don't see how we can merge it if it causes a big drop in performance. In the meantime, I'll focus on building a benchmark so we have a clearer picture on the performance.

danidee10 avatar Nov 03 '25 16:11 danidee10

Yeah I'm sorry I don't have a number, it just felt slower. This is definitely a "feeling" assessment, but it makes sense.

rissson avatar Nov 03 '25 16:11 rissson

Thanks for clarifying — that makes sense. I understand ORM async may reduce throughput. I’ll wait for your benchmark results. hmmmm i have understood something too , thank you so much

Demagalawrence avatar Nov 04 '25 05:11 Demagalawrence