Alachisoft NCache 3.8

Session Locking in NCache Session Store Provider

ASP.NET applications are inherently multithreaded. Because requests that arrive in parallel are processed on concurrent threads drawn from a thread pool, it's possible that two or more requests targeting the same session will execute at the same time. (The classic example is when a page contains two frames, each targeting a different ASPX in the same application, causing the browser to submit overlapping requests for the two pages.) To avoid data collisions and erratic behavior, the NCache Session Store Provider provides the functionality that exclusively locks the session-store item for a particular session when it begins processing the first request by calling GetItemExclusive method, causing other requests targeting the same session to wait for the lock to come free. When the request completes, the lock is released during the call to the SetAndReleaseItemExclusive method.


Because there's no harm in allowing concurrent requests to perform overlapping reads, the lock is typically implemented as a reader/writer lock-that is, one that allows any number of threads to read a session but that prevents overlapping reads and writes as well as overlapping writes.


If a locked session is encountered during the call to either the GetItemExclusive or GetItem method, it will re-request the session data at half-second intervals until either the lock is released or the amount of time specified in the ExecutionTimeout property has elapsed. If the request times out, SessionStateModule calls the ReleaseItemExclusive method to free the session-store data and request the session-store data at that time.

However, this way of freeing the session-store data may lower the performance of applications that have huge amount of requests for the same session data. It is possible that a huge number requests are waiting for same session data to be freed and only of them being able to acquire the next lock. To avoid this situation, NCache gives its users an option to specify the number of retries that SessionStateModule should make calls to either GetItemExclusive or GetItem method before returning an empty session. This number of retries can be configured by specifying "sessionLockingRetry" value in the NCache Session Store Provider section. By default Session Locking retries is disabled (i.e set to -1) and SessionStateModule will re-request the session data at half-second intervals until either the lock is released or the amount of time specified in the ExecutionTimeout property has elapsed.


See Also

Using Session State Module | Using Session State Store Provider | Using sessions in geographically separated web farms