erik.engstrand@precio.se
Feb 8, 2010
  6179
(0 votes)

Subscription job deadlock on site with large number of users

On web sites with large number of users you may get a deadlock in the SQL-server when executing the EPiServer subscription job.

The error message is something like this: Exception has been thrown by the target of an invocation. [Transaction (Process ID 105) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.]

image

Cause of the error

The error comes from the SQL-server when the subscription job executes the method ProfileManager.GetAllProfiles(). From my investigations the error appears when users are registering to the site during this method is executed. On large sites this is a very common situation. Users do register all the time.

Solution

I have used Reflector to copy all code in EPiServer’s class SubscriptionJob to a new customized subscripttion job.

I made some changes to the code. First of all there are some code that could be optimized. In my application the execution time for the subscription job has gone from 30 to 6 minutes, by implementing this fix.

In the method protected virtual int SendSubscriptions(EPiServerProfile profile) there is no check if the user does subscribe to any pages or not. When you have large number of users and also a custom membership provider this will cause the job to run very slow. The PrincipalInfo of the users is fetched for all users. The method GetUser will be called for all users in the membership provider.
Some small changes to the code will fix this issue. Just check if the profile does subscribe to more than zero pages.

   1: protected virtual int SendSubscriptions(EPiServerProfile profile)
   2: {
   3:     int num = 0;
   4:     if (profile.SubscriptionInfo.SubscribedPages.Count > 0)
   5:     {
   6:         IPrincipal principal = PrincipalInfo.CreatePrincipal(profile.UserName);
   7:         foreach (SubscriptionDescriptor descriptor in profile.SubscriptionInfo.SubscribedPages)
   8:         {

 

The solution to the real problem is more difficult. I have been able to get rid of many deadlocks but not all.

The profiles are fetched in blocks of 1000 in the subscription job. Between every 1000 some work is done for sending subscription emails to the users. This takes some time. For the next 1000 users it can be a deadlock. I think this occurs if a new user have registered during this time.

I tried to increase the pageSize for the methodProfileManager.GetAllProfiles() to decrease the time between the first and the last profile. The deadlocks are almost gone. Is there any reason why EPiServer do this job in page sizes of 1000? Is there a better solution to this problem?

   1: protected virtual string InternalExecute()
   2: {
   3:     int num3;
   4:     ProfileInfoCollection infos;
   5:     int num = 0;
   6:     int num2 = 0;
   7:     int pageSize = 200000;
   8:     Label_0004:
   9:     infos = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All, num2++, pageSize, out num3);
  10:     if (infos.Count != 0)
  11:     {
Feb 08, 2010

Comments

Sep 21, 2010 10:33 AM

Thanks for sharing. Nice optimize trick
/ Anders Hattestad

Sep 21, 2010 10:33 AM

Great info.
/ Per Nergård

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024