0

We have a webfarm with 2 servers, both has the DFS replication implemented. We have a section on homepage which displays 4 articles known as "TopStories" and are cached so that it does not fetches from database everytime

Now when editors updates the Topstories, that change should get live immediately to the end users across the web-farm, now say editor made the changes to the Topstories on server 1 and the end-user who accessed the website was taken to server 2, so user won't be able to see the latest updates on topstories, as on server 2 it will still fetch from cache

So for this we recycles the app-pool by updating the web.config file.

We are using below code to make an update in appsettings element of web.config.
public void RefreshWebConfig()
    {
        //XmlDocument doc = new XmlDocument();
        //doc.Load(HttpContext.Current.Server.MapPath("web.config"));
        //doc.Save(HttpContext.Current.Server.MapPath("web.config"));

        System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

        System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["TopStorySessionKey"];
        int SessionKey = 0;
        if (setting != null)
        {
            SessionKey = Convert.ToInt32(CommonUtil.GetConfigurationValue<int>("TopStorySessionKey"));
            if (SessionKey == 100)
                SessionKey = 0;
            config.AppSettings.Settings["TopStorySessionKey"].Value = Convert.ToString(SessionKey+1);
        }
        else
        {
            config.AppSettings.Settings.Add("TopStorySessionKey", SessionKey.ToString());
        }
        config.Save();

    }

I have 2 questions related to this:

  1. In any instance updating the web.config's app-setting key could delete the whole web.config file using the above code?
  2. Is there any better way to replicate the change on both the servers.

Thanks in advance.

Abbas
  • 4,948
  • 31
  • 95
  • 161

1 Answers1

1

In any instance updating the web.config's app-setting key could delete the whole web.config file using the above code?

I can't see how this code as written would delete the web.config, but it's a rather indirect way to force the app pool to recycle. You can do it directly:

Restarting (Recycling) an Application Pool

Is there any better way to replicate the change on both the servers.

It is rather expensive to recycle the entire app pool just to invalidate a small cache. Instead, consider using a MemoryCache. If you can place the latest articles e.g. a file (whether a single file accessed as a network share, or as a copy of the file on each web server), you can use a file dependency to expire the cache. If the articles are in a SQL database, you can use a SqlChangeMonitor to expire the database. If you can allow the latest articles to be e.g. 2 minutes old, you can use sliding expiration to reload the cache every so often.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • We cannot restart app pool directly, because that would cause just the server 1 app pool to recycle (or the one from which the editor is making change), is it possible to recycle app-pool on both servers at the same time. – Abbas Apr 28 '15 at 16:33
  • You are currently touching web.config on both web servers. How are you triggering that today, and why can't you run code to recycle the app domain rather than running code to update web.config? – Eric J. Apr 28 '15 at 17:48
  • As i said, i don't have to take care of another server if we do through file system, because DFS automatically replicates the changes on another server, so we just have to update the web.config of current server, as soon as DFS finds changes in it, it will replicate the same on another server, but in case of directly recycling app pool DFS won't come to rescue. – Abbas Apr 29 '15 at 07:37
  • 1
    So... use a file other than web.config to indicate a change, have DFS replicate that file, and invalidate the cache based on that file changing (as a simple solution). That avoids all of the performance cost of recycling the app pool. – Eric J. Apr 29 '15 at 16:13