Wednesday, May 2, 2012

How to use dispatcher to serve cache content if render is unavailable in CQ / WEM

Use Case: You want to serve cached content if all the renderer in dispatcher.any is unavailable

Prerequisite: You need dispatcher version 4.1.0. You can download latest dispatcher from package share. You can also connect to Day Care to get latest dispatcher release.

Problem: Currently If all renderer is unavailable, You get "502 Bad response code". It might possible that all your pages are already cached before render went down. It will make sense to serve cached content in that case.

Solution: You can use following parameter in dispatcher.any for this,

/ServeStaleOnError "1"


You have to restart apache after making changes. You can add this tag any where at /website level. So it should be,


/farms
  {
  # first farm entry (label is not important, just for your convenience)
  /website
    {
....
/ServeStaleOnError "1"
}
}

If you want to serve custom error page if pages are not cached. Feel free to modify httpd.conf and add following line to it,

ErrorDocument 502 <Path for custom 502 page>

This is true for any error page. More information can be found here http://httpd.apache.org/docs/2.0/mod/core.html#errordocument

If you are using this make sure that DispatcherPassError is set to 0. More information about this can be found here, 

You can also handle 5XX error effectively by adjusting following params in dispatcher.any
    /health_check
      {
      # Page gets contacted when an instance returns a 500
      }
    /retryDelay "1"
    /numberOfRetries "5"
    /unavailablePenalty "1"
Again You can find more information about it on Day Doc. 

Another cool thing you can do with latest dispatcher is, You can use * for client header section, So that you don't have to add all entry. Something like

 /clientheaders {*}

Feel free to comment if something is not clear.

15 comments:

  1. Should it not be /ServeStaleOnError "1" to turn on the behavior of serving potentially state content on error from origin?

    ReplyDelete
    Replies
    1. oooops .. thanks for pointing out ... fixed ..

      Delete
    2. Hii Yogesh, The invalidation is still happening if my render server returns 500 error even after adding the property "/ServeStaleOnError "1"" you suggested. I am using Apache2.2 and AEm6.1. Please help me to resolve this issue . Thanks in advance:)

      Delete
  2. So using health_check, when an instance returns a 503 error to dispatcher, does dispatcher...

    1) redirect the request to the health_check page, returning an error to the client if the health_check page also fails?

    or,

    2) probe the health_check page and if it fails, redirect the request to a working instance, all transparent to the client?

    ReplyDelete
  3. @JamesH

    1) CQ do not redirect request to health_check page. That is just for Probe reason and incrementing unavailablePenalty for RetryDelay
    2) If you want to redirect to some page on 5XX you have to use apache ERROR document http://httpd.apache.org/docs/2.0/mod/core.html#errordocument

    ReplyDelete
  4. Yogesh,
    I'm having an issue in calling a servlet from dispatcher. I have a servlet that posts content to author from publish. /xxx is working on publish instance when called directly whereas when I put a dispatcher in front, the path is not getting redirected to publish instance's servlet. How to fix this?

    ReplyDelete
    Replies
    1. Make sure that path under which servlet is registered is not blocked by filter. What is your servlet path ?

      Delete
  5. I'm using a path like /xxxxwrite and I get this error in apache error_log as File does not exist: /var/www/html/xxxwrite

    ReplyDelete
  6. OK. Yogesh. It did work. I was'nt adding the path correctly in dispatcher.any. Thanks

    ReplyDelete
  7. Hi Yogesh,

    I am new to dispatcher. I configured dispatcher in my system.I am following the below process.
    I created a project with some template. Using that I created a page in site admin,then I activated the page . I am trying to open this page using dispatcher port so it will cached in httdocs. But when I am trying to open it using dispatcher port my page is getting redirected to geometrixx page(http://localhost:4503/content/geometrixx/en/toolbar/account/login.html?resource=%2Fcf#/content/karthik/test2.html) asking for login.
    How can I avoid this behavior

    ReplyDelete
    Replies
    1. @Karthik,

      For Author instance it is expected Behavior, As resource needs to be authenticated before you gain access to it. For Publish instance you have to make sure that Anonymous access is allowed for this resource.

      Yogesh

      Delete
  8. Where in dispatcher.any file should you add the /retryDelay and /numberOfRetries parameters? Anywhere? or is there a specific place these need to be added under?

    ReplyDelete
    Replies
    1. It is under root that is under /website. Please check https://dev.day.com/docs/en/cq/current/deploying/dispatcher/disp_config.html for example.

      Delete
  9. Hi, I have created a servlet */bin/listfiles* to do some search operations, which returns a json response, But the search response is getting cached in the dispacher .. Is there any way to invalidate servlet caching from the dispacher everyday?

    ReplyDelete
    Replies
    1. You can always add .json in invalidation rule under invalidate section of your dispatcher file. This will make sure that every time you activate a page, cached json will get invalidated. This is best approach. You can always have a cron Job in your machine to invalidate or delete cache on machine every day.

      Delete