Friday, May 01, 2009

Varnish Reverse Proxy Accelerator (Ca...

Intro:
The need to offer a mechanism to accelerate the performance of web applications could arguably be even more relevant as we move into a a more linked data approach.  With the need to possible offer multiple representations of a single resource URI to address needs for XML, RDF or XHTML+RDFa (or microformats) etc. the need to improve performance and address request from cache when possible increases.

Related:
Past efforts in this area have involved Apache Connectors to Tomcat (Ref: http://tomcat.apache.org/connectors-doc/index.html ) and also reviewing Glassfish.  Glassfish Portfolio (Ref: http://www.sun.com/software/products/glassfish_portfolio/ ) has many elements focused on performance and there is also Galssfish Grizzly (Ref: https://grizzly.dev.java.net/ ) which offers some some impressive sounding performance aspects using NIO. 

Approach:
However, to address needs of maintaining URI persistence and caching what was really needed was a "reverse-proxy accelerator".  Given that set of criteria I arrived soon at using Varnish (Ref: http://varnish.projects.linpro.no/ ).  The target application is hosted on Glassfish but obviously Varnish can reverse proxy for a number of machines and any backend talking over http.  Indeed, it's this ability that is important to me in order to address my desire to be able to alter systems and networking aspects behind Varnish and have it maintain my URI's in a "Cool URIs" style/approach (Ref: http://www.w3.org/Provider/Style/URI ). 

Following the nice tutorial by Jay Kuri, that is located on that site it is rather easy to get Varnish up and running on Linux and doing reverse proxy and caching.    I would like highlight a few things Jay, mentioned in that write up and detail a few aspects I ran into. 

One of the biggest that Jay, mentions is the issue around Varnish not wanting to cache anything that has a cookie reference.  Since almost all web based applications are going to do this as a simple session management approach (even if you are not doing accounts), Varnish by default will not cache your URIs.    Jay, goes on to recommend code like:

    if (obj.http.Cache-Control ~ "max-age") {
        unset obj.http.Set-Cookie;
        deliver;
    }

to override this behavior and respond from cache for content.  As noted this does mean that is starts to become depending on the content provider/web app developer to issue the commands to inform cache system like Varnish about the behavior we expect from them.

Setting behavior in Grails:
Using Grails (Ref: http://www.grails.org ) it's easy to set the format of our return in via the withFormat{} syntax.  Note we would want to make the approapriate entries in our Config.groovy grails.mime.types section (Ref: The "content negotiation" section of http://www.grails.org/1.0+Release+Notes )

So something like:

    withFormat {
      html {
        response.setHeader("Vary", "Accept")
        def nowPlusHour = new Date().time + 3600000
        response.addHeader("Last-Modified",
            String.format('%ta, %<te %<tb %<tY %<tH:%<tM:%<tS %<tZ', new Date()))
        response.addHeader("Expires",
            String.format('%ta, %<te %<tb %<tY %<tH:%<tM:%<tS %<tZ',
             new Date(nowPlusHour)))

        [allSites: allSites, allAutoSites: onlyAutoSites]
      }
      rdf {
          def data = modelAsRDFService.asRDF(AgeModel.findAllByLeg(params.id),
             "/loc/sites/${params.id}")

          response.setHeader("Vary", "Accept")
          response.contentType = "application/rdf+xml"
          def nowPlusHour = new Date().time + 3600000
          response.addHeader("Last-Modified",
            String.format('%ta, %<te %<tb %<tY %<tH:%<tM:%<tS %<tZ', new Date()))
          response.addHeader("Expires",
            String.format('%ta, %<te %<tb %<tY %<tH:%<tM:%<tS %<tZ',
             new Date(nowPlusHour)))
         
          response.outputStream << data
      }
    }
  }

In this code we have set the LAST-MODIFIED and EXPIRES header entries. Note in this simple example I have simply pushed the expires time ahead by one hour.  You can set this however you wish depending on your view of the relative age your resources can be and still be valid.

The VARY header is set to address some linked data best practices.  This informs the client that the representation of this resource URI can change based on how we call it.  Here it can be requested as HTML  (what is in fact XHTML+RDFa in our case..  a whole topic in itself) and RDF.

There is no builder for RDF, so I simply pass the model to a service that generates and returns this for me.   The Export Plugin (Ref: http://www.grails.org/Export+Plugin ) might be worth looking at if you are looking for various other formats to serialize your data to.  I don't set the content type for the HTML in the code above, on review I likely should though by default it is coming back as text/html so that may be fine.  

Be sure to have your RDF contain a reference to itself and its resource URI if you are doing a linked data approach. 

There is also likely other response header elements one could consider here based on the reverse proxy and caching needs.  A review of http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4 and your accelerator package may reveal other setting of value for your particular environment.

Using curl to validate behavior:
While I really love Firebug, I found it to not be the easiest to use to verify proper cache behavior by Varnish.  Rather, I used (being a bit of CLI lover), curl.  A curl request on a resource URI is made using the -D out option to capture the headers.  Two consecutive headers are shown below:

HTTP/1.1 200 OK
X-Powered-By: Servlet/2.5
Server: Sun Java System Application Server 9.1
Vary: Accept
Last-Modified: Fri, 1 May 2009 16:10:00 CDT
Expires: Fri, 1 May 2009 17:10:00 CDT
Content-Type: image/png
Content-Length: 20951
Date: Fri, 01 May 2009 19:59:32 GMT
X-Varnish: 868897910
Age: 0
Via: 1.1 varnish
Connection: keep-alive

HTTP/1.1 200 OK
X-Powered-By: Servlet/2.5
Server: Sun Java System Application Server 9.1
Vary: Accept
Last-Modified: Fri, 1 May 2009 16:10:00 CDT
Expires: Fri, 1 May 2009 17:10:00 CDT
Content-Type: image/png
Content-Length: 20951
Date: Fri, 01 May 2009 19:59:34 GMT
X-Varnish: 868897911 868897910
Age: 2
Via: 1.1 varnish
Connection: keep-alive

Note the two values in the X-Varnish response header entry indicating to us that the content came from the previously cached content.  If we continually saw only one number here would need to investigate why our request is being passed through to the backend server.

Conclusion:
Varnish, with a few considerations related to explicate cache control instructions provides a nice acceleration to web application performance.  As a from the start reverse proxy (vs something like Squid which started life as a forward proxy) it plays a valuable roll in linked data approaches by allowing changes to systems and networks behind the scene while allowing URI persistence to be maintained.  I've also had no issues with it with respect to 303 redirects on generic documents, also important in linked data approaches. 

41 comments:

美麗 said...

小遊戲,ut男同志聊天室,成人圖片區,交友104相親網,0951成人頻道下載,男同志聊天室,成人貼圖,成人影片,tt1069同志交友網,成人視訊,aio交友愛情館,情色視訊,情色視訊,色情遊戲,交友戀愛小站,jp成人,熊貓貼圖,成人圖片,成人文章,正妹,成人小說,杜蕾斯成人,ut 聊天室,熊貓貼圖區,交友聊天找e爵,ol制服美女影片,777成人區,bt成人,女同志聊天室,貼圖片區,一葉情貼圖片區,6k聊天室,69成人,成人貼圖站,色情影片,聊天室ut,免費成人影片,成人漫畫,0204貼圖區,小高聊天室,歐美免費影片,

GarthStich said...

情色論壇情色視訊聊天室080中部人聊天室ut本土自拍美女老師票選素人寫真圖片免費a圖a片網hk 85cc net手機a片成人情色網18禁貼圖區明星走光貼圖人妻照片ut0204 com成人av女優找fun風夫妻聯誼俱樂部下載a片熊貓成人貼圖熟女貼圖a圖小站網路正妹照片亂倫自拍後宮佳麗優質無碼dvd亂倫圖書館偷拍照片童顏巨乳免費交友ggoo隋棠 寫真比基尼女郎正妹無名相簿的密碼日本a片無碼a區褲襪美腿圖庫夜未眠成人影城wii777台片專賣店無碼影片ol美腿舒淇寫真集自拍密錄館777p2p慾望城市圖貼區性愛補補聊天室性愛女優日本女優無碼色情論壇倉井空寫真集tiger777 net大奶妹做愛影片成人光碟

如此的 said...

Never put both feet in your mouth at the same time, because then you will not have a leg to stand on.............................................

119 said...

GOOD........................................

微笑每一天 said...

IT IS A VERY NICE SUGGESTION, THANK YOU LOTS! ........................................

婉婷婉婷 said...

TAHNKS FOR YOUR SHARING~~~VERY NICE.................................................

MichellSommerville0202 said...

It's great!!.............................................

LaciRossetti199 said...

援交女豆豆出租情人視訊sogo論壇視訊辣妹桃園兼職援交辣妹視訊一對一視訊520sex日本視訊小魔女自拍av1688影音娛樂網辣手美眉甜心寶貝直播貼片免費色咪咪視訊網pc交友視訊美女ggoo免費視訊情色網咆哮小老鼠高雄援交夢中情人情趣用品sex888免費看影片波霸美女寫真sex888免費看影片視訊新竹援交留言0401成人聊天室甜心寶貝貼影片援交友留言桃園sogo 論壇080情人網視訊泳裝秀拓網交友色美眉免費看視訊免費色咪咪影片網 兼職援交聊天室ilover99a片天堂卡通aa片台灣情色網無碼avdvd色色網sexy diamond sex888入口高雄視訊辣妹自拍免費a片亞洲東洋影片hilive本土自拍天堂西門慶成人論壇 費 aaa 片試看dudu sex免費影片avdvd一夜情色妹妹免費情慾影片觀賞qq美美色網影片av免費影片日本 a 片自拍偷拍網站情色小說jp成人a 片日本avdvd女優xxx383美女寫真日本avdvd小魔女免費影城無碼avdvd無碼卡通情色情色論壇甜心寶貝貼片區Show-live視訊聊天室 情色免費A片情色偷拍免費A片一本道 a片 東京熱avdvd影片色美眉台中援交aa 片試看aaa 片試看情人輔助品成人網站做愛自拍偷拍免費試看av免費成人電影dudu sex免費 aa 片試看臺灣情色網線上免費a長片0204免費a片試看a片免費試看a片天堂台灣論壇成人a漫畫免費視訊聊天ing免費視訊美女aaa影片下載城卡通aa片免費看成人影片分享視訊聊天評比104免費成人情色文學小說

勝傑懿綺 said...

若無一番寒徹骨,焉得梅花撲鼻香。..................................................

婉耿賢耿賢亞 said...

人生有些波折,才能有些成長,所以不論順逆,凡是成長、成功的助緣,都應該心存感激。 ..................................................

佳喬 said...

愛情不是慈善事業,不能隨便施捨。...............................................................

阿楊 said...

量力而為,別勉強了,Cut your coat according to your cloth..............................................

皇豪 said...

免費a片觀看,凌辱人妻溫泉 豆豆聊天室 s383情色視訊大網咖 免費視訊影片 1111免費成人電影 嘉義視訊哈啦聊天室 絕色成人 av168成人 視訊聊天室aqaq hbo論壇 45av無碼a片 免費視訊聊天網 後宮電影電影院 免費情愛影片 成人免費視訊-玩美女人 dv影視免費線上 嘟嘟kk拓網視訊 sex888免費看影片論壇,18 成人 美女視訊免費看 sogo論壇a片線上看 聊天室ukiss 免費視訊ggo 飄蟲本土自拍論壇區 情趣 用品成人用品維納斯 666成人視訊網 dodo豆豆聊天室 sex888,sexy girl avdvd無碼情色電影 sexy girl免費aa片試看 sex999免費影片兼職援交 影音視訊免費送 300 點 色美媚部落格免費 18 成人dudu sex,h影片 視訊辣妹34c甜心寶貝直播貼片 18 禁亞洲名模瘋情 aio辣妹交友愛情館 李冠儀性感照 視訊交友icc 6k聊天館 免費影片觀賞,sex女優王國 環球辣妹聊天室2009情色文學168論壇 85街免費影片 a片商 成人貼圖站色情av片 sex520-成人網 視訊美女msvt s383情色視訊網 777美女dvd 台灣a片王-avdvd 視訊美女 寫真

雅婷 said...

生活總是起起伏伏,心情要保持快樂才好哦!!..................................................

于呈均名 said...

很用心的blog~很喜歡~願您一切順心 ....................................................

tgibson said...

成功等於目前,其他都是這句話的註解。........................................

孝齊 said...

No pains, no gains...................................................................

建月 said...

Man proposes, God disposes...................................................................

堅燕 said...

永遠支持你呀!!HO!!HO!!HO!!HO!!~~......................................................................

茹順茹順 said...

一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼....................................................................

信豪信豪 said...

一定要保持最佳狀況呦,加油!!!期待你發表的新文章!.................................................................

凱文凱文 said...

所有的資產,在不被諒解時,都成了負債............................................................

芸茂芸茂 said...

安安!剛開始玩這個,來這裡逛一下^^............................................................

楊淑敏 said...

每次看完你的文章,總是回味許久,要經常發表喔。..................................................................

JasonBirk佳琪 said...

我們老得太快,卻聰明得太遲。..................................................

文辰文辰 said...

人生中最好的禮物就是屬於自己的一部份..................................................

楊儀卉 said...

什麼樣的學習計畫並不重要,重要的是你是什麼樣的人。............................................................

陳佑發 said...

你不能決定生命的長度,但你可以控制它的寬度............................................................

倫音倫音 said...

Quality is better than quantity.............................................................

劉佳仲 said...

Practice makes perfect...................................................

黃靜文黃靜文 said...

One swallow does not make a summer...................................................

曹韻婷 said...

No garden without its weeds...................................................

治冠霖士 said...

人生中最好的禮物就是屬於自己的一部份............................................................

熙又陽辰 said...

Make hay while the sun shines...................................................

張忠張鴻水玉 said...

文章不求沽名釣譽,率性就是真的..................................................................

怡張樺怡張樺怡張樺 said...

may the blessing be always with you!!............................................................

峻胡邦慧v帆 said...

用心經營blog,人氣百分百~^^ 加油 ..................................................................

沛汝 said...

thank you so much~~ SUPPORT!!..................................................................

1615 said...

開心不開心都是一天,祝您能夠笑著面對一切!...............................................................

建枫 said...

真是太猛了,請受小弟一拜Orz(>O<)......................................................

惠NorrisBradwell041花 said...

聰明的人喜歡猜心 雖然每次都猜對了卻失去了自己的心............................................................