1) Gazelle is a fast PSGI/Plack HTTP server written in Perl and C code.
2) Benchmarks show it can handle 3x more requests per second than other servers for simple applications.
3) Its speed comes from optimizations like using accept4, writev system calls, and being written mostly in fast C code via XS.
4. Gazelle #とは
• Plack Handler / PSGI Server
• HTTP/1.0 Web Server
• Preforking Architecture
• Suitable for running application servers
behind a reverse proxy
• Starlet compatible / hot deploy
• Fast Fast Fast
8. Gazelle はなぜ速い
• Only Support HTTP/1.0 and does not
support KeepAlive. It make code very simple
• Mostly written in XS
• Ultra fast HTTP processing using
picohttpparser
• Use accept4(2)
• Use writev(2) for output responses
9. Simple HTTP/1.0 GET
accept4(2)
read(2)
parse_header
poll(2)
complete?
OK
execute app
writev(2)
poll(2)
written?
close(2)
No
OK No
10. Mostly written in XS
accept4(2)
read(2)
parse_header
poll(2)
complete?
OK
execute app
writev(2)
poll(2)
written?
close(2)
No
OK No
XS
XS
11. Perl code using XS
while (1) {
if ( my ($fd, $buf, $env) = accept_psgi(
fileno($listen_sock), $timeout, $listen_sock_is_tcp,
$host || 0, $port || 0
) ) {
my $guard = guard { close_client($fd) };
$res = Plack::Util::run_app $app, $env;
my $status_code = $res->[0];
my $headers = $res->[1];
my $body = $res->[2];
write_psgi_response($fd, $timeout, $status_code, $headers, $body);
}
12. Perl code using XS
while (1) {
if ( my ($fd, $buf, $env) = accept_psgi(
fileno($listen_sock), $timeout, $listen_sock_is_tcp,
$host || 0, $port || 0
) ) {
my $guard = guard { close_client($fd) };
$res = Plack::Util::run_app $app, $env;
my $status_code = $res->[0];
my $headers = $res->[1];
my $body = $res->[2];
write_psgi_response($fd, $timeout, $status_code, $headers, $body);
}
23. 高速なサーバを書くには
• write XS, minimize Perl code
• reduce system calls
• Zero Copy
24. 高速なAppサーバって必要なの?
• ISUCON :)
• Social Games, AdTech, SNS
• High optimized applications
• few msec ~ few tens of msec
• Several hundreds of request/sec/host
• 1PVあたりの利益が小さいサービス