Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'Lock' in web applications

A few months ago I was interviewing for a job inside the company I am currently in, I dont have a strong web development background, but one of the questions he posed to me was how could you improve this block of code.

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

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

However after some discussion he said, lock is good but never use it in web applications!

What is the basis behind his statement? Why shouldnt I use lock in web applications?

like image 733
kyndigs Avatar asked Nov 30 '10 14:11

kyndigs


People also ask

What are web locks?

Web Locks Concepts and Usage. A lock is an abstract concept representing some potentially shared resource, identified by a name chosen by the web app.

What is web locks API?

The Web Locks API is a new addition to the Web Platform which allows you to execute JavaScript in a lock, a resource which can potentially get shared with other browser tabs. This API is currently available in Chrome and other Chromium-based browsers with no major signals from other browser vendors.

When should I use lock C#?

C# lock in thread The lock keyword is used to get a lock for a single thread. A lock prevents several threads from accessing a resource simultaneously. Typically, you want threads to run concurrently. Using the lock in C#, we can prevent one thread from changing our code while another does so.

How do you lock and unlock an application object?

Right-click the object or objects, and then choose Lock.


2 Answers

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

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

As for the web portion of the question, when you lock you block every thread that attempts to access the object (which for the web could be hundreds or thousands of users). They will all be waiting until each thread ahead of them unlocks.

like image 92
Mark Avenius Avatar answered Sep 23 '22 06:09

Mark Avenius


There is no special reason why locks should not be used in web applications. However, they should be used carefully as they are a mechanism to serialize multi-threaded access which can cause blocking if lock blocks are contended. This is not just a concern for web applications though.

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

To say that locks should never be used is a bit of a blanket statement really. It really depends on what your requirements are e.g. a thread-safe in-memory cache to be shared between requests will potentially result in less request blocking than on-demand fetching from a database.

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

like image 30
Tim Lloyd Avatar answered Sep 25 '22 06:09

Tim Lloyd