Building Scalable .NET Web Applications

24
Building Scalable .NET Web Applications Buu Nguyen, MVP (ASP.NET) www.buunguyen.net/blog [email protected] Microsoft Confidential 1
  • date post

    18-Oct-2014
  • Category

    Technology

  • view

    13.411
  • download

    1

description

 

Transcript of Building Scalable .NET Web Applications

Page 1: Building Scalable .NET Web Applications

Building Scalable .NET Web Applications

Buu Nguyen, MVP (ASP.NET)www.buunguyen.net/[email protected]

Microsoft Confidential 1

Page 2: Building Scalable .NET Web Applications

Agenda

− Scalability− Scaling Web Client Tier− Scaling Web & Application Server

Tiers− Scaling Database Tier

Page 3: Building Scalable .NET Web Applications

Scalability

Page 4: Building Scalable .NET Web Applications

Scalability

− A system is scalable when it can accommodate more loads and larger data set by increasing hardware power

− Scalability implies performance but not the other way around

Page 5: Building Scalable .NET Web Applications

Vertical Scaling vs. Horizontal Scaling

http://www.gigaspaces.com/files/pics/online_gaming_scalability_diagram_website.jpg

http://www.ourofficebox.co.uk/Images/scalability2%20750.png

Page 6: Building Scalable .NET Web Applications

Microsoft Confidential

6

Don't Underestimate Vertical Scaling− StackOverflow.com

− 1M page views per day− 500K questions and millions of posts− 817th largest site

− Hardware− 2 web servers (1 Xeon 4-core processor, 8GB RAM)− 1 database server (2 Xeon 4-core processors, 48GB RAM)

− Software− IIS 7.5 on Windows Server 2008 R2− HAProxy (inside Linux VM)− SQL Server 2008 Enterprise− ASP.NET MVC and .NET 3.5

− Source: http://www.microsoft.com/casestudies/Case_Study_Detail.aspx?casestudyid=4000006676

Page 7: Building Scalable .NET Web Applications

Scaling Web Client Tier

Page 8: Building Scalable .NET Web Applications

Client Resource Management− Minify and Gzip JavaScript, CSS

− MS Ajax Minifier, YUI Compressor, Google Closure Compiler

− Combine JavaScript and CSS− MS Script Loader

− CSS on top− JavaScript at bottom − Add Expires headers for all resources

Page 9: Building Scalable .NET Web Applications

Microsoft Confidential

9

Combres

− Currently 2.0, Apache license− http://combres.codeplex.com/

− Key Features− Combine, compress, minify, cache (server &

client) JavaScript and CSS resources− Automatic change detection− Extensible architecture

− User Guide− http://www.codeproject.com/KB/aspnet/combres

2.aspx

Page 10: Building Scalable .NET Web Applications

JavaScript & Ajax Optimization− Reverse-Ajax (Comet) instead of

polling− Use dedicated Comet server

− Examples: StreamHub, Meteor− http://cometdaily.com/maturity.html

− Yield to timer by chunking processing− Avoid outer-scope lookup− Split initial payload (i.e. before onload)− Employ non-blocking JS loading

techniques− MS Ajax Library Script Loader, YUI Script

Loader

Page 11: Building Scalable .NET Web Applications

Scaling Web and Application Server Tiers

Page 12: Building Scalable .NET Web Applications

Load Balancing

− The act of properly distribute workload across machines/resources in a cluster

− Approaches− DNS’ “A” records− Poor man’s hand-coded redirection− Software, e.g. HAProxy, NLB, LVS− Hardware, e.g. F5

− Considerations− Session state− View state

Page 13: Building Scalable .NET Web Applications

Domain Sharding

− Partition resources across different hosts− By type, e.g. static/dynamic, JS/CSS etc.− By functionality, e.g. forum module

− Benefits− Balance loads− Parallel downloads− Avoid redundant cookies− Isolated optimization

Page 14: Building Scalable .NET Web Applications

Content Delivery Network (CDN)− Services

− Microsoft, Google, Akamai etc.− Static resources only

− Benefits− As domain sharding− Redundancy and availability− Smart DNS routing− High chance of browser cache hit

Page 15: Building Scalable .NET Web Applications

Distributed Cache

− Enable persistent cache for a server cluster− Fast (in-memory) − Distributed – ignorant by clients

− Solutions− AppFabric Cache (Velocity)

− Support POCO, XML, binary− ASP.NET integration

− Extensible cache provider− Others: memcached

Page 16: Building Scalable .NET Web Applications

Concurrency

− It’s a waste of cores if you don’t have enough threads

− Solutions− Parallel Task Library (PTL)− PLINQ− F#

Page 17: Building Scalable .NET Web Applications

Others

− Compensation over distributed transactions

− Asynchronous over synchronous services− Related: asynchronous controller in MVC

2.0

Page 18: Building Scalable .NET Web Applications

Side Notes on Entity Framework− Singleton ObjectContext doesn’t fit

web apps− Not thread-safe− Consume RAM− Might fit rich client

− One OC per request is the common strategy for web apps

− Use compiled query to reuse generated EF command tree

Page 19: Building Scalable .NET Web Applications

Scaling Database Tier

Page 20: Building Scalable .NET Web Applications

Indexes

− Clustered vs. non-clustered indexes− Guidelines

− Indexing columns in WHERE and SELECT− Indexing columns with highly unique

values− Cluster-indexing primary keys− Avoid over-indexing

Page 21: Building Scalable .NET Web Applications

Replication

− Publisher-subscribers configuration−i.e. master-slave in MySQL−Write to subscribers won’t

propagate back−Common options

− Transactional (near real-time)− Snapshot (time interval)

−Suitable for read-intensive applications

Page 22: Building Scalable .NET Web Applications

…Replication

− Peer-2-peer configuration−i.e. master-master in MySQL−Issues

− Identity conflicts− No “true” copy at any point of time− High network latency

Page 23: Building Scalable .NET Web Applications

Partitioning

Vertical 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 data

Horizontal Partitioning (Sharding)− Distribute table rows into

logical groups, e.g. − US customers, European

customers− Application layer picks

shards & aggregates data

Page 24: Building Scalable .NET Web Applications

NoSQL− Partitioning makes relational databases not

so relational any more− Complexity in DB design & application layer

− NoSQL, “not-only-relational”, is about DBs built with scalability in mind − Sacrifice integrity & ACID to a certain extent

− Apache Cassandra− Auto load balancing− Identical nodes− Elastic capacity− Flexible schema

− Others: MongoDB, Voldermort, Tokyo Cabinet