in reply to Bid data but need fast response time

Maybe something a simple as this is all you need? This loads a dictionary into a hash as dataset, and can service 100,000 requests from each of 4 concurrent clients at a rate of ~40,000 requests per second.

Note: This is a cut-down version of a server that expects each client to make many requests via a persistent connections. If your clients would only make a single request for each connection, I'd use a thread pool architecture, but I expect the throughput to be at least as good.

#! perl -slw use strict; use threads ( stack_size => 4096 ); use IO::Socket; use constant { SERVERIP => '127.0.0.1', SERVERPORT => 3000, MAXBUF => 4096, }; sub s2S{ my( $p, $h ) = sockaddr_in( $_[0] ); $h = inet_ntoa( $h ); "$h:$p"; } my %DB :shared; chomp, $DB{ $_ } = $. while <>; close *ARGV; my $lsn = IO::Socket::INET->new( LocalHost => SERVERIP, LocalPort => SERVERPORT, Reuse => 1, Listen + => SOMAXCONN ) or die $!; print "Listening..."; while( my $client = $lsn->accept ) { async { while( 1 ) { $client->recv( my $in, MAXBUF ); unless( length $in ) { print "Disconnected from ", s2S $client->peername; shutdown $client, 2; close $client; last; }; print "Received $in from ", s2S $client->peername; my( $cmd, @args ) = split ' ', $in; if( $cmd eq 'FETCH' ) { $client->send( $DB{ $args[ 0 ] } ); } else { $client->send( 'Bad command' ); } } }->detach; } sleep 1e9;

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Bid data but need fast response time
by Anonymous Monk on May 21, 2012 at 15:25 UTC
    Very nice. I suppose I could have my web-script check that the process is running and start it if it isn't running yet. (I might even keep your "sleep" so that my server processes expire every so often as a memory-leak counter-measure.)

    Quick question -- what is the ":shared" in:

    my %DB :shared;

    I expect somewhere between 1 and 100 simultaneous clients. Each connection should last no more than 1sec with 10k+ queries.

      I might even keep your "sleep" so that my server processes expire every so often as a memory-leak counter-measure.)

      You'll probably want to set to something less than 31 years then :)

      I expect somewhere between 1 and 100 simultaneous clients. Each connection should last no more than 1sec with 10k+ queries.

      Then I'd go for a thread pool server something like this (again, a cut down version of pre-existing test code, not a production ready code):

      #! perl -slw use strict; use threads; use Thread::Queue; use IO::Socket; use constant { SERVERIP => '127.0.0.1', SERVERPORT => 3000, MAXBUF => 4096, }; our $R //= 8; sub s2S{ my( $p, $h ) = sockaddr_in( $_[0] ); $h = inet_ntoa( $h ); "$h:$p"; } my %DB :shared; chomp, $DB{ $_ } = $. while <>; close *ARGV; my $Q = new Thread::Queue; my $Qcleanup = new Thread::Queue; sub responder { my $tid = threads->tid; while( my $fileno = $Q->dequeue() ) { print "[$tid] Servicing fileno: $fileno"; open my $client, '+<&=', $fileno or die $!; bless $client, 'IO::Socket::INET'; while( 1 ) { $client->recv( my $in, MAXBUF ); unless( length $in ) { print "Disconnected from ", s2S $client->peername; shutdown $client, 2; close $client; $Qcleanup->enqueue( $fileno ); last; }; print "Received $in from ", s2S $client->peername; my( $cmd, @args ) = split ' ', $in; if( $cmd eq 'FETCH' ) { $client->send( $DB{ $args[ 0 ] } ); } else { $client->send( 'Bad command' ); } } } } threads->create( \&responder )->detach for 1 .. $R; my $lsn = IO::Socket::INET->new( LocalHost => SERVERIP, LocalPort => SERVERPORT, Reuse => 1, Listen + => SOMAXCONN ) or die $!; my @clients; print "Listening..."; while( my $client = $lsn->accept ) { my $fileno = fileno( $client ); $clients[ $fileno ] = $client; print "[0] queing ", $fileno; $Q->enqueue( $fileno ); close $clients[ $Qcleanup->dequeue ] while $Qcleanup->pending; }

      In a test running 8 responders, it served 1000 responses to each of 100 concurrent clients and achieved an average response time at the clients of 0.002 seconds.

      That's clients and server running in the same box so no network latency. But on the other hand, that's 8 server threads and 100 client threads all running in the same box which will obviously adversely affect server responsiveness.

      Here's a graph of its response times using 8 threads to respond to 16, 32, 64, 100, & 128 concurrent clients.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      I see - it is an attribute. I shoulda RTFM.