Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dave 3 posts 85 karma points
    Aug 31, 2018 @ 09:56
    Dave
    0

    Load balancing and the Published event

    Hi,

    I'm a bit new to the entire umbraco thing, but have been using it for a few months now and have some questions about load balancing that I hope someone can give me pointers on.

    I have the site (v7.11.1) setup in Azure and was looking to add another copy of the site, but to a different location e.g. East US. From the bits of info I have found, it seems Umbraco can do this out of the box. I have done a quick test setup and it does seem to work.

    The only thing that doesn't work is some custom code that runs when items are published. The site has some custom caching and when items are published it expires the content out of the cache.

    This works just fine on the 'main' site where the edits are being done, but the code doesn't fire on the 'secondary' site so the cache is not cleared. Is there anyway to have the code run on the 'secondary' site when it content is changed? I tried to find info in the docs, but my experience so far has been that the docs aren't always up to date :(

    Also for load balancing what is the best setup to have? is it better to have 2 public servers for the frontend and 1 private server to do edits on?

    We have noticed the site restart when there is a lot of editing/publishing going on, so was wondering if the split of frontend servers and one for editing might help with keeping the public facing part of the site up?

    Many Thanks

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 31, 2018 @ 13:04
    Dave Woestenborghs
    100

    Hi Dave,

    Dave here :-)

    For updating your cache on your load balanced server you need to use different events. You need to hook in to the PageCacheRefresher.CacheUpdated event.

    This is an event that will execute on all servers after a publish

    You can find an example in this post :

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/79614-memory-cache-refresh

    Dave

  • Dave 3 posts 85 karma points
    Aug 31, 2018 @ 14:55
    Dave
    0

    Hi Dave :)

    Thanks for the reply, had a quick play with it in a single server setup and looks like the cache busting / removal code should have always been in that method.

    Another question now, any ideas under what circumstances the MessageType is RefreshById?

    I couldn't trigger this condition in the code

    if (e.MessageType == MessageType.RefreshById || e.MessageType == MessageType.RemoveById)
    

    I tried publishing a single item and publishing a subtree and both times the MessageType was RefreshByInstance.

  • Dave 3 posts 85 karma points
    Sep 07, 2018 @ 07:43
    Dave
    2

    Ok, I've had a little more time to test it out and the RefreshById and RemoveById conditions do get triggered on the 'secondary' server.

    So the RefreshByInstance and RemoveByInstance happens on the server the publish event came from and the RefreshById and RemoveById happen on other servers picking up the change.

    This is the code I finally ended up with, in case it helps anyone else:

        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {     
            PageCacheRefresher.CacheUpdated += PageCacheRefresher_CacheUpdated;
        }
    
        private void PageCacheRefresher_CacheUpdated(PageCacheRefresher sender, Umbraco.Core.Cache.CacheRefresherEventArgs e)
        {
            string key = null;
    
            var cache = new CacheService();
    
            if (e.MessageType == MessageType.RefreshByInstance || e.MessageType == MessageType.RemoveByInstance)
            {
                var content = (Umbraco.Core.Models.Content)e.MessageObject;
                key = CacheService.GetCacheKeyForModel(content);
            }
            else if (e.MessageType == MessageType.RefreshById || e.MessageType == MessageType.RemoveById)
            {
                int id = (int)e.MessageObject;
                var content = ApplicationContext.Current.Services.ContentService.GetById(id);
                key = CacheService.GetCacheKeyForModel(content);
            }
    
            if (!string.IsNullOrEmpty(key))
                cache.Delete(key, true);
        }
    

    just ignore the CacheService lines. Also in this situation there is no UmbracoContext so have used the Content service to get the model (as I needed that to create my key)

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Sep 07, 2018 @ 07:46
    Dave Woestenborghs
    0

    Nice clarification !

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft