Ultra Fast ASP.NET 4.5, 2nd Edition
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
iii
AbouttheAuthor................................................................................................
AbouttheTechnicalReviewers ...........................................................................
Chapter1:PrinciplesandMethod ..........................................................................
Chapter3:Caching ...............................................................................................
Chapter5:ASP.NETThreadsandSessions.........................................................
Chapter6:UsingASP.NETtoImplementandManageOptimizationTechniques199
Chapter7:ManagingASP.NETApplicationPolicies ...........................................
Chapter9:SQLServerAnalysisServices............................................................
Chapter11:PuttingItAllTogether .....................................................................
Glossary..............................................................................................................
Introduction
ThetimethatIspentworkingatMicrosoftwasanunexpectedlytransformingexperience.Thefirsthalf ofmycareerregularlyputmeandthecompaniesIworkedwithincompetitionwithMicrosoft,andIwas oftensurroundedbyanti-Microsoftstoriesandpropaganda.However,whenIheardabout.NET,I decidedIwantedtoknowmoreandthatthebestwaytodothatwastolearnatthesource.
AsIgotintothetechnologyandthecompany,whatIfoundwasmorethanalittlesurprising.The .NETFramework,theC#language,ASP.NET,andSQLServeraresophisticatedandtechnicallybeautiful achievements.AfterworkingwithJavaforseveralyears,whichalsohasadefiniteelegance,itwas refreshingandempoweringtouseawell–integratedplatform,whereeverything(mostly)worked togetherseamlessly.Atatechnicallevel,IfoundthatIusuallyagreedwiththedecisionsandtradeoffs theplatformdevelopersmade,andthattheresultingsystemhelpedtosubstantiallyimprovemy productivityasadeveloper,aswellasthequalityoftheresultingsoftware.IalsofoundtheMicrosoft engineeringteamstobewonderfullybright,creative,and—perhapsmostsurprisingofalltomeasa formeroutsider—sincerelyinterestedinsolvingcustomerproblems.
Myenthusiasmforthetechnologyhelpedcarrymeintoacustomer–facingpositionasasolutions architectattheMicrosoftTechnologyCenterinSiliconValley.Beingexposedin–depthtocustomer issueswasanothereye–openingexperience.First,Icouldseefirst–handtheremarkablypositiveimpact ofMicrosofttechnologiesonmanypeopleandcompanies.Second,Icouldalsoseetheintense frustrationandpoorresultsthatsomepeoplewerehaving.Thisbookis,inpart,aresponsetosomeof thosefrustrations.
MyperspectiveisthatASP.NETandSQLServerhavetremendouspotential.However,keyaspects ofthetechnologiesarenotobvious.I’vetalkedwith(andinterviewed)manydevelopersandmanagers whosensethepotentialbutwhohavehadextremedifficultywhenitcomestotheimplementation. Unfortunately,realizingthetechnology’sfullpotentialrequiresmoreup–fronteffortthansome alternativeapproaches;it’sarichenvironment,andtoappreciateitfullyrequiresacertainperspective. Oneofmygoalsforthisbookistohelpremovesomeofthefogthatmaybemaskingtheend–to–end visionofthetechnologyandtohelpyouseethebeautyandthefullpotentialofASP.NETandSQL Server.
AnotherreasonIwrotethisbookisthatIamfrustratedconstantlybyhowslowsomesitesare, andI’mhopingyouwillbeabletousetheinformationheretohelpchangethat.TheWebhasamazing possibilities,wellbeyondeventhefantasticlevelit’sreachedalready—buttheycanberealizedonlyif performanceisgood.Slowsitesareaturn–offforeveryone.
MyInternetconnectiontodayusesan11MbpsDSLline,andeachofthetwelvehyperthreaded coresinmydesktopCPUrunsatnearly3GHz;that’snearlyfourtimesthenetworkbandwidthandthree timesthenumberofCPUcoresIhadwhenIwrotethefirsteditionofthisbookjustacoupleofyears ago.It’sastonishinglyfast.YetevenwiththatmuchnetworkandCPUspeed,manywebpagesstilltakea longtimetoload—sometimesaminuteormore—andmylocalnetworkandCPUarealmostidleduring thattime.Assoftwareprofessionals,thatshouldconcernus.Ifinditalmostembarrassing.Iwanttobe proudofnotjustmyownworkbutalsotheworkofmyprofessionasawhole.Let’smakeoursitesnot justfast,butultra–fast.
xx
WhoThisBookIsFor
Thefirsttwoandlasttwochaptersinthisbookprovideinformationthatwillbeusefultoallweb developers,regardlessofwhichunderlyingtechnologyyouuse.Themiddlesevenchapterswillinterest intermediatetoadvancedarchitectsanddeveloperswhoaredesigning,buildingormaintainingweb sitesusingASP.NETandSQLServer.ExperiencedwebdeveloperswhohaverecentlymovedfromJavaor PHPto.NETwillalsofindlotsofvaluableandinterestinginformationhere.
Thisbookwillbeusefulfornondeveloperswhohaveatechnicalinterestinwhatmakesawebsite fast.Inparticular,ifyou’reinvolvedwithwebsiteoperations,security,testing,ormanagement,youwill discovermanyoftheprinciplesandissuesthatyourdevelopmentteamsshouldbeaddressing,along withdemonstrationsthathelpdrivethepointshome.
ASP.NETMVC,WindowsAzure,andSQLAzure
AlthoughIfocusinthisbookonASP.NETwebforms,IIS,andSQLServerontheserverside,youcan applymanyofthesamefundamentalarchitecturalprinciplestotheASP.NETMVC,WindowsAzure,and SQLAzureplatforms.AlthoughASP.NETMVChasgrownsubstantiallysinceitsintroduction,Microsoft originallybuiltitontopofwebforms,sothefoundationofbothsystemsisthesame.WindowsAzurefor webapplicationsusesIISrunninginvirtualmachines,andSQLAzureisaslightlytrimmed–down, multi–tenantversionofSQLServer.Onceyouunderstandthekeyprinciples,youwillbeabletoapply themregardlessoftheplatformorlanguage.
ContactingtheAuthor
Youcanreachmeat Iwouldlovetohearaboutyourexperienceswiththeultra–fastapproach. Techniquestoimproveperformanceandscalabilityareconstantlyevolving,alongwiththe underlyingtechnology.IamveryinterestedinhearingaboutanytechniquesIhaven’tcoveredherethat youfindtobeeffective.
Pleaseletmeknowifyoufindanyerrorsinthetextorthecodesamples,ortweaksthatcanmake themevenbetter.
Acknowledgments
ForthefirsteditionIwouldliketothankEwanBuckinghamforhisearlysupportandencouragement; MatthewMoodieforhelpwithoverallstructureandflow;SimonTaylorandPhildeJouxfortechnical reviews;AnitaCastroforprojectmanagement;andKimWimpsettforcopyediting.
Forthecurrentedition,I’dliketothankMatthewMoodieagainasleadeditor;FabioFerracchiati andEricLawrencefortechnicalreviews;AdamHeathforprojectmanagement;andChandraClarkfor copyediting.
xxi
C H A P T E R 1
Principles and Method
Modernlarge-scalewebsitesareamazinglycomplexfeatsofengineering.Partlyasaresultofthis,many sitesrunintosignificantperformanceandscalabilityproblemsastheygrow.Infact,it’snotunusualfor largesitestobereengineeredalmostfromscratchatsomepointinordertohandletheirgrowth. Fortunately,consistentlyfollowingafewbasicprinciplescanmakesitesfasterwhilethey’restillsmall, whileminimizingtheproblemsyouwillencounterastheygrow.
Thisbookwillexplorethoseprinciplesandshowhowandwhyyoushouldapplythem. I’mbasingtheideaspresentedhereonmyworkdevelopingnetwork-orientedsoftwareoverthepast
30+years.IstartedworkingwiththeInternetin1974andwithUnixandCin1979andlatermovedto C++andthenJavaandC#.IlearnedaboutASP.NETandSQLServerindepthwhileworkingatMicrosoft, whereIhelpedarchitectanddevelopalarge-scalewebsiteforMSNTV.Ipolishedthatknowledgeover thenextfewyearswhileIwasanarchitectattheMicrosoftTechnologyCenter(MTC)inSiliconValley. Duringthattime,Ihelpedruntwo-tothree-dayarchitecturaldesignsessionsonceortwiceeachweek forsomeofMicrosoft’slargestandmostsophisticatedcustomers.OtherMTCarchitectsandIwould worktofirstunderstandcustomerissuesandproblemsandthenhelparchitectsolutionsthatwould addressthem.
Itdidn’ttakelongbeforeIdiscoveredthatalotofpeoplehadthesamequestions,manyofwhich werefocusedaroundperformanceandscalability.Forexample:
- “HowcanwemakeourHTMLdisplayfaster?”(Chapter2)
- “What’sthebestwaytodocaching?”(Chapter3)
- “HowcanweuseIIStomakeoursitefaster?”(Chapter4)
- “Howshouldwehandlesessionstate?”(Chapter5)
- “HowcanweimproveourASP.NETcode?”(Chapters5to7)
- “Whyisourdatabaseslow?”(Chapters8and9)
- “Howcanweoptimizeourinfrastructureandoperations?”(Chapter10)
- “Howdoweputthepiecestogether?”(Chapter11) Oneofthethemesofthisbookistopresenthigh-impactsolutionstoquestionslikethese.
system.
Inthischapter,I’llstartwithadescriptionofperformanceandscalability,alongwithwhatImean byultra-fastandultra-scalable.ThenI’llpresentahigh-leveloverviewoftheend-to-endprocessthat’s involvedingeneratingawebpage,andI’lldescribethecoreprinciplesuponwhichIbasethisapproach toperformance.I’llconcludewithadescriptionoftheenvironmentandtoolsthatIusedindeveloping theexamplesthatIpresentlaterinthebook.
TheDifferenceBetweenPerformanceandScalability
Wheneversomeonetellsmethattheywanttheirsystemtobefast,thefirstquestionIaskis,“Whatdo youmeanbyfast?”Atypicalanswermightbe“Itneedstosupportthousandsofusers.”Asitecanbeslow andstillsupportthousandsofusers.
Scalabilityandperformancearedistinctlydifferent.Inthecontextofthisbook,whenItalkabout improvingasite’sperformance,whatImeanisdecreasingthetimeittakesforaparticularpagetoload orforaparticularuser-visibleactiontocomplete.Whatasingleuserseeswhilesittingattheircomputer is“performance.”
Scalability,ontheotherhand,hastodowithhowmanyusersasitecansupport.Ascalablesiteis onethatcaneasilysupportadditionalusersbyaddingmorehardwareandnetworkbandwidth(no significantsoftwarechanges),withlittleornodifferenceinoverallperformance.Ifaddingmoreusers causesthesitetoslowdownsignificantlyandaddingmorehardwareorbandwidthwon’tsolvethe problem,thenthesitehasreacheditsscalabilitythreshold.Oneofthegoalsindesigningforscalabilityis toincreasethatthreshold;itwillnevergoaway.
WhyUltra-fastandUltra-scalable?
Speedandscalabilityshouldapplytomorethanjustyourwebservers.Manyaspectsofweb developmentcanandshouldbefastandscalable.Allofyourcodeshouldbefast,whetheritrunsatthe client,inthewebtier,orinthedatatier.Allofyourpagesshouldbefast,notjustafewofthem.Onthe developmentside,beingfastmeansbeingagile:fastchangesandfixes,anddeployments.
Adefinitesynergyhappenswhenyouapplyspeedandscalabilitydeeplyinaproject.Notonlywill yourcustomersandusersbehappier,butengineerstoowillbehappierandwillfeelmorechallenged. Surprisingly,lesshardwareisoftenrequired,andqualityassuranceandoperationsteamscanoftenbe smaller.That’swhatImeanbyultra-fastandultra-scalable(whichIwilloftenrefertoasjustultra-fast forshort,eventhoughscalabilityisalwaysimplied).
Theultra-fastapproachisverydifferentfromanimpulsive,“do-it-now”typeofprogramming.The architecturalproblemsthatinevitablyarisewhenyoudon’tapproachdevelopmentinamethodicalway tendtosignificantlyoffsetwhatevershort-termbenefitsyoumightrealizefromtakingshortcuts.Most large-scalesoftwaredevelopmentprojectsaremarathons,notsprints;advanceplanningand preparationpayhugelong-termbenefits.
I’vesummarizedthegoalsoftheultra-fastandultra-scalableapproachinTable1-1.
Table1-1.GoalsoftheUltra-fastandUltra-scalableApproach Component Ultra-fast and Ultra-scalable Goals
Pages Everypageisscalableandfastunderload. Tiers Alltiersarescalableandfastunderload. Agility Youcanrespondquicklytochangingbusinessneeds,andyoucanreadilymaintain performanceandscalabilityintheeventofchanges. Maintainability Youcanquicklyfindandfixperformance-relatedbugs. Operations Youcanquicklydeployandgrowyoursites.Capacityplanningisstraightforwardand reliable. Hardware Yourserversarewellutilizedunderload;fewermachinesarerequired.
Buildingafastandscalablewebsitehassomehigh-levelsimilaritiestobuildingaracecar.Youneed toengineeranddesignthecoreperformanceaspectsfromthebeginninginorderforthemtobe effective.Inracing,youneedtodecidewhatclassorleagueyouwanttoracein.IsitgoingtobeFormula One,stockcar,rallying,dragster,ormaybejustkart?Ifyoubuildacarforkart,notonlywillyoube unabletocompeteinFormulaOne,butyouwillhavetothrowthewholedesignawayandstartagainif youdecideyouwanttochangetoanewclass.Withwebsites,buildingasiteforjustyourselfandafew friendsisofcoursecompletelydifferentfrombuildingeBayorYahoo.Adesignthatworksforonewould becompletelyinappropriatefortheother.
Atop-endracecardoesn’tjustgofast.Youcanalsodothingslikechangeitswheelsquickly,fillit withfuelquickly,andevenquicklyswapouttheengineforanewone.Inthatway,racecarsarefastin multipledimensions.Yourwebsiteshouldalsobefastinmultipledimensions.
Inthesamewaythatit’sabadideatodesignaracecartogofastwithoutconsideringsafety,itis alsonotagoodideatodesignahigh-performancewebsitewithoutkeepingsecurityinmind.Inthe chaptersthatfollow,Iwillthereforemakeanoccasionalbriefdiversionintosecurityinareaswherethere issignificantoverlapwithperformance,suchaswithcookiesinChapter3.
Optimization
Asmanyindustryexpertshaverightlypointedout,optimizationcanbeadeadlytrapandtime-waster. Thekeytobuildinghigh-performancewebsitesisengineeringthemsothatoptimizationisnotrequired togetdecentresults.However,aswithracing,ifyouwanttocompetewiththebest,thenyouneedto integratemeasuring,adjusting,tuning,tweaking,andinnovatingintoyourdevelopmentprocess. There’salwayssomethingyoucandobetter,providedyouhavethetime,money,andmotivationto doso.
Therealtrickisknowingwheretolookforperformanceandscalabilityproblemsandwhatkindsof changesarelikelytohavethebiggestimpact.Comparingtheweightofwheellugstooneanotheris probablyawasteoftime,butgettingthefuelmixturejustrightcanwintherace.Improvingthe efficiencyofaninfrequentlycalledfunctionwon’timprovethescalabilityofyoursite;switchingtousing asynchronouspageswill.
Idon’tmeanthatsmallthingsaren’timportant.Infact,manysmallproblemscanquicklyaddupto beabigproblem.However,whenyou’reprioritizingtasksandallocatingtimetothem,besuretofocus onthehigh-impacttasksfirst.Puttingahighpolishonaracecarisniceandmighthelpitgoalittle faster,butifthetransmissionisnogood,youshouldfocusyoureffortstherefirst.Polishingsome internalAPIjusthowyouwantitmightbenice,buteliminatinground-tripsshouldbeamuchhigher priority.
Process
Ultra-fastisastateofmind—aprocess.Itbeginswiththearchitectureandthedesign,anditflowsinto allaspectsofthesystem,fromdevelopmenttotestingtodeployment,maintenance,upgrades,and optimization.However,aswithbuildingaracecaroranyothercomplexproject,thereisusuallyasense ofurgencyandadesiretogetsomethingdonequicklythat’s“goodenough.”Understandingwherethe bigimpactpointsareisacriticalpartofbeingabletodothateffectively,whilestillmeetingyour businessgoals.TheapproachI’vetakeninthisbookistofocusonthethingsyoushoulddo,ratherthan toexploreeverythingthatyoucoulddo.Thegoalistohelpyoufocusonhigh-impactareasandtoavoid gettinglostintheweedsintheprocess.
I’veworkedwithmanysoftwareteamsthathavehaddifficultygettingmanagementapprovalto workonperformance.Oftenthesesameteamsrunintoperformancecrises,andthosecrisessometimes leadtoredesigningtheirsitesfromscratch.Managementtendstofocusinevitablyonfeatures,aslongas performanceis“goodenough.”Theproblemisthatperformanceisonlygoodenoughuntilitisn’t—and that’swhenacrisishappens.Inmyexperience,youcanoftenavoidthisslipperyslopebynotselling performancetomanagementasafeature.It’snotafeature,anymorethansecurityorqualityare features.Performanceandtheotheraspectsoftheultra-fastapproachareanintegralpartofthe application;theypermeateeveryfeature.Ifyou’rebuildingaracecar,makingitgofastisn’tanextra featurethatyoucanaddattheend;itispartofthearchitecture,andyoubuilditintoeverycomponent andeveryprocedure.
There’snomagichere.Thesearethekeystomakingthiswork:
- Developingadeepunderstandingofthefullend-to-endsystem
- Buildingasolidarchitecture
- Focusingeffortonhigh-impactareas,andknowingwhat’ssafetoignoreordefer
- Understandingthatalittleextraup-fronteffortwillhavebigbenefitsinthelong term
- Usingtherightsoftwaredevelopmentprocessandtools Youmighthaveheardaboutsomethingcalledthe“eight-secondruleforwebperformance.It’sa human-factors-derivedguidelinethatsaysifapagetakeslongerthaneightsecondstoload,there’sa goodchanceuserswon’twaitandwillclickawaytoanotherpageorsite.Ratherthanfocusingonrules likethat,thisbooktakesacompletelydifferentapproach.Insteadoftargetingartificialperformance metrics,theideaistofocusfirstonthearchitecture.Thatputsyouintherightleague.Then,buildyour siteusingasetofwell-groundedguidelines.Withthefoundationinplace,youshouldn’tneedtospenda lotofeffortonoptimization.Theideaistosetyoursightshighfromthebeginningbyapplyingsome high-enddesigntechniques.Youwanttoavoidbuildingaracerforkartandthenhavetothrowitaway whenyourkeycompetitorsmoveuptoFormulaOnebeforeyoudo.
TheFullExperience
Performanceshouldencompassthefulluserexperience.Forexample,thetimetoloadthefullpageis onlyoneaspectoftheoveralluserexperience;perceivedperformanceisevenmoreimportant.Ifthe usefulcontentappears“instantly”andthensomeadsshowuptensecondslater,mostuserswon’t complain,andmanywon’tevennotice.However,ifyoudisplaythepageintheoppositeorder,withthe slowadsfirstandthecontentafterward,youmightrisklosingmanyofyourusers,eventhoughthetotal pageloadtimeisthesame.
Websitesthatonepersonbuildsandmaintainscanbenefitfromthisapproachasmuchaslarger websitescan(imagineakartracerwithsomeFormulaOneparts).Afastsitewillattractmoretrafficand morereturnvisitorsthanaslowone.Youmightbeabletogetalongwithasmallerserveroraless expensivehostingplan.Yourusersmightvisitmorepages.
Asanexampleofwhat’spossiblewithASP.NETandSQLServerwhenyoufocusonarchitectureand performance,onesoftwaredeveloperbyhimselfbuiltthesitepof.com,andin2009,itwasoneofthe highest-trafficsitesinCanada.Thesiteservesmorethan45millionvisitorspermonth,with1.2billion pageviewspermonth,or500to600pagespersecond.Yetitonlyusesthreeload-balancedwebservers, withdualquad-coreCPUsand8GBRAM,plusafewdatabaseservers,alongwithacontentdistribution network(CDN).TheCPUsonthewebserversaverage30percentbusy.Idon’tknowmanydetailsabout theinternalsofthatsite,butafterlookingattheHTMLitgenerates,I’mconfidentthatyoucouldusethe techniquesI’mprovidinginthisbooktoproduceacomparablesitethat’sevenfaster.
Unfortunately,there’snofreelunch:buildinganultra-fastsitedoestakemorethoughtand planningthanaquick-and-dirtyapproach.Italsotakesmoredevelopmenteffort,althoughusuallyonly inthebeginning.Overthelongrun,maintenanceanddevelopmentcostscanactuallybesignificantly less,andyoushouldbeabletoavoidanycostlyground-uprewrites.Intheend,Ihopeyou’llagreethat thebenefitsareworththeeffort.
End-to-EndWebPageProcessing
AcommonwaytothinkabouttheWebisthatthereisabrowserononeendofanetworkconnection andawebserverwithadatabaseontheotherend,asinFigure1-1.
Figure1-1.Simplifiedwebarchitecturemodel
Thesimplifiedmodeliseasytoexplainandunderstand,anditworksfineuptoapoint.However, quiteafewothercomponentsareactuallyinvolved,andmanyofthemcanhaveanimpacton performanceandscalability.Figure1-2showssomeofthemforwebsitesbasedonASP.NETand SQLServer.
Figure1-2.Webarchitecturecomponentsthatcanimpactperformance
AllofthecomponentsinFigure1-2canintroducedelayintothetimeittakestoloadapage,butthat delayismanageabletosomedegree.Additionalinfrastructure-orientedcomponentssuchasrouters, loadbalancers,andfirewallsaren’tincludedbecausethedelaytheyintroduceisgenerallynotvery manageablefromasoftwarearchitectureperspective.
Inthefollowinglist,I’vesummarizedtheprocessofloadingawebpage.Eachofthesestepsoffers opportunitiesforoptimizationthatI’lldiscussindetaillaterinthebook:
1. First,thebrowserlooksinitslocalcachetoseewhetheritalreadyhasacopyof thepage.SeeChapter2.
2. Ifthepageisn’tinthelocalcache,thenthebrowserlooksuptheIPaddressof theweborproxyserverusingDNS.Thebrowserandtheoperatingsystem haveeachhaveseparateDNScachestostoretheresultsofpreviousqueries.If theaddressisn’talreadyknownorifthecacheentryhastimedout,thena nearbyDNSserverisusuallyconsultednext(it’softeninalocalrouter,for example).SeeChapter10.
Proxyserverscanbeeithervisibleortransparent.Avisibleproxyisonethatthe user’sbrowseroroperatingsystemisawareof.Theyaresometimesusedat largecompanies,forexample,tohelpimprovewebperformancefortheir employeesorsometimesforsecurityorfilteringpurposes.Atransparentproxy interceptsalloutgoingTCPconnectionsonport80(HTTP),regardlessoflocal clientsettings.Ifthelocalproxydoesn’thavethedesiredcontent,thenthe HTTPrequestisforwardedtothetargetwebserver.SeeChapters2and3.
4. SomeISPsalsouseproxiestohelpimproveperformancefortheircustomers andtoreducethebandwidththeyuse.Aswiththelocalproxy,ifthecontent isn’tavailableintheISPproxycache,thentherequestisforwardedalong.See Chapter3.
5. Thenextstopisawebserveratthedestinationsite.Alargesitewillhavea numberofload-balancedwebservers,anyofwhichwillbeabletoacceptand processincomingrequests.Eachmachinewillhaveitsownlocaldiskand separatecachesattheoperatingsystemdriverlevel(http.sys),inInternet InformationServices(IIS),andinASP.NET.SeeChapters3through7.
6. Iftherequestedpageneedsdatafromthedatabase,thenthewebserverwill openaconnectiontooneormoredatabaseservers.Itcanthenissuequeries forthedataitneeds.ThedatamightresideinRAMcacheinthedatabase,orit mightneedtobereadinfromdisk.SeeChapters8and9.
7. Whenthewebserverhasthedataitneeds,itdynamicallycreatesthe requestedpageandsendsitbacktotheuser.Iftheresultshaveappropriate HTTPresponseheaders,theycanbecachedinmultiplelocations.See Chapters3and4.
8. Whentheresponsearrivesattheclient,thebrowserparsesitandrendersitto thescreen.SeeChapter2. OverviewofPrinciples
Thefirstandmostimportantruleofbuildingahigh-performancesiteisthatperformancestartswiththe applicationitself.Ifyouhaveapagewithaloopcountingtoagazillion,forexample,nothingI’m describingwillhelp.
PerformancePrinciples
Withtheassumptionofasoundimplementation,thefollowingaresomehigh-impactcorearchitectural principlesforperformanceandscalability:
- Focusonperceivedperformance.Usersarehappieriftheyquicklyseearesponse aftertheyclick.It’sevenbetterifwhattheyseefirstistheinformationthey’re mostinterestedin.SeeChapter2.
- Reduceroundtrips.Everyroundtripisexpensive,whetherit’sbetweentheclient andthewebserverorbetweenthewebserverandthedatabase.“Chattiness”is oneofthemostcommonkillersofgoodsiteperformance.Youcaneliminatethese typesofroundtripsbycaching,combiningrequests(batching),combiningsource filesordata,combiningresponses(multipleresultsets),workingwithsetsofdata, andothersimilartechniques.SeeChapters2through8.
- Cacheatalltiers.Cachingisimportantatmoststepsofthepagerequestprocess.
Youshouldleveragethebrowser’scache,cookies,on-pagedata(hiddenfieldsor ViewState),proxies,theWindowskernelcache(http.sys),theIIScache,the ASP.NETapplicationcache,pageandfragmentoutputcaching,theASP.NET cacheobject,server-sideper-requestcaching,databasedependencycaching, distributedcaching,andcachinginRAMatthedatabase.SeeChapters3and8.
- Minimizeblockingcalls.ASP.NETprovidesonlyalimitednumberofworker threadsforprocessingwebpagerequests.Iftheyareallblockedbecausetheyare waitingforcompletionoflong-runningtasks,theruntimewillqueueupnew incomingHTTPrequestsinsteadofexecutingthemrightaway,andyourweb serverthroughputwilldeclinedramatically.Youcouldhavealongqueueof requestswaitingtobeprocessed,eventhoughyourserver’sCPUutilizationwas verylow.Minimizingtheamountoftimethatworkerthreadsareblockedisa cornerstoneofbuildingascalablesite.Youcandothisusingfeaturessuchas asynchronouspages,asyncHttpModules,asyncI/O,asyncdatabaserequests, backgroundworkerthreads,andServiceBroker.Maximizingasynchronous activityinthebrowserisakeyaspectofreducingbrowserpageloadtimesbecause itallowsthebrowsertodomultiplethingsatthesametime.SeeChapters2and Chapters5through8.
- OptimizediskI/Omanagement.Disksarephysicaldevices;theyhaveplatters thatspinandread/writeheadsthatmovebackandforth.Rotationandhead movement(diskseeks)taketime.DisksworkmuchfasterwhenyoumanageI/O toavoidexcessiveseeks.ThedifferenceinperformancebetweensequentialI/O andrandomI/Ocaneasilybe40to1ormore.Thisisparticularlyimportanton databaseservers,wherethedatabaselogiswrittensequentially.Properhardware selectionandconfigurationplaysabigrolehere,too,includingchoosingthetype andnumberofdrives,usingthebestRAIDlevel,usingtherightnumberoflogical drivesorLUNs,andsoon.SolidStateDisks(SSDs)havenomovingparts,andcan bemuchfasterforcertainI/Opatterns.SeeChapters8and10.
SecondaryTechniques
Youcanoftenapplyanumberofsecondarytechniqueseasilyandquicklythatwillhelpimprovesystem- levelperformanceandscalability.Aswithmostofthetechniquesdescribedhere,it’seasiertoapply themeffectivelywhenyoudesignthemintoyourwebsitefromthebeginning.Aswithsecurityand qualityrequirements,thelaterinthedevelopmentprocessthatyouaddressperformanceandscalability requirements,themoredifficulttheproblemstendtobe.I’vesummarizedafewexamplesofthese techniquesinthefollowinglist:
- Understandbehavior.Byunderstandingthewaythatthebrowserloadsaweb page,youcanoptimizeHTMLandHTTPtoreducedownloadtimeandimprove bothtotalrenderingspeedandperceivedspeed.SeeChapter2.
- AvoidfullpageloadsbyusingAjaxandplainJavaScript.Youcanuseclient-side fieldvalidationandothertypesofrequestgatingwithJavaScripttocompletely avoidsomepagerequests.YoucanuseAjaxtorequestsmallamountsofdatathat canbedynamicallyinsertedintothepageorintoarichuserinterface.See Chapter2.
- Avoidsynchronousdatabasewritesoneveryrequest.Heavydatabasewritesarea commoncauseofscalabilityproblems.Incorrectuseofsessionstateisafrequent sourceofproblemsinthisarea,sinceithastobebothreadandwritten(and deserializedandreserialized)witheveryrequest.Youmaybeabletousecookies toreduceoreliminatetheneedforserver-sidesessionstatestorage.SeeChapters 5and8.
- Monitoringandinstrumentation.Asyoursitegrowsintermsofbothcontentand users,instrumentationcanprovidevaluableinsightsintoperformanceand scalabilityissues,whilealsohelpingtoimproveagilityandmaintainability.You cantimeoff-boxcallsandcomparetheresultsagainstperformancethresholds. YoucanuseWindowsperformancecounterstoexposethosemeasurementstoa richsetoftools.Centralizedmonitoringcanprovidetrendanalysistosupport capacityplanningandtohelpidentifyproblemsearly.SeeChapter10.
- UnderstandhowSQLServermanagesmemory.Forexample,whenaT-SQL commandmodifiesadatabase,theserverdoesasynchronous(andsequential) writetothedatabaselog.Onlyafterthewritehasfinishedwilltheserverreturnto therequestor.Themodifieddatapagesarestillinmemory.Theywillstaythere untilSQLServerneedsthememoryforotherrequests;theywillbewrittentothe datafilebythebackgroundlazywriterthread.ThismeansthatSQLServercan processsubsequentreadrequestsforthesamedataquicklyfromcache.Italso meansthatthespeedofthelogdiskhasadirectimpactonyourdatabase’swrite throughput.SeeChapter8.
- Effectiveuseofpartitioningatthedatatier.Oneofthekeystoaddressing databasescalabilityistopartitionyourdata.Youmightreplicateread-onlydatato agroupofload-balancedserversrunningSQLExpress,oryoumightpartition writabledataamongseveralseversbasedonaparticularkey.Youmightsplitup datainasinglelargetableintomultiplepartitionstoavoidperformanceproblems whenthedataisprunedorarchived.SeeChapter8.
Iwilldiscusstheseandothersimilartechniquesatlengthinthechaptersahead. Whatthisbookisnotaboutislow-levelcodeoptimization;myfocushereismostlyonthehigh- impactaspectsofyourapplicationarchitectureanddevelopmentprocess.
EnvironmentandToolsUsedinThisBook
Althoughcross-browsercompatibilityisimportant,inkeepingwiththepointImadeearlierabout focusingonthehigh-impactaspectsofyoursystem,I’vefoundthatfocusingdevelopmentandtuning effortsonthebrowsersthatcomprisethetop90percentorsoinusewillbringmostoftherestforfree. Youshouldbeabletomanagewhateverquirkinessmightbeleftafterwardonanexceptionbasis,unless you’rebuildingasitespecificallyorientedtowardoneoftheminoritybrowsers.
Ialsodon’tconsiderthecaseofbrowserswithoutJavaScriptorcookiesenabledtoberealistic anymore.Withoutthosefeatures,theWebbecomesafairlybarrenplace,soIthinkofthemasbeinga givenforrealusers;searchenginesandotherbotsareanentirelydifferentstory,ofcourse.
AsofApril2012,themostpopularbrowsersaccordingtoNetApplicationswereInternetExplorer with54percent,Firefoxwith21percent,andChromewith19percent.Theremaining6percentwassplit betweenSafari,Opera,andothers.
SoftwareToolsandVersions
ThespecifictoolsthatI’veusedforthecodeexamplesandfiguresarelistedinTable1-2,includinga roughindicationofcost.Asingle$indicatesapriceunderUS$100,$$isbetween$100and$1,000,and $$$ismorethan$1,000.
Table1-2.SoftwareToolsandVersions Software Version Cost
AdobePhotoshop CS5 $$ Contig1.6
Freedownload
Software Version Cost
ExpressionWeb 4.0.1303.0SP2 $$ FiddlerWebDebugger 2.3.9 Freedownload Firebug 1.10 Freedownload(Firefoxplug-in) Firefox11.0
Freedownload InternetExplorer 8and9 Freedownload LogParser 2.2 Freedownload .NETFramework 4.5 Freedownload Office 2010Ultimate $$ Silverlight5
Freedownload SQLServer 2012RC0Developer,
StandardandEnterprise $ $$$
SQLServerDataTools April2012 Freedownload(VSplug-in) SQLServerFeaturePack October2008 Freedownload SystemCenterOperationsManager 2012RC $$ VisualStudio 2010PremiumSP1,
11DeveloperPreview&Beta $$$
WindowsServer 2008R2Standard $$ Windows 7Professionalx64 $$ Wireshark1.6.4
Freedownload YSlow 3.0.4 Freedownload(Firefoxplug-in)
MostofthecodethatIdiscussanddemonstratewillalsoworkinVisualStudioWebExpress,which isafreedownload. Terminology
Seetheglossaryfordefinitionsofbusinessintelligence(BI)-specificterminology.
TypographicConventions
Iamusingthefollowingtypographicconventions:
- Italics:Termdefinitionsandemphasis
- Bold:Textasyouwouldseeitonthescreen
- Monospace:Code,URLs,filenames,andothertextasyouwouldtypeit
Author’sWebSite
Mywebsiteatonsofmanyofthewebpagesusedassamples ordemonstrations,alongwithcodedownloadsandlinkstorelatedresources.
Summary