Repository navigation
H2O: Final reduction of the number of database connections - #3751
Conversation
|
No way, they are already too fast, it's unfair 😄 |
|
@volyrique Just curious... Why is h2o's fortunes implementation so fast? :) Do you think it's anything that other frameworks can learn from? When I read through |
|
Currently the best explanation I have is that the database connection pools are properly sized (well, it is still a work in progress). Otherwise the implementation uses pretty standard techniques:
The application depends quite a lot on various libraries for all the major pieces of functionality, so I wouldn't say that lower-level APIs are used per se; it's just that the lower-level nature of the C programming language itself means that one does not need a special reminder to think about some of the items on the list (particularly about memory allocations and copies). By design, the program has thread-local database connection pools, so that the amount of shared mutable state (database connections being an example of it) is minimized, which has performance and correctness implications (less lock contention and lower probability of race conditions, for example). In fact, each worker thread is more or less independent, and rarely needs to synchronize with another one inside the Web application code (the cache used for the cached queries test type being a notable exception, which is the reason for the disastrous results right now); obviously, I can't make any guarantees for the library dependencies of the program other than trying to use reentrant interfaces as much as possible. Furthermore, one of the major decisions I have made is to use the same configuration for all test types (I have noticed that some implementations have special configurations just for the database tests), and although I haven't done much experiments (too many values to try), it seems that the best value for the number of worker threads is the same as the number of logical processors in the system, i.e. 28 (which is the expected situation for a workload that does not touch the disk). As a result, I can vary the number of database connections only in multiples of 28. I don't think the performance of h2o is exceptional. When the runs in the Citrine environment started, h2o was fast, but not that much: It was not even in the top 10, as visible in run 8de314d3-b970-4295-a3ca-3682f3905a3b, for instance. However, at that point it was using only 1 database connection per thread (28 in total), which was a good value for the ServerCentral environment, but obviously not that good for Citrine. Previously, I noticed that the PostgreSQL variants of ULib usually performed similarly, but it seems that they have not been tuned for the current environment. I have been experimenting a lot with the pool size, and with this change I will have data for 1-6 and 8 connections per thread (again, multiply by 28 for the total number). It seems that for all tests except the updates one (which is special) the application reaches saturation around 4. I will have to wait for a run with this change, but the updates test probably peaks at 2 connections per thread, and gets worse quickly with higher values. So, I will probably push a final change that is going to be a compromise between the performance on the updates test and the rest. The other frameworks that have a global connection pool might be able to tweak that further, and obtain even better performance, assuming that they got the basics right. P.S. I forgot to mention the techniques that libh2o (which is the foundation for the whole application) used to achieve good performance - this presentation discusses some of them. One cool idea is that there is a layer on top of the system memory management functions such as |
|
@volyrique Thanks for the thorough write-up. It's a great demonstration of the amazing talent and generosity of the contributors to this project. I do not want to diminish the achievement, but as a separate matter, I'd like a sanity check that observes the network traffic using Wireshark or similar to confirm the behavior is what we want and expect. A field of very high-performance frameworks appear to converge to approximately 340K RPS on Fortunes and then a step function jumps h2o up to nearly 500K. I feel some additional due diligence is warranted, but I would be happy to have it all check out just fine. |
|
Sure, no problem, is that additional verification something that I would have to do? BTW, h2o was also performing at around 340k RPS on the fortunes test with 2 database connections per thread (refer to run 089634a5-833f-4a8b-abc3-b694244913a5, for example). |
|
@volyrique Not required, but if you have time and are interested, it would be appreciated. I'd like some sanity checking, but I wasn't sure who would have the time and when. For the time being, however, we'll just trust the results. |
|
All right, in order to obtain a reasonable amount of data, I ran a reduced configuration - 2 worker processes, each with 1 thread (which is more or less equivalent to 1 process with 2 threads) that handles 2 database connections in its pool, so 4 database connections in total. The necessary modifications to the
The Web application was started in the standard way with Docker and the following parameters: I used the same wrk command line as in the continuous benchmarking setup, except that I changed the duration to 1 s instead of 15 s (again, to keep the amount of data reasonable), and the server address to 127.0.0.1 (I was running wrk in the server container): By design the Web application established the database connections as soon as it started (and before I ran tcpdump), so I killed manually the 4 PostgreSQL processes handling the connections to force the program to repeat the whole process of connecting to the database and to make that appear in the packet capture. This was the reason there were 4 failed requests. The packet capture can be found here. You can inspect it with Wireshark, but the interesting bits are the packets with sizes 44 and 939, which are the commands from the client and the responses from the database respectively: wrk reports 14087 requests, and if we presume around 512 (the concurrency parameter) more being in progress when the time has run out, the numbers add up. There are 4 less responses from the database because of the initial failed requests I mentioned. I hope that this is sufficient as a sanity check. |
|
I didn't thank you for this detail earlier, @volyrique. I just saw you mention it from the Google Group. Thanks for digging in and verifying things and for sharing tips with the rest of us! |

No description provided.