I wanted to share a recent experience I had with crystal-pg that might be helpful for others.
I’ve been benchmarking Crystal against Go and noticed that my Crystal implementation was performing 5-10 times slower than the Go version when running the same SQL query.
My initial tests with SQLite showed comparable performance to Go, so the bottleneck was clearly with PostgreSQL. I spent a lot of time tweaking connection parameters (retry_attempts, pool_size, max_connections, etc.) and even tried compiling with -D preview_mt, but nothing seemed to close the significant performance gap.
On a whim, I added sslmode=disable to my connection string, and the results were instantaneous and dramatic. The performance shot up, bringing it nearly on par with the Go implementation.
I haven’t had the chance to dig into why enabling SSL has such a massive performance impact with crystal-pg, but the difference is night and day.
If you’re running into similar performance issues with PostgreSQL, I highly recommend giving this a try. It might just be the solution you’re looking for. I’d be curious to hear if anyone else has encountered this or has any insights into the cause.
That’s a good find. I wonder, did you try playing with the Crystal version and this to see if there was some change with Crystal that would have affected this?
Before trying Go, or diving into code changes I actually played with Crystal versions. Tried 1.14.0 1.16.0 1.17.1 and master. Had no luck, hence I pushed forward with building a reproducible version with Go and narrowed it down to the driver. It wasn’t easy though
Regardless of whether the Go comparison module uses TLS: It’s very surprising that toggling TLS makes a significant difference in crystal-pg.
With modern hardware and software, TLS shouldn’t have much overhead over unencrypted traffic.
Your findings suggest that something appears to be not working as performant as it should be expected.