473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Asynchronous I/O or multiple threads?

Hi all,

I'm designing a fairly simple service that will run on W2K/SP4 and W2K3
servers. It's job is to be a very specialized database server that listens
on a given IP address / TCP port and handles multiple connections. Client
programs will make a connection and pass text strings to the service, which
will then return a value for each of the strings. It's unlikely the service
will ever have more than 100 simultaneous connections and most of what it
will do is a lookup in a table for each string. We've considered MSDE for
this and for a number of reasons it's impractical for us to use it.

In researching various ways of handling simultaneous connections I've seen
examples using multithreading and others using async I/O. I'd like the
program to scale well but I don't want to overwhelm the server. I'm looking
for guidance on choosing between the two methods.

Alternately, I'd be happy to use a commercial database server if I could
find one that can be distributed easily and royalty free and which doesn't
cost too much. Something like the VistaDB database engine would be ideal but
it can't run as a service.

Thoughts / suggestions?

Nov 16 '05 #1
6 4405
Quiet Man <qu******@mailinator.com> wrote:
I'm designing a fairly simple service that will run on W2K/SP4 and W2K3
servers. It's job is to be a very specialized database server that listens
on a given IP address / TCP port and handles multiple connections. Client
programs will make a connection and pass text strings to the service, which
will then return a value for each of the strings. It's unlikely the service
will ever have more than 100 simultaneous connections and most of what it
will do is a lookup in a table for each string. We've considered MSDE for
this and for a number of reasons it's impractical for us to use it.

In researching various ways of handling simultaneous connections I've seen
examples using multithreading and others using async I/O. I'd like the
program to scale well but I don't want to overwhelm the server. I'm looking
for guidance on choosing between the two methods.

Alternately, I'd be happy to use a commercial database server if I could
find one that can be distributed easily and royalty free and which doesn't
cost too much. Something like the VistaDB database engine would be ideal but
it can't run as a service.

Thoughts / suggestions?


Given that MSDE is the most obvious choice IMO, it would help if you
could state the reasons you have for not using it.

In terms of handling simultaneous connections, asynchronous IO is
likely to scale better, but be warned that you'll end up using system
threadpool threads automatically - you need to be aware of this, as if
you start doing other things which require the threadpool within
threads that are already running in the pool, you can easily deadlock
(by having all the threadpool threads waiting until another threadpool
thread is available before they can do any more work).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Quiet Man <qu******@mailinator.com> wrote:
I'm designing a fairly simple service that will run on W2K/SP4 and W2K3
servers. It's job is to be a very specialized database server that listens
on a given IP address / TCP port and handles multiple connections. Client
programs will make a connection and pass text strings to the service, which will then return a value for each of the strings. It's unlikely the service will ever have more than 100 simultaneous connections and most of what it
will do is a lookup in a table for each string. We've considered MSDE for
this and for a number of reasons it's impractical for us to use it.

In researching various ways of handling simultaneous connections I've seen
examples using multithreading and others using async I/O. I'd like the
program to scale well but I don't want to overwhelm the server. I'm looking for guidance on choosing between the two methods.

Alternately, I'd be happy to use a commercial database server if I could
find one that can be distributed easily and royalty free and which doesn't
cost too much. Something like the VistaDB database engine would be ideal but it can't run as a service.

Thoughts / suggestions?


Given that MSDE is the most obvious choice IMO, it would help if you
could state the reasons you have for not using it.

In terms of handling simultaneous connections, asynchronous IO is
likely to scale better, but be warned that you'll end up using system
threadpool threads automatically - you need to be aware of this, as if
you start doing other things which require the threadpool within
threads that are already running in the pool, you can easily deadlock
(by having all the threadpool threads waiting until another threadpool
thread is available before they can do any more work).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

----------------

Thanks for the reply Jon.

Regarding MSDE - one big reason is the complexity of deployment. This app
will be installed on servers that may or may not already have MSDE 1.0, MSDE
2000, MSDE 2000A, SQL 7 or one of the SQL 2000 engines (personal, developer,
standard, enterprise) installed already for use by other apps - this makes
the deployment process / installation program a bear. Another reason is
size - the rest of the app is looking like about 4 MB of code and the MSDE
is 45MB or so. Lessor reasons include the throughput uncertainty that comes
with the workgroup governor (which has bitten us in the past) and resistance
on the part of some network / server admins due to past security issues with
the SQL 2000 engine. Still lessor reasons are the thought that MSDE is
overkill and that a specialized app will have a smaller memory footprint and
possibly be quicker.

All that being said I do like the MSDE and have used it successfully in the
past.

Thanks for the async I/O scalability note. I also appreciate your pointing
out the threadpool consideration. I'm hoping it won't be a problem in that
99% of what this service will be doing is looking up values in a global
table in read-only mode. The table will peak at about 300,000 string / value
pairs, average string length of 8 characters. The data will be refreshed
once per day or less. I may have to look into limiting the number of
connections that the service will accept.

Quiet Man

Nov 16 '05 #3
Hi,

I may have missed the point here, but why do you need a database at all for
such a simple task ?

Cheers

Doug Forster

"Quiet Man" <qu******@mailinator.com> wrote in message
news:eQ**************@TK2MSFTNGP10.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Quiet Man <qu******@mailinator.com> wrote:
I'm designing a fairly simple service that will run on W2K/SP4 and W2K3
servers. It's job is to be a very specialized database server that
listens
on a given IP address / TCP port and handles multiple connections. Client
programs will make a connection and pass text strings to the service,

which
will then return a value for each of the strings. It's unlikely the

service
will ever have more than 100 simultaneous connections and most of what it
will do is a lookup in a table for each string. We've considered MSDE for
this and for a number of reasons it's impractical for us to use it.

In researching various ways of handling simultaneous connections I've
seen
examples using multithreading and others using async I/O. I'd like the
program to scale well but I don't want to overwhelm the server. I'm

looking
for guidance on choosing between the two methods.

Alternately, I'd be happy to use a commercial database server if I could
find one that can be distributed easily and royalty free and which
doesn't
cost too much. Something like the VistaDB database engine would be ideal

but
it can't run as a service.

Thoughts / suggestions?


Given that MSDE is the most obvious choice IMO, it would help if you
could state the reasons you have for not using it.

In terms of handling simultaneous connections, asynchronous IO is
likely to scale better, but be warned that you'll end up using system
threadpool threads automatically - you need to be aware of this, as if
you start doing other things which require the threadpool within
threads that are already running in the pool, you can easily deadlock
(by having all the threadpool threads waiting until another threadpool
thread is available before they can do any more work).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

----------------

Thanks for the reply Jon.

Regarding MSDE - one big reason is the complexity of deployment. This app
will be installed on servers that may or may not already have MSDE 1.0,
MSDE
2000, MSDE 2000A, SQL 7 or one of the SQL 2000 engines (personal,
developer,
standard, enterprise) installed already for use by other apps - this makes
the deployment process / installation program a bear. Another reason is
size - the rest of the app is looking like about 4 MB of code and the MSDE
is 45MB or so. Lessor reasons include the throughput uncertainty that
comes
with the workgroup governor (which has bitten us in the past) and
resistance
on the part of some network / server admins due to past security issues
with
the SQL 2000 engine. Still lessor reasons are the thought that MSDE is
overkill and that a specialized app will have a smaller memory footprint
and
possibly be quicker.

All that being said I do like the MSDE and have used it successfully in
the
past.

Thanks for the async I/O scalability note. I also appreciate your pointing
out the threadpool consideration. I'm hoping it won't be a problem in that
99% of what this service will be doing is looking up values in a global
table in read-only mode. The table will peak at about 300,000 string /
value
pairs, average string length of 8 characters. The data will be refreshed
once per day or less. I may have to look into limiting the number of
connections that the service will accept.

Quiet Man

Nov 16 '05 #4
100 threads should be not problem. Some use <= 1000 threads without issue
(however 1000 is pushing it imo.) You could fire up new threads upto to
some max, say 500, then block new requests with server busy reply. You
could also use your own thread pool (many existing example out there such as
Codeproject) with some max and have ready threads ready to go. These
servers are much easier to create and reason about. Async servers may scale
better, but are harder to code and get right and have the potential thread
pool blocking issue. If you have like 10,000 active requests, then async is
the option, but if you really have that many then the server will melt
anyway or be so slow that almost nothing gets done. So your back to
factoring out load between other servers - which you could do with either
approach. Therefore, the threaded server may start to look better again.
If your not blocking alot in your processing piece (i.e. just doing cached
table lookups) then potentially you could service all requests with *one*
thread using a queued approach. Queue up all requests and service the queue
with one tight server loop and send the reply. Your replies could be put
into yet another queue that is serviced by one-to-N threads (say your own
thread pool or the system thread pool) so that one reply can't block the
server. For that matter, your receiver side could be 1-N threads also that
waits for and builds the complete requests - so one slow client does not do
a DoS on your server. Each Client-Listener thread would put completed
request into the server IN-Q. The one server thread reads from IN-Q,
processes, and writes reply to OUT-Q. The Out thread pool handles the
socket sends and socket closing (UDP may work better for this single
request/reply deal). Even with TCP, this would work well for simple
request/reply where a reply signals the end of the session. I tend to favor
the threaded server or queued server approach (others may vary.) hth

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Quiet Man" <qu******@mailinator.com> wrote in message
news:en**************@TK2MSFTNGP14.phx.gbl...
Hi all,

I'm designing a fairly simple service that will run on W2K/SP4 and W2K3
servers. It's job is to be a very specialized database server that listens
on a given IP address / TCP port and handles multiple connections. Client
programs will make a connection and pass text strings to the service, which will then return a value for each of the strings. It's unlikely the service will ever have more than 100 simultaneous connections and most of what it
will do is a lookup in a table for each string. We've considered MSDE for
this and for a number of reasons it's impractical for us to use it.

In researching various ways of handling simultaneous connections I've seen
examples using multithreading and others using async I/O. I'd like the
program to scale well but I don't want to overwhelm the server. I'm looking for guidance on choosing between the two methods.

Alternately, I'd be happy to use a commercial database server if I could
find one that can be distributed easily and royalty free and which doesn't
cost too much. Something like the VistaDB database engine would be ideal but it can't run as a service.

Thoughts / suggestions?


Nov 16 '05 #5
Thanks for your thoughts William. It helps to have some guidelines regarding
what a reasonable number of threads is, etc. I obviously haven't done a
great deal of multithreaded programming.

I'm comfortable with limiting the service to accepting 100 simultaneous
connections. A typical load on the server will be less than 1000 connections
per hour; each connection will pass (in one big lump) an average of 200 text
strings that will be looked up. If I go with a multithreaded approach my
current thought is that each thread will take one of these connections,
lookup values for each of the text strings, return all the values and at
that point terminate the connection (so yes, the reply indicates the end of
the session). The thread would then be put back into the thread pool for
reuse. All the table data (probably 300,000 rows) will stay in RAM so
lookups should be extremely quick.

I might be able to pull this off with a single threaded app but it seems
better to plan for future growth now. I haven't done any performance testing
yet but I can't imagine that the actual lookup process will take much time -
I'm guessing that most of the time will be occupied by making the connection
and transferring the data between the client and the server.

I'll have to study up on the response queue idea.

Again, thanks. You've given me plenty to think about.

------------------------------

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:#R*************@TK2MSFTNGP12.phx.gbl...
100 threads should be not problem. Some use <= 1000 threads without issue
(however 1000 is pushing it imo.) You could fire up new threads upto to
some max, say 500, then block new requests with server busy reply. You
could also use your own thread pool (many existing example out there such as
Codeproject) with some max and have ready threads ready to go. These
servers are much easier to create and reason about. Async servers may scale
better, but are harder to code and get right and have the potential thread
pool blocking issue. If you have like 10,000 active requests, then async is
the option, but if you really have that many then the server will melt
anyway or be so slow that almost nothing gets done. So your back to
factoring out load between other servers - which you could do with either
approach. Therefore, the threaded server may start to look better again.
If your not blocking alot in your processing piece (i.e. just doing cached
table lookups) then potentially you could service all requests with *one*
thread using a queued approach. Queue up all requests and service the queue
with one tight server loop and send the reply. Your replies could be put
into yet another queue that is serviced by one-to-N threads (say your own
thread pool or the system thread pool) so that one reply can't block the
server. For that matter, your receiver side could be 1-N threads also that
waits for and builds the complete requests - so one slow client does not do
a DoS on your server. Each Client-Listener thread would put completed
request into the server IN-Q. The one server thread reads from IN-Q,
processes, and writes reply to OUT-Q. The Out thread pool handles the
socket sends and socket closing (UDP may work better for this single
request/reply deal). Even with TCP, this would work well for simple
request/reply where a reply signals the end of the session. I tend to favor
the threaded server or queued server approach (others may vary.) hth

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Quiet Man" <qu******@mailinator.com> wrote in message
news:en**************@TK2MSFTNGP14.phx.gbl...
Hi all,

I'm designing a fairly simple service that will run on W2K/SP4 and W2K3
servers. It's job is to be a very specialized database server that listens
on a given IP address / TCP port and handles multiple connections. Client
programs will make a connection and pass text strings to the service, which will then return a value for each of the strings. It's unlikely the service will ever have more than 100 simultaneous connections and most of what it
will do is a lookup in a table for each string. We've considered MSDE for
this and for a number of reasons it's impractical for us to use it.

In researching various ways of handling simultaneous connections I've seen
examples using multithreading and others using async I/O. I'd like the
program to scale well but I don't want to overwhelm the server. I'm looking for guidance on choosing between the two methods.

Alternately, I'd be happy to use a commercial database server if I could
find one that can be distributed easily and royalty free and which doesn't
cost too much. Something like the VistaDB database engine would be ideal but it can't run as a service.

Thoughts / suggestions?


Nov 16 '05 #6
Just got a chance to check the group again. Many thanks! I'll be examining
the code and trying it out as well tomorrow.

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
Sound good. Here is a little tcp server and client (see attached zip of
both projects) I just worked up for a simple example. I use XmlSerializer
to serialize a type to send and receive. This is overkill if you just need
to send one string and return one string, but is helpful if you needs are
more then just passing one string. Just got it to the point of working
without a lot of debugging or error handling, but should be a good start.
The main server loop is below:

private void ProcessStart()
{
listener = new TcpListener(lep);
listener.Start();
Socket s = null;

while( IsStarted )
{
try
{
s = listener.AcceptSocket();
}
catch(System.Net.Sockets.SocketException sx)
{
if ( ! IsStarted )
break; // We Stopped server.
Console.WriteLine("Server Error:"+sx.Message);
isStarted = false;
}
MyWorker newWorker = new MyWorker(s, this);
newWorker.Start();
Console.WriteLine("Server received new request.");
}
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Quiet Man" <qu******@mailinator.com> wrote in message
news:OO*************@TK2MSFTNGP11.phx.gbl...
Thanks for your thoughts William. It helps to have some guidelines regarding what a reasonable number of threads is, etc. I obviously haven't done a
great deal of multithreaded programming.

....

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Zunbeltz Izaola | last post by:
Hi, I have the following problem. I'm developing a GUI program (wxPython). This program has to comunicate (TCP) whit other program that controls a laboratory machine to do a measurement. I...
48
by: Steve - DND | last post by:
I'm trying to determine if I need to make my application multi-threaded, or if it can be done with asynchronous programming. I realize that asynch calls do create a new thread in the background,...
1
by: Julian Hershel | last post by:
Reading about asynchronous programming (ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconasynchronousdesignpatterno verview.htm) I could not clarify some doubts. Hope you can help me. 1) Are...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
0
by: MalamisuraE | last post by:
I need to write a class that will process a qued list of items, each item could take a long or short time based on the items. I want to use Asynchronous Callback Methods to do this instead of...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
4
by: Morgan Cheng | last post by:
Since ASP.NET 2.0, asynchronous web service client can be implemented with event-based pattern, instead of original BeginXXX/EndXXX pattern. However, I didn't find any material about event-based...
0
by: alan | last post by:
Hello all, I'd like to ask recommendations about a "good" generic asynchronous I/O library for C++. By generic I mean, something I can use even on files, stdin/stdout, and sockets. I've seen...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.