Slow performance due to per query latency connecting to database?

AI-generated summary

Mark.Wahl reported slow Discourse page loads (~1 second) despite PostgreSQL queries completing in under 10ms. Logging showed high ActiveRecord overhead (e.g., 439.7ms), prompting questions about ORM inefficiency or misconfiguration. mpalmer suggested memory swapping as a possible cause, but Mark confirmed ample free RAM (6GB+) and low container usage (2GB). sam asked for server specs and install method; Mark clarified he uses the official Docker image with PostgreSQL and Redis on separate SSD servers, while Discourse runs on bare metal with HDD and a Xeon E3-1230 v6. sam advised checking Mini Profiler and suspected network latency, noting that even 10ms per query adds up quickly. Mark re-enabled Mini Profiler and discovered a 13.6ms ping between Discourse and its database/Redis servers — far from the expected 0.1ms intra-datacenter latency. sam pointed out that 77 queries per admin topic page at 13.6ms each would account for ~1 second of delay. Mark agreed and considered the issue resolved, attributing the slowdown to network latency rather than ORM or database performance.

I have installed discourse and noticed 1 second page loads in some cases. I enabled logging of queries on postgresql and noticed all queries executed in less than 10 milliseconds. However, looking at the /var/www/discourse/log/production.log file (I used docker exec to view this log). I noticed a couple logs that looked something like this:
Completed 200 OK in 724ms (Views: 20.1ms | ActiveRecord: 439.7ms)

Can anybody with knowledge advice on how the ORM framework can carry such overhead. Could i possibly be missing a configuration setting?

That smells… swappy. AR does a lot of memory work to convert data between formats, and if you’re a bit tight on memory, it could end up touching lots of pages that need to be swapped in.

It is running on a 16 GB instance and has over 6 GB of free ram available at the moment. Also the instance is using 2 GB RAM at the moment inferred from docker stats.

What are the technical specs of the server? Is this bare metal or cloud?

Are you using our official install ?

I am using the official docker image, but i an running both PostgreSQL and Redis on independent servers. The discourse container is running on Bare metal, HDD, Intel(R) Xeon(R) CPU E3-1230 v6 @ 3.50GHz, X86_64 architecture. The PostgreSQL and Redis instances are however running on SSD servers.

What does mini profiler show on home page when you drill in. This can be networking. You have to look at logging on the app, not in pg.

If you are paying 10ms round trip tax in every query it adds up super fast

Oh, i ended up disabling it as i thought it could be the cause of the issue following this guide:
https://meta.discourse.org/t/rack-mini-profiler-showing-up-in-production-for-admins/14735

I will re-enable it and advice.

Thanks for the latency hint. I all along thought the PostgreSQL server was in the same data center. I just noticed both the Redis server and PostgreSQL server are both 13.6 ms ping time away from the discourse containers. That is unacceptable. Will look into improving this. I had assumed it was around 0.1 ms which is typical when they are in the same data center.

Given we will run 77 queries for admins on a topic page, well that is about 1 second of your delay right there.

Very true. I consider this issue resolved.