Using Lock in Web Applications

A few months ago I interviewed to work inside the company I currently work for, I do not have a strong background for web development, but one of the questions he asked me was how you could improve this block of code.

I don’t remember the code block perfectly, but to sum it up, it was a web hit counter and it used a lock on hitcounter.

lock(HitCounter) { // Bla... } 

However, after some discussion, he said that the castle is good, but never uses it in web applications!

What is his expression? Why shouldn't I use blocking in web applications?

+6
multithreading locking
source share
7 answers

There is no particular reason why locks should not be used in web applications. However, they should be used with caution because they are a serialization mechanism for multi-threaded access that can cause a lock if locks are blocked. However, this is not just a web application issue.

What you should always remember is that on modern hardware, an unprotected lock takes 20 nanoseconds to flip . With this in mind, the usual practice of trying to make code inside lock blocks minimal should be followed. If you have a minimal code inside the block, the overhead is quite small and potential for competition.

To say that locks should never be used is a slightly complete application. It really depends on your requirements, for example. a thread-safe in-memory cache that should be used in conjunction with queries can potentially result in less query blocking than on-demand fetching from a database.

Finally, the BCL and ASP.Net Framework types certainly use locks internally, so you indirectly use them anyway.

+6
source share

Application domain can be redesigned.

This can cause the old appdomain to still serve some requests, and the new appdomain to also serve new requests.

Static variables are not shared between them, so locking on a static global will not provide exclusivity in this case.

+6
source share

First of all, you never want to block an object that you are actually using in any application. You want to create a lock object and lock this:

 private readonly object _hitCounterLock = new object(); lock(_hitCounterLock) { //blah } 

Regarding the web part of the question, when do you block the blocking of every thread that is trying to access an object (which for the Internet can be hundreds or thousands of users). They will all wait until each thread in front of them is unlocked.

+2
source share

Late :), but for future readers of this, an additional point:

If the application is running in a web farm, ASP running on multiple computers will not use the lock object

So this can only work if 1. There is no need to support the web farm and 2. ASP is configured (not the default) DO NOT use parallel instances during reuse until the old requests are submitted (as mentioned in Andras above )

+2
source share

This code will create a bottleneck for your application, since all incoming requests will have to wait at that moment before the previous one exits the block.

0
source share
Lock

It is intended only for use in multi-threaded applications, where several threads require access to the same shared variable, so the lock is performed exclusively by the requesting thread, and all incomplete threads will block and wait until the lock is released.

user requests are isolated in web applications, so no lock is required by default

0
source share

A couple of reasons ...

  • If you try to block the read / write operation of the database, there is a very high risk of a race condition in any case, since the database does not belong to the process that is performing the lock, so it can be read from / written by another process - perhaps even a hypothetical future version of IIS. which starts several processes for each application.

  • Locks are commonly used in client applications for threads other than the UI, i.e. background / worker threads. Web applications do not have such great benefits for multi-threaded processing if you are not trying to take advantage of several cores (in this case, locks on objects related to requests will be acceptable), since each request can be launched on its own server, and the server does not can answer until it processes all the output (or at least the sequential chunk ) anyway.

0
source share

All Articles