Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

H2O: Final reduction of the number of database connections - #3751

Merged
NateBrady23 merged 1 commit into
TechEmpower:masterfrom
volyrique:database
May 18, 2018
Merged

NateBrady23 merged 1 commit into
TechEmpower:masterfrom
volyrique:database

Conversation

@volyrique

Copy link
Copy Markdown
Contributor

No description provided.

@sebastienros

Copy link
Copy Markdown
Contributor

No way, they are already too fast, it's unfair 😄

@michaelhixson

Copy link
Copy Markdown
Contributor

@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 fortune.c, it seems like it's doing the same general sequence of things as most other frameworks but with lower-level APIs. Maybe that's all it is - every little step of the implementation is faster?

@volyrique

volyrique commented May 18, 2018 •

Copy link
Copy Markdown
Contributor Author

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:

  • fully asynchronous database commands - the program never blocks unless absolutely necessary
  • efficient load balancing among the worker threads with SO_REUSEPORT - this is especially important for thread-local database connection pools (more on that later), so that one never ends up in the situation in which one pool is fully saturated, while another one is idle
  • prepared statements - created once when a database connection is established
  • minimizing the number of system calls - libh2o uses writev(), so all fortunes are sent with one system call
  • avoiding memory allocations and copies as much as possible
  • extensive usage of the memory pool functionality provided by libh2o - every HTTP request has an associated memory pool, and all allocations belonging to the pool are freed together in one go after the request is completed; note that it is not possible to override the memory management routines used by the database client library, libpq, for example, so not every allocation is a pooled one
  • natural merge sort as the sorting algorithm - takes roughly linear time if the input is already almost sorted
  • memcmp() as the string comparison function - no Unicode normalization, locale-specific issues, etc. (but keep in mind that UTF-8 is used as per the requirements)

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 malloc() and free(), and as a result there are thread-local memory pools (and the per-request pools are yet another layer on top of them), so there is less thread contention inside malloc(), for example.

@NateBrady23
NateBrady23 merged commit a7e9fb7 into TechEmpower:master May 18, 2018
@bhauer

bhauer commented May 18, 2018 •

Copy link
Copy Markdown
Contributor

@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.

fortunes

@volyrique

Copy link
Copy Markdown
Contributor Author

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).

@bhauer

bhauer commented May 18, 2018

Copy link
Copy Markdown
Contributor

@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.

@volyrique

volyrique commented May 19, 2018 •

Copy link
Copy Markdown
Contributor Author

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 frameworks/C/h2o/start-servers.sh script are the following:

  • set NUM_WORKERS to 2
  • set all occurences of USE_PROCESSES to true
  • set all occurences of DB_CONN to 2

The Web application was started in the standard way with Docker and the following parameters:
tfb --mode debug --test h2o
I captured the database communication with tcpdump because the toolset validation functionality doesn't really cover it:
tcpdump -w fortunes.pcap tcp port 5432

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):

root@ed68eaaf334f:/# wrk -H 'Host: 10.0.0.1' -H 'Accept: application/json,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,*/*;q=0.7' -H 'Connection: keep-alive' --latency -d 1 -c 512 --timeout 8 -t 28 "http://127.0.0.1:8080/fortunes"
Running 1s test @ http://127.0.0.1:8080/fortunes
  28 threads and 512 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    36.97ms   17.28ms  84.20ms   78.68%
    Req/Sec   482.45    123.84     1.36k    90.75%
  Latency Distribution
     50%   32.15ms
     75%   41.42ms
     90%   68.70ms
     99%   82.28ms
  14087 requests in 1.10s, 18.77MB read
  Non-2xx or 3xx responses: 4
Requests/sec:  12821.33
Transfer/sec:     17.09MB

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:

tcpdump -r fortunes.pcap 2>/dev/null | grep "length 44" | wc -l
14589
tcpdump -r fortunes.pcap 2>/dev/null | grep "length 939" | wc -l
14585

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.

@bhauer

bhauer commented Jun 10, 2018

Copy link
Copy Markdown
Contributor

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants