473,799 Members | 3,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4448
Quiet Man <qu******@maili nator.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Quiet Man <qu******@maili nator.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.co m>
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******@maili nator.com> wrote in message
news:eQ******** ******@TK2MSFTN GP10.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Quiet Man <qu******@maili nator.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.co m>
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******@maili nator.com> wrote in message
news:en******** ******@TK2MSFTN GP14.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******** *****@TK2MSFTNG P12.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******@maili nator.com> wrote in message
news:en******** ******@TK2MSFTN GP14.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******** ******@TK2MSFTN GP10.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.Accept Socket();
}
catch(System.Ne t.Sockets.Socke tException sx)
{
if ( ! IsStarted )
break; // We Stopped server.
Console.WriteLi ne("Server Error:"+sx.Mess age);
isStarted = false;
}
MyWorker newWorker = new MyWorker(s, this);
newWorker.Start ();
Console.WriteLi ne("Server received new request.");
}
}

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

"Quiet Man" <qu******@maili nator.com> wrote in message
news:OO******** *****@TK2MSFTNG P11.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
2936
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 have a dialog box, wiht two buttoms "Start measurement" and "Stop". "Start" executes a function that do the measurement in the following way.
48
5439
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, but when they come back, they return to the thread that launched them, is that correct? If I go multi-threaded, then I just need to spawn a new thread for each new connection, right? What I'm doing here is making a service that accepts...
1
1556
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 asynchronous programming and multithreaded programming two different pictures? As I read the help topic above it is not clear to me if the design pattern opens a new thread or not to run the methods asynchronously. 2) One unique thread can run...
4
3822
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. I've read through what must be dozens of ways to do socket communication in C#, and it seems they all devolve into three basic options - Socket.Select, IOCP through a native interface, and Asynchronous callbacks. I'm fine using Asynchronous...
2
2187
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 it back to the SAME socket - the data isnt a large value (measured in bytes rather than MB or GB) i TRIED thinking of this in the Asynchronous way - BeginReceive - then pass to the OnClient Connected handler, which calls a Wait For Data
0
1198
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 creating my own threads as I am convinced this is the way to go from researching the material. For threads you can create an array of threads, but for Asynchrouness Delegates you cant since each one needs its own seperate delagate and AsyncCallback...
4
3607
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 client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with sockets before and I am struggling with something. From what I can tell the sample server code ...
4
4078
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 server side asynchronous web service. So, we can only implement asynchronous webmethod with BeginXXX/EndXXX pattern, right? I don't why ASP.NET 2.0 don't provide event-based server side pattern.
0
1274
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 boost::asio, and also ioxxx, but most of the examples I've seen with them are about sockets. Can they be used for general i/o (files, terminal)? Can anyone point me to good tutorials about using them for stuff other than sockets? My use case...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.