Building Scalable .NET Web Applications

  • Published on

  • View

  • Download




Application Lifecycle Management with Visual Studio 2010Building Scalable .NET Web ApplicationsBuu Nguyen, MVP (ASP.NET) Confidential1AgendaScalabilityScaling Web Client TierScaling Web & Application Server TiersScaling Database TierScalabilityScalabilityA system is scalable when it can accommodate more loads and larger data set by increasing hardware powerScalability implies performance but not the other way aroundVertical Scaling vs. Horizontal Scaling't Underestimate Vertical ScalingStackOverflow.com1M page views per day500K questions and millions of posts817th largest siteHardware2 web servers (1 Xeon 4-core processor, 8GB RAM)1 database server (2 Xeon 4-core processors, 48GB RAM)SoftwareIIS 7.5 on Windows Server 2008 R2HAProxy (inside Linux VM)SQL Server 2008 EnterpriseASP.NET MVC and .NET 3.5Source: Confidential6Scaling Web Client TierClient Resource ManagementMinify and Gzip JavaScript, CSSMS Ajax Minifier, YUI Compressor, Google Closure CompilerCombine JavaScript and CSSMS Script LoaderCSS on topJavaScript at bottom Add Expires headers for all resources8CombresCurrently 2.0, Apache license FeaturesCombine, compress, minify, cache (server & client) JavaScript and CSS resourcesAutomatic change detectionExtensible architectureUser Guide Confidential9JavaScript & Ajax OptimizationReverse-Ajax (Comet) instead of pollingUse dedicated Comet serverExamples: StreamHub, Meteor to timer by chunking processingAvoid outer-scope lookupSplit initial payload (i.e. before onload)Employ non-blocking JS loading techniquesMS Ajax Library Script Loader, YUI Script Loader10Scaling Web and Application Server TiersLoad BalancingThe act of properly distribute workload across machines/resources in a clusterApproachesDNS A recordsPoor mans hand-coded redirectionSoftware, e.g. HAProxy, NLB, LVSHardware, e.g. F5ConsiderationsSession stateView state12Domain ShardingPartition resources across different hostsBy type, e.g. static/dynamic, JS/CSS etc.By functionality, e.g. forum moduleBenefitsBalance loadsParallel downloadsAvoid redundant cookiesIsolated optimization13Content Delivery Network (CDN)ServicesMicrosoft, Google, Akamai etc.Static resources onlyBenefitsAs domain shardingRedundancy and availabilitySmart DNS routingHigh chance of browser cache hit14Distributed CacheEnable persistent cache for a server clusterFast (in-memory) Distributed ignorant by clientsSolutionsAppFabric Cache (Velocity)Support POCO, XML, binaryASP.NET integrationExtensible cache providerOthers: memcached15ConcurrencyIts a waste of cores if you dont have enough threadsSolutionsParallel Task Library (PTL)PLINQF#16OthersCompensation over distributed transactionsAsynchronous over synchronous servicesRelated: asynchronous controller in MVC 2.017Side Notes on Entity FrameworkSingleton ObjectContext doesnt fit web appsNot thread-safeConsume RAMMight fit rich clientOne OC per request is the common strategy for web appsUse compiled query to reuse generated EF command tree18Scaling Database TierIndexesClustered vs. non-clustered indexesGuidelinesIndexing columns in WHERE and SELECTIndexing columns with highly unique valuesCluster-indexing primary keysAvoid over-indexing 20ReplicationPublisher-subscribers configurationi.e. master-slave in MySQLWrite to subscribers wont propagate backCommon optionsTransactional (near real-time)Snapshot (time interval)Suitable for read-intensive applications21ReplicationPeer-2-peer configurationi.e. master-master in MySQLIssuesIdentity conflictsNo true copy at any point of timeHigh network latency22PartitioningVertical Partitioning (Clustering)Distribute tables into multiple DBs, each representing a cluster of related tables, e.g.Customer DB, product DB, forum DB etc.Application layer aggregates dataHorizontal Partitioning (Sharding)Distribute table rows into logical groups, e.g. US customers, European customersApplication layer picks shards & aggregates data23NoSQLPartitioning makes relational databases not so relational any moreComplexity in DB design & application layerNoSQL, not-only-relational, is about DBs built with scalability in mind Sacrifice integrity & ACID to a certain extentApache CassandraAuto load balancingIdentical nodesElastic capacityFlexible schemaOthers: MongoDB, Voldermort, Tokyo Cabinet24