Performance
Six-second page loads from a database at 15% CPU
Our internal BI tool had been “slow” for months. The database looked healthy, and the app servers were nearly idle. The problem was the distance between them, multiplied.
- Stack
- Metabase, ECS Fargate, Amazon RDS for PostgreSQL, CloudWatch Logs Insights
- My role
- Investigation and the migration plan
- Outcome
- The cause was traced to cross-region round trips and a starved connection pool. A phased fix is planned.
What people saw
Dashboards took seconds to open, and even the home page was slow. Everything we normally check looked fine. App task CPU was under 1% and memory under 20%. Database CPU was 8–18%, and the server had hundreds of free connections.
Following one request
With debug logging on, the app records three things for each request: how long it took, how many calls it made to its own application database, and how full the connection pool was. For the endpoint that loads the current user:
- about 16 calls to the application database
- each one taking 0.5 to 1.1 seconds
- a median of 6–10 seconds for a request that should be instant
I then ran the same kinds of queries on the database server itself, read-only. They took 0.02–0.5 ms.
Distance, times sixteen
The app ran in Mumbai. Its application database was in Oregon, about 280 ms away per round trip. The pool also tested each connection when it was handed out, which added another round trip per call. Sixteen calls in a row over that distance takes seconds before any real work starts.
And the pool was always full
Each app task has 15 connections to its application database. The pool was full during about 70% of API requests. About 40 connected data sources were set to sync their metadata every hour, and each sync took 10–46 minutes. One took almost two hours. Background syncs held the pool almost all the time, so user requests waited behind them.
The plan
In order of payoff:
- Move the application database into the app’s region. It’s only 5.4 GB, so a dump and restore fits in a maintenance window. Each of those round trips drops from ~280 ms to around a millisecond.
- Split the scheduler. The tasks that serve users stop running syncs, and one dedicated task runs them.
- Sync large data sources daily instead of hourly, and make the pool bigger.
- Clean up the old instance. Most of its disk was a 1.6 TB database that nothing had connected to in a long time.
Each phase has its own commands, checks before and after, and a rollback, written down before anything changes.
What I took from it
- When server metrics look healthy but the app is slow, the app is usually waiting on the network, a lock or a pool.
- Count round trips before tuning anything.
- Measure on the server and at the client. The gap between the two numbers is the diagnosis.