ASP Threading
So
what are ASP and COM Threading models and why do they matter? Well
IIS is a highly efficient environment to work in - but only if you
follow the threading rules.
Imagine that your web server is a store and the products you're
selling are your web pages. You have a whole bunch of visitors but
your store has lots of checkouts so you can handle all of them at
the same time. Even when there's a rush, the queues are short because
there are so many checkouts.
When you're using server-side objects things work slightly differently. It's a bit like having a till on your checkout - unless you've got a till you're not going to be able to take a sale. In the same way - unless you've got an object you're not going to be able to give your client a web page.
COM Threading Models
Objects come in different threading flavors. Some are more suitable than others for use under IIS.
Single Threaded objects are not recommended for use under IIS. Only one instance of a Single Threaded object can operate at any one time so it's a bit like having only one till between all your checkouts. No matter how many checkouts you've got, all your sales assistants have to wait their turn to use the till.
Apartment Threaded objects normally work fine under IIS. It's like giving each checkout a till. Each till can only accomodate one person but but that doesn't matter because each till is only being used by one person.
Apartment Threaded objects can cause problems when used with application or session scope. Doing this is like intentionally creating one till for the entire store - something you might want to do if you want to see a continuous total of the number of goods sold. Store assistants will have to wait for their turn at the till and your queues will build up.
Both Threaded objects avoid this problem. They're like one master till with lots of terminals. You can see all the items going through at the same time but each sales assistant can have his own terminal so your queues will be kept to a minimum.
You may also come across Free and Neutral Threaded objects. Free Threaded objects are pretty much the same as Both Threaded objects. Neutral Threaded objects are like a more flexible version of Apartment Threaded objects.
COM Threading Cheats
So it's easy? I want Apartment Threaded objects or if I want to use my objects in Application or Session scope then I want Both Threaded objects?
Well... not quite... While the threading models are a good start for knowing how your components will perform there are standard cheats which allow components to promise performance yet fail to deliver it.
Suppose I have a Single Threaded object that I want to market for use under IIS. I know this isn't going to be a terribly attractive option and I really want it to be a Both Threaded object. I've got two choices - I can rewrite it to be Both Threaded or I can cheat by hiding it inside a new object and installing a gatekeeper which only allows access to one client at a time.
Why?
So why would a developer do this? Put simply - to remove these bottlenecks can be a lot of work. Many companies developed products for Visual Basic - a one till store - and revamping for the hypermarket world of ASP is not something they want to spend a lot of money on. In fact maybe it's not even possible!
So how do you detect this kind of problem? What kinds of things should you look for?
- Components written in Visual Basic don't have to exhibit this kind of problem but it's not uncommon.
- Look for components written for Visual Basic and then retargeted for ASP. The VB component side was almost certainly not multithreaded so you should be looking for evidence of a major rewrite.
- Look for components that require or suggest use in Application or Session scope for efficiency reasons. The only reason to suggest this is as a workaround for threading problems.
However don't get obsessive - knowing your limitations is half the battle. If you want to use some cool component and it seems to be thread unfriendly, just be aware of this and use it in a way which means that you won't impact the rest of your system.
