Hello, Crystal community,
We’ve recently used Crystal to develop a Kemal-based API service in our company. It is a very simple one, having a couple endpoints; and it reads its data from a Postgres database.
We’ve hit some issues, two of which we could not find any workaround for:
When a batch of tens of requests is thrown against the service, the response times, initially small, grow all the time up to the last successfully processed request; for example, for 20 concurrent requests, response processing times (in seconds) look like this:
0.46
0.75
0.69
1.30
1.59
1.45
1.93
2.38
2.47
2.91
3.14
3.81
3.74
4.21
4.36
4.72
5.11
5.35
5.89
6.17
Monitoring the database server reveals that for the requests processed longer, at least half of the time is spent in the ClientWrite event (which is explained as “Waiting to write data to the client”, seems like the database server is ready to push data to the client but the client does something else instead of readily consuming it, at least for a while).
We would expect the times to grow slower, where the main component would be the time of processing a single request, plus a relatively small concurrency overhead.
On the contrary, times continue to grow quite fast, linearly with the number of concurrent requests, e.g. for a batch of 50 requests, the last would have times close to 15 seconds.
The other issue we could not solve or diagnose is that when errors are raised (e.g. clients time out and disconnect, or a PoolTimeout exception is raised) certain fibers are lost, never returning to the main section, where an ensure
clause decrements a counter of requests in-flight - this counter remains positive instead of going back to zero. The number of fibers lost differs but can by as big as 5 or 8 for a batch of 80 requests.
The database pool is initialized having max_idle_pool_size
equal to max_pool_size
so that all created connections are accepted back into the pool.
Has anyone experienced something similar? Any hints on what might be wrong or what we may be missing? Any idea or help is highly appreciated.