Building Scalable .NET Web Applications

15,253 views

Published on

Published in: Technology

Building Scalable .NET Web Applications

  1. 1. Building Scalable .NET Web Applications<br />Buu Nguyen, MVP (ASP.NET)<br />www.buunguyen.net/blog<br />buunguyen@kms-technology.com<br />Microsoft Confidential<br />1<br />
  2. 2. Agenda<br />Scalability<br />Scaling Web Client Tier<br />Scaling Web & Application Server Tiers<br />Scaling Database Tier<br />
  3. 3. Scalability<br />
  4. 4. Scalability<br />A system is scalable when it can accommodate more loads and larger data set by increasing hardware power<br />Scalability implies performance but not the other way around<br />
  5. 5. Vertical Scaling vs. Horizontal Scaling<br />http://www.ourofficebox.co.uk/Images/scalability2%20750.png<br />http://www.gigaspaces.com/files/pics/online_gaming_scalability_diagram_website.jpg<br />
  6. 6. Don't Underestimate Vertical Scaling<br />StackOverflow.com<br />1M page views per day<br />500K questions and millions of posts<br />817th largest site<br />Hardware<br />2 web servers (1 Xeon 4-core processor, 8GB RAM)<br />1 database server (2 Xeon 4-core processors, 48GB RAM)<br />Software<br />IIS 7.5 on Windows Server 2008 R2<br />HAProxy (inside Linux VM)<br />SQL Server 2008 Enterprise<br />ASP.NET MVC and .NET 3.5<br />Source: http://www.microsoft.com/casestudies/Case_Study_Detail.aspx?casestudyid=4000006676<br />Microsoft Confidential<br />6<br />
  7. 7. Scaling Web Client Tier<br />
  8. 8. Client Resource Management<br />Minify and Gzip JavaScript, CSS<br />MS Ajax Minifier, YUI Compressor, Google Closure Compiler<br />Combine JavaScript and CSS<br />MS Script Loader<br />CSS on top<br />JavaScript at bottom <br />Add Expires headers for all resources<br />
  9. 9. Combres<br />Currently 2.0, Apache license<br />http://combres.codeplex.com/<br />Key Features<br />Combine, compress, minify, cache (server & client) JavaScript and CSS resources<br />Automatic change detection<br />Extensible architecture<br />User Guide<br />http://www.codeproject.com/KB/aspnet/combres2.aspx<br />Microsoft Confidential<br />9<br />
  10. 10. JavaScript & Ajax Optimization<br />Reverse-Ajax (Comet) instead of polling<br />Use dedicated Comet server<br />Examples: StreamHub, Meteor<br />http://cometdaily.com/maturity.html<br />Yield to timer by chunking processing<br />Avoid outer-scope lookup<br />Split initial payload (i.e. before onload)<br />Employ non-blocking JS loading techniques<br />MS Ajax Library Script Loader, YUI Script Loader<br />
  11. 11. Scaling Web and Application Server Tiers<br />
  12. 12. Load Balancing<br />The act of properly distribute workload across machines/resources in a cluster<br />Approaches<br />DNS’ “A” records<br />Poor man’s hand-coded redirection<br />Software, e.g. HAProxy, NLB, LVS<br />Hardware, e.g. F5<br />Considerations<br />Session state<br />View state<br />
  13. 13. Domain Sharding<br />Partition resources across different hosts<br />By type, e.g. static/dynamic, JS/CSS etc.<br />By functionality, e.g. forum module<br />Benefits<br />Balance loads<br />Parallel downloads<br />Avoid redundant cookies<br />Isolated optimization<br />
  14. 14. Content Delivery Network (CDN)<br />Services<br />Microsoft, Google, Akamai etc.<br />Static resources only<br />Benefits<br />As domain sharding<br />Redundancy and availability<br />Smart DNS routing<br />High chance of browser cache hit<br />
  15. 15. Distributed Cache<br />Enable persistent cache for a server cluster<br />Fast (in-memory) <br />Distributed – ignorant by clients<br />Solutions<br />AppFabric Cache (Velocity)<br />Support POCO, XML, binary<br />ASP.NET integration<br />Extensible cache provider<br />Others: memcached<br />
  16. 16. Concurrency<br />It’s a waste of cores if you don’t have enough threads<br />Solutions<br />Parallel Task Library (PTL)<br />PLINQ<br />F#<br />
  17. 17. Others<br />Compensation over distributed transactions<br />Asynchronous over synchronous services<br />Related: asynchronous controller in MVC 2.0<br />
  18. 18. Side Notes on Entity Framework<br />Singleton ObjectContext doesn’t fit web apps<br />Not thread-safe<br />Consume RAM<br />Might fit rich client<br />One OC per request is the common strategy for web apps<br />Use compiled query to reuse generated EF command tree<br />
  19. 19. Scaling Database Tier<br />
  20. 20. Indexes<br />Clustered vs. non-clustered indexes<br />Guidelines<br />Indexing columns in WHERE and SELECT<br />Indexing columns with highly unique values<br />Cluster-indexing primary keys<br />Avoid over-indexing <br />
  21. 21. Replication<br />Publisher-subscribers configuration<br />i.e. master-slave in MySQL<br />Write to subscribers won’t propagate back<br />Common options<br />Transactional (near real-time)<br />Snapshot (time interval)<br />Suitable for read-intensive applications<br />
  22. 22. …Replication<br />Peer-2-peer configuration<br />i.e. master-master in MySQL<br />Issues<br />Identity conflicts<br />No “true” copy at any point of time<br />High network latency<br />
  23. 23. Partitioning<br />Vertical Partitioning (Clustering)<br />Horizontal Partitioning (Sharding)<br />Distribute tables into multiple DBs, each representing a cluster of related tables, e.g.<br />Customer DB, product DB, forum DB etc.<br />Application layer aggregates data<br />Distribute table rows into logical groups, e.g. <br />US customers, European customers<br />Application layer picks shards & aggregates data<br />
  24. 24. NoSQL<br />Partitioning makes relational databases not so relational any more<br />Complexity in DB design & application layer<br />NoSQL, “not-only-relational”, is about DBs built with scalability in mind <br />Sacrifice integrity & ACID to a certain extent<br />Apache Cassandra<br />Auto load balancing<br />Identical nodes<br />Elastic capacity<br />Flexible schema<br />Others: MongoDB, Voldermort, Tokyo Cabinet<br />

×