ASP. NET improves performance through distributed sessions

Source: Internet
Author: User

If we are using Session, we will build a high-performance and scalable ASP. NET websites must solve the distributed Session architecture, because the SESSION processing capability of a single server will quickly experience performance bottlenecks, which are also called Session synchronization. Microsoft has its own distributed Session solution, SessionStateServer. For details, refer:

ASP. NET Session State Partitioning
Http://blog.maartenballiauw.be/post/2008/01/23/ASPNET-Session-State-Partitioning.aspx

ASP. NET load balancing and ASP. NET state server
The http://blog.maartenballiauw.be/post/2007/11/ASPNET-load-balancing-and-ASPNET-state-server-(aspnet_state). aspx

However, this article is about another solution, that is, to use Memcached to reach the distributed SESSION architecture. Memcached has been widely used in website construction as a distributed cache server.

I. Session mechanism

Session is intended for users, and we can also understand it as for browsers. When the browser first accesses the ASP. NET webpage (the session function is not disabled on the webpage), it will send the following HTTP header to the client:

After receiving the preceding HTTP header, the browser saves the unique SESSIONID in its own COOKIE (as long as the COOKIE is not disabled, this article does not discuss the case of disabling the COOKIE, for more information, see http://www.cnblogs.com/fish-li/archive/2011/07/31/2123191.html ). When the browser requests the server for access again, it will add the following identifier to the HTTP request header. We can see that this SESSIONID is the above SESSIONID:

This mechanism is used between the browser and the server to ensure the user's SESSION.

If the Cookie is disabled in the browser on the client, we will find that the Set-Cookie in the browser is different every time we refresh the browser, and the Cookie ID will never appear in the sending request header. At this time, we will find that the Session is invalid (of course, to prevent this situation, Microsoft allows us to set cookieless = "true" in sessionState and use the URL to pass sessionid ).

Ii. Memcached Providers

The Memcached client I use is Memcached Providers. After downloading the client, you will find that Memcached Providers has provided support for Distributed sessions. If you do not use Memcached Providers, refer to Memcached Tip 1: Use Memcached Providers. The example provided by Memcached Providers is to store sessions directly in the database. We can configure it to store sessions in the distributed SESSION memory, that is, modify dbType from SQL to none. :

The distributed Session provided by Memcached Providers has nothing special, because the SessionStateProvider type provided by Memcached Providers implements ASP. the abstract class SessionStateStoreProviderBase in. NET. We can see that the Session processing class specified in the configuration file is SessionStateProvider. Therefore, ASP. NET will consciously use SessionStateProvider to process all sessions after receiving requests from the client, which is exactly the same as this class, the SESSION is read and stored in Memcached. (If SQL is set, the SESSION is stored in the SQLSERVER database ).

The setting and reading of sessions are no different from those of traditional systems. Read:

      Session["sname2"] = "sluminjxxi";      Session.Timeout = 2;

Take:

      Response.Write(Session["sname2"]);

Iii. Why should I configure SQL statements?

The disadvantage of traditional SESSION exists when dbType is set to none only. What if Memcached's memory reaches the upper limit? Memcached uses the LRU elimination algorithm (which has not been used for a long time). Here, we don't need to go into details about the mechanism of this algorithm in Memcached. We just need to know what kind of mechanism it is like when the memory is tight, even if the SESSION time is not reached, Memcached may kill it. Therefore, it is safe to add persistent storage of SQLSERVER under Memcached. If the cache hits, the cache will be taken directly. If the cache is dead, it will be confirmed again in the database. Of course, this will cause some performance loss, but it is a safer approach.

Some scripts for SESSION Initialization are provided in the download file provided by Memcached Providers. After the script is correctly executed, it will generate a table such as tblSessions and several stored procedures:

TblSessions stores separate sessions as follows:

4. Memcached Providers BUG

In the current Memcached Providers (version 1.2), there is a BUG about SessionStateProvider (29520-TRUNK) (I have submitted it to codeplex and believe their next version should be corrected. If we test the SESSION expiration time, we find that after a refresh, it will always be 20 minutes (that is, the default value ). This is because in the ReleaseItemExclusive overload method (this method is used to release the lock on the items in the Session data storage area), there is no expiration time for Session re-storage, as shown below:

The source code provided by Memcached Providers is commented out, and the correct one should be the one I corrected. With the corrected DLL, everything is complete.

V. scalability of sessions stored in databases

With the increase in access traffic (of course, to this extent, it indicates that the website is very successful and most websites do not need to consider this step), even if we use Memcached as the cache, using a single SQL Server to store sessions still brings about performance problems. In this case, we can adopt a horizontal partition architecture for database design, that is, according to a certain algorithm (according to SESSIONID, or User Name) to store sessions in different databases. At this time, if we still use Memcached Providers, we must further modify the source code, which originally supported a single SQLSERVER server and programming to support multiple servers. Of course, if you do not like SQLSERVER, you can also change it to support mysql, mongodb, any custom KEY-VALUE framework, and so on. If this is not the case, it will not be shown yet.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.