SlideShare a Scribd company logo
1 of 35
Scaling ASP.NET websites
from thousands to millions
of users
Lessons learnt from scaling Web 2.0 apps to millions of
users


Omar AL Zabir
Chief Architect, SaaS Platform, BT
omaralzabir.com
omaralzabir@gmail.com
Twitter.com/omaralzabir


Book “Building Web 2.0 portal using ASP.NET 3.5” from O‟Reilly
How to blow up a website

     Get some super fast fiber broadband connection.
     Run this on couple of quad core desktops:


for (int j = 0; j < 100; j ++)
{
  for (int i = 0; i < 100; i ++)
  {
    var client = new WebClient();
    client.DownloadStringAsync(
        new Uri("http://www.microsoft.com/MISReport.aspx");
  }
  Thread.Sleep(500);
}
Prevent App Level DOS attack

 Application Level DOS attacks are attempts to hit
  your expensive pages too frequently so that you
  have 100% CPU and your site goes down. For ex,
  hitting some expensive Report page continuously.
 You can run out of ASP.NET Threads and stop
  responding completely to any request.
 I‟m not talking about Network level DOS attacks
  like TCP SYN flood or DDOS attacks that hardware
  firewalls can prevent.
 Firewalls don‟t block legitimate requests.
Prevent App level DOS attacks
 Protect only expensive pages. Pages that are unlikely to
  get hit too many times within a short duration.
 Build a HttpModule and hook on OnInit event.
 Store which IP is making how many number of hits in
  last 10 minutes. Store the table in some in-memory
  cache.
 If the threshold has exceeded, stop responding to that
  IP. Call Response.End()
 Solution is here:
  http://tinyurl.com/omarDOS

   IP         Hits in last 10 mins
   128.0.0.1 500
   128.0.0.2 345
ASP.NET ProcessModel Optimization
 ASP.NET ProcessModel Defaults
    maxWorkerThreads = 20
    maxIOThreads = 20
    memoryLimit = 60
 Nowadays, servers are way too
  powerful. You don‟t need to be
  conservative.
 Change default process model
  setting in machine.config to make
  best use of CPU power.
ASP.NET Pipeline Optimization

 Default ASP.NET Pipeline has several components that intercept
  each and every request. Thus they add extra processing overhead
  on every request.
 Multiply the overhead by hundreds of requests per second – you
  get a significant overhead.
ASP.NET Pipeline Optimization
 Remove what you don‟t need.
 If you are using Forms Authentication, SQL Server
  storage, no web.config based role based permission then
  you can remove most of them:
Prevent large ASP.NET cookies on
static content
 Each and every request, even static files, get the
  ASP.NET cookies sent.
 517 bytes of worthless data per request.
 Avg pages have 40 resources. 40 x 517 = 20 KB.
 1M page view = 20 GB of data upload to server.

Cookie:
.DBANON=w3kYczsH8Wvzs6MgryS4JYEF0N-8ZR6aLRSTU9KwVaGa
ydD6WwUHD7X9tN8vBgjgzKf3r3SJHusTYFjU85yYfnunyCeuExcZs8
95JK9Fk1HS68ksGwm3QpxnRZvpDBAfJKEUKee2OTlND0gi43qwwtI
PLeY1; ASP.NET_SessionId=bmnbp155wilotk45gjhitoqg; .DBAUTH
12=2A848A8C200CB0E8E05C6EBA8059A0DBA228FC5F6EDD2940
1C249D237812344C15B3C5C57D6B776037FAA8F14017880E57B
DC14A7963C58B0A0B30229AF0123A6DF56601D814E75525E7DC
A9AD4A0EF200832B39A1F35A5111092F0805B0A8CD3D2FD5E3A
B6176893D86AFBEB68F7EA42BE61E89537DEAA3279F3B576D0C
44BA00B9FA1D9DD3EE985F37B0A5A134ADC0EA9C548D
Prevent ASP.NET cookies on static
content
 Setup a new website in IIS, map to the same code folder.
 Map static.yoursite.com host header to that website.
 Prefix all css, js, image links with http://static.yoursite.com/
 Add a Global.asax‟s EndResponse event on the new website.
 HttpContext context = HttpContext.Current;
 if (context.Request.Url.Host == “static.yoursite.com”)
 {
       List<string> cookiesToClear = new List<string>();
       foreach (string cookieName in context.Request.Cookies)
       {
         HttpCookie cookie =
       context.Request.Cookies[cookieName];
         cookiesToClear.Add(cookie.Name);
       }
       foreach (string name in cookiesToClear)
       {
         HttpCookie cookie = new HttpCookie(name,
       string.Empty);
         cookie.Expires = DateTime.Today.AddYears(-1);
         context.Response.Cookies.Set(cookie);
System.net optimization

 If you are using HttpWebRequest or WebClient or any
  other TCP/IP operation, increase the max connection
  limit.
 Default is 2 per IP.
 WCF service calls are limited by this setting.
 Unless you suspect rogue clients, set some reasonably
  high number on webservers, but moderate number on
  desktop clients.
System.net default setting is
suboptimal




                          Bottleneck




      Max 2 concurrent calls
ASP.NET Profile Provider

 Anonymous Provider creates one anonymous user in
  database on every first hit to Profile object‟s Custom
  properties:




 The SP aspnet_Profile_GetProperties gets called when
  you access Profile object for first time in a request. And
  this SP is slooooooooow!
ASP.NET Profile Provider
 The slow SP that gets fired when you access custom Profile
  properties:


CREATE PROCEDURE [dbo].[    aspnet_Profile_GetProperties]
        @ApplicationName      nvarchar(256), @UserId …
AS
BEGIN

      DECLARE @ApplicationId uniqueidentifier
      SELECT @ApplicationId = NULL
      SELECT @ApplicationId = ApplicationId
             FROM dbo.aspnet_Applications
                 WHERE LOWER(@ApplicationName) = LoweredApplicationName

…
…
…
      IF (@@ROWCOUNT > 0)
      BEGIN
          UPDATE dbo.aspnet_Users
         SET LastActivityDate=@CurrentTimeUtc
         WHERE UserId = @UserId
      END
END
ASP.NET Profile Provider

 The slow SP‟s execution plan
ASP.NET Profile Provider
 Don‟t update LastActivityDate when Profile object is
  loaded. Do it only when Profile object is updated.
 Update once every hour or so. If LastActivityDate <
  DateTime.Now.AddHours(-1) then update. No need to do
  per request or too frequently.
 Hard code the Application ID to avoid one lookup inside
  the SP.
 tinyurl.com/omarGetProp
ASP.NET Membership
   ASP.NET Membership Provider Stored Procs use default
    transaction isolation level, which is Serializable.

   Transaction (Process ID ##) was deadlocked on lock resources
   with another process and has been chosen as the deadlock
   victim. Rerun the transaction.
   Timeout Expired. The Timeout Period Elapsed Prior To
   Completion Of The Operation Or The Server Is Not Responding.


ALTER PROCEDURE [dbo].[aspnet_Profile_GetProperties]
@ApplicationName nvarchar(256),
@UserName nvarchar(256),
@CurrentTimeUtc datetime
AS
BEGIN

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
ASP.NET Membership Query
 Common queries that brings down hell:
    Select * from aspnet_users where UserName = „blabla‟
    Select * from aspnet_membership where Email =
     “someone@somewhere.com”
 What‟s wrong with these queries?
ASP.NET Membership Queries

 Let's look at the indexes:
 Table: aspnet_users
     Clustered Index = ApplicationID, LoweredUserName
     NonClustered Index = ApplicationID, LastActivityDate
     Primary Key = UserID
 Table: aspnet_membership
     Clustered Index = ApplicationID, LoweredEmail
     NonClustered = UserID
 Table: aspnet_Profile
     Clustered Index = UserID


 DO NOT use Email or UserName fields in WHERE clause. They are not
  part of the index instead LoweredUserName and LoweredEmail fields
  are in conjunction with ApplicationID field. All queries must have
  ApplicationID in the WHERE clause.
A love story

 .NET 3.0 was released, woohoo!
WCF!
Lambda Expressions!!
Linq to SQL!!!
 Upgraded to .NET 3.0, top to bottom.
 Major deployment over the weekend.
 Monday 9 AM, peak traffic.
 No response from site.
~100% on all web servers
Linq to SQL is not suitable for high
volume web applications
 Linq to SQL is not optimized for web application.
 No disconnected entity support.
 Entities are not serializable.
 Linq to sql expressions consume high CPU when
  compiled to SQL.

var query = from widget in dc.Widgets
where widget.ID == id && widget.PageID == pageId
select widget;

var widget = query.SingleOrDefault();
How bad Linq to SQL is?




Source: JD Conley‟s blog
Fixing Linq to SQL
 Convert all queries to Compiled Queries.
 tinyurl.com/omarLINQ
Linq to SQL transaction deadlocks
       Large table, high read and medium write, causes query
        timeouts, high locks, transaction deadlock because of
        SERIALIZATION isolation level.

        Transaction (Process ID ##) was deadlocked on lock resources
        with another process and has been chosen as the deadlock
        victim. Rerun the transaction.
        Timeout Expired. The Timeout Period Elapsed Prior To
        Completion Of The Operation Or The Server Is Not Responding.
using (var db = new YourDataContext2())
{
  db.Connection.Open();
  db.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ
UNCOMMITTED;");

    var user = db.aspnet_Users.First();
    var pages = user.Pages.ToList();
}
Linq to SQL transaction deadlocks
  Connection does not close!
  Bug in Data Context code.
  Solution is to override the Dispose.

class YourDataContext2 : YourDataContext, IDisposable {
 public new void Dispose() {
   if (base.Connection != null)
    if (base.Connection.State != System.Data.ConnectionState.Closed) {
       base.Connection.Close();
       base.Connection.Dispose();
    }
   base.Dispose();
 }
}
Content Delivery Network (CDN)
 CDN cache and deliver content from their servers that
  are closest to users‟ computers. The closer servers are
  to end user, the shorter roundtrip time for every request.
  For ex, Akamai has servers almost every city in the
  world.
 Content from CDN nodes get served faster with lower
  latency than coming from your servers.
 CDN Nodes have better caching and compression
  algorithms.
 CDN nodes can offload your server and network from
  delivering static files. Thus better throughput for
  dynamic content.
Content Delivery Network


              0.7
              sec


 Washington, DC

                       2.2
                       sec

                           Australia
Without CDN




              www.yoursite.com
How CDN works

 static.yoursite.com




                       www.yoursite.com
Two types of CDN

 Static – you upload the files to CDN and
  they give you an URL. E.g.
  yoursite.cachefly.net/logo.gif
 Dynamic – Host your dynamic application
  behind the CDN. For ex, Edgecast and
  Panther Express.
   Very cheap - $0.2/GB
How Dynamic CDN works




                        www.yoursite.com
How Dynamic CDN works




                          www.yoursite.com




                        Static content
                        cached,
                        Compressed
                        automatically
How Dynamic CDN works




                          www.yoursite.com




                        Static content
                        cached,
                        Compressed
                        automatically
13 disasters for production websites
1.    Faulty hard drive supplied by supplier, data corruption within weeks.
2.    Controller malfunctions and corrupts all disks in the same controller.
3.    RAID malfunction.
4.    CPU overheated and burned out.
5.    Firewall went down.
6.    Remote Desktop stopped working after a patch installation.
7.    Remote Desktop max connection exceeded. Cannot login anymore to
      servers.
8.    Database got corrupted while we were moving the production
      database from one server to another over the network.
9.    One developer deleted the production database accidentally while
      doing routine work.
10.   Support crew at hosting service formatted our running production
      server instead of a corrupted server that we asked to format.
11.   Windows got corrupted and was not working until we reinstalled.
12.   DNS goes down. Don’t get domain from GoDaddy.
13.   Internet backbone goes down in different part of the world.

                                 http://tinyurl.com/omar13
Conclusion

 ASP.NET out of the box, does not scale for millions of
  hits.
 Must make the hacks at code, database and
  configuration level to get it to scale.
 That‟s reality for any technology, not ASP.NET specific.

More Related Content

What's hot

File organization and indexing
File organization and indexingFile organization and indexing
File organization and indexingraveena sharma
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorialGiuseppe Maxia
 
PostgreSQL: Joining 1 million tables
PostgreSQL: Joining 1 million tablesPostgreSQL: Joining 1 million tables
PostgreSQL: Joining 1 million tablesHans-Jürgen Schönig
 
Node.js Меньше сложности, больше надежности Holy.js 2021
Node.js Меньше сложности, больше надежности Holy.js 2021Node.js Меньше сложности, больше надежности Holy.js 2021
Node.js Меньше сложности, больше надежности Holy.js 2021Timur Shemsedinov
 
Databases: Concurrency Control
Databases: Concurrency ControlDatabases: Concurrency Control
Databases: Concurrency ControlDamian T. Gordon
 
Experiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan Zhang
Experiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan ZhangExperiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan Zhang
Experiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan ZhangDatabricks
 
ETL Patterns with Postgres
ETL Patterns with PostgresETL Patterns with Postgres
ETL Patterns with PostgresMartin Loetzsch
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQLNicole Ryan
 
Gluster for Geeks: Performance Tuning Tips & Tricks
Gluster for Geeks: Performance Tuning Tips & TricksGluster for Geeks: Performance Tuning Tips & Tricks
Gluster for Geeks: Performance Tuning Tips & TricksGlusterFS
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022Alexander Ioffe
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answersMichael Belete
 
EAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV ModelEAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV ModelKhoa Truong Dinh
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and OperatorsMohan Kumar.R
 
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...Insight Technology, Inc.
 

What's hot (20)

hacking with node.JS
hacking with node.JShacking with node.JS
hacking with node.JS
 
File organization and indexing
File organization and indexingFile organization and indexing
File organization and indexing
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
 
PostgreSQL: Joining 1 million tables
PostgreSQL: Joining 1 million tablesPostgreSQL: Joining 1 million tables
PostgreSQL: Joining 1 million tables
 
Node.js Меньше сложности, больше надежности Holy.js 2021
Node.js Меньше сложности, больше надежности Holy.js 2021Node.js Меньше сложности, больше надежности Holy.js 2021
Node.js Меньше сложности, больше надежности Holy.js 2021
 
Spring Batch 2.0
Spring Batch 2.0Spring Batch 2.0
Spring Batch 2.0
 
Same origin policy
Same origin policySame origin policy
Same origin policy
 
Databases: Concurrency Control
Databases: Concurrency ControlDatabases: Concurrency Control
Databases: Concurrency Control
 
Experiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan Zhang
Experiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan ZhangExperiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan Zhang
Experiences Migrating Hive Workload to SparkSQL with Jie Xiong and Zhan Zhang
 
ETL Patterns with Postgres
ETL Patterns with PostgresETL Patterns with Postgres
ETL Patterns with Postgres
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQL
 
Isam
IsamIsam
Isam
 
My Sql Work Bench
My Sql Work BenchMy Sql Work Bench
My Sql Work Bench
 
Gluster for Geeks: Performance Tuning Tips & Tricks
Gluster for Geeks: Performance Tuning Tips & TricksGluster for Geeks: Performance Tuning Tips & Tricks
Gluster for Geeks: Performance Tuning Tips & Tricks
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
 
EAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV ModelEAV Sytem- Magento EAV Model
EAV Sytem- Magento EAV Model
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...
 

Similar to Scaling asp.net websites to millions of users

10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websites10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websitesoazabir
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsSarvesh Kushwaha
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaYevgeniy Brikman
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performancerudib
 
How to build a Citrix infrastructure on AWS
How to build a Citrix infrastructure on AWSHow to build a Citrix infrastructure on AWS
How to build a Citrix infrastructure on AWSDenis Gundarev
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisationgrooverdan
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comVan-Duyet Le
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextPrateek Maheshwari
 
Sherlock Homepage (Maarten Balliauw)
Sherlock Homepage (Maarten Balliauw)Sherlock Homepage (Maarten Balliauw)
Sherlock Homepage (Maarten Balliauw)Visug
 
Sherlock Homepage - A detective story about running large web services (VISUG...
Sherlock Homepage - A detective story about running large web services (VISUG...Sherlock Homepage - A detective story about running large web services (VISUG...
Sherlock Homepage - A detective story about running large web services (VISUG...Maarten Balliauw
 
Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...Maarten Balliauw
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing Techglyphs
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performanceEngine Yard
 
Sherlock Homepage - A detective story about running large web services - NDC ...
Sherlock Homepage - A detective story about running large web services - NDC ...Sherlock Homepage - A detective story about running large web services - NDC ...
Sherlock Homepage - A detective story about running large web services - NDC ...Maarten Balliauw
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questionsAkhil Mittal
 

Similar to Scaling asp.net websites to millions of users (20)

10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websites10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websites
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC Applications
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
PPT
PPTPPT
PPT
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performance
 
SQL Injection
SQL InjectionSQL Injection
SQL Injection
 
How to build a Citrix infrastructure on AWS
How to build a Citrix infrastructure on AWSHow to build a Citrix infrastructure on AWS
How to build a Citrix infrastructure on AWS
 
Practical OData
Practical ODataPractical OData
Practical OData
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisation
 
Web Security
Web SecurityWeb Security
Web Security
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.com
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's Next
 
Sherlock Homepage (Maarten Balliauw)
Sherlock Homepage (Maarten Balliauw)Sherlock Homepage (Maarten Balliauw)
Sherlock Homepage (Maarten Balliauw)
 
Sherlock Homepage - A detective story about running large web services (VISUG...
Sherlock Homepage - A detective story about running large web services (VISUG...Sherlock Homepage - A detective story about running large web services (VISUG...
Sherlock Homepage - A detective story about running large web services (VISUG...
 
AD102 - Break out of the Box
AD102 - Break out of the BoxAD102 - Break out of the Box
AD102 - Break out of the Box
 
Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
Sherlock Homepage - A detective story about running large web services - NDC ...
Sherlock Homepage - A detective story about running large web services - NDC ...Sherlock Homepage - A detective story about running large web services - NDC ...
Sherlock Homepage - A detective story about running large web services - NDC ...
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questions
 

Recently uploaded

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Scaling asp.net websites to millions of users

  • 1. Scaling ASP.NET websites from thousands to millions of users Lessons learnt from scaling Web 2.0 apps to millions of users Omar AL Zabir Chief Architect, SaaS Platform, BT omaralzabir.com omaralzabir@gmail.com Twitter.com/omaralzabir Book “Building Web 2.0 portal using ASP.NET 3.5” from O‟Reilly
  • 2. How to blow up a website  Get some super fast fiber broadband connection.  Run this on couple of quad core desktops: for (int j = 0; j < 100; j ++) { for (int i = 0; i < 100; i ++) { var client = new WebClient(); client.DownloadStringAsync( new Uri("http://www.microsoft.com/MISReport.aspx"); } Thread.Sleep(500); }
  • 3. Prevent App Level DOS attack  Application Level DOS attacks are attempts to hit your expensive pages too frequently so that you have 100% CPU and your site goes down. For ex, hitting some expensive Report page continuously.  You can run out of ASP.NET Threads and stop responding completely to any request.  I‟m not talking about Network level DOS attacks like TCP SYN flood or DDOS attacks that hardware firewalls can prevent.  Firewalls don‟t block legitimate requests.
  • 4. Prevent App level DOS attacks  Protect only expensive pages. Pages that are unlikely to get hit too many times within a short duration.  Build a HttpModule and hook on OnInit event.  Store which IP is making how many number of hits in last 10 minutes. Store the table in some in-memory cache.  If the threshold has exceeded, stop responding to that IP. Call Response.End()  Solution is here: http://tinyurl.com/omarDOS IP Hits in last 10 mins 128.0.0.1 500 128.0.0.2 345
  • 5. ASP.NET ProcessModel Optimization  ASP.NET ProcessModel Defaults  maxWorkerThreads = 20  maxIOThreads = 20  memoryLimit = 60  Nowadays, servers are way too powerful. You don‟t need to be conservative.  Change default process model setting in machine.config to make best use of CPU power.
  • 6. ASP.NET Pipeline Optimization  Default ASP.NET Pipeline has several components that intercept each and every request. Thus they add extra processing overhead on every request.  Multiply the overhead by hundreds of requests per second – you get a significant overhead.
  • 7. ASP.NET Pipeline Optimization  Remove what you don‟t need.  If you are using Forms Authentication, SQL Server storage, no web.config based role based permission then you can remove most of them:
  • 8. Prevent large ASP.NET cookies on static content  Each and every request, even static files, get the ASP.NET cookies sent.  517 bytes of worthless data per request.  Avg pages have 40 resources. 40 x 517 = 20 KB.  1M page view = 20 GB of data upload to server. Cookie: .DBANON=w3kYczsH8Wvzs6MgryS4JYEF0N-8ZR6aLRSTU9KwVaGa ydD6WwUHD7X9tN8vBgjgzKf3r3SJHusTYFjU85yYfnunyCeuExcZs8 95JK9Fk1HS68ksGwm3QpxnRZvpDBAfJKEUKee2OTlND0gi43qwwtI PLeY1; ASP.NET_SessionId=bmnbp155wilotk45gjhitoqg; .DBAUTH 12=2A848A8C200CB0E8E05C6EBA8059A0DBA228FC5F6EDD2940 1C249D237812344C15B3C5C57D6B776037FAA8F14017880E57B DC14A7963C58B0A0B30229AF0123A6DF56601D814E75525E7DC A9AD4A0EF200832B39A1F35A5111092F0805B0A8CD3D2FD5E3A B6176893D86AFBEB68F7EA42BE61E89537DEAA3279F3B576D0C 44BA00B9FA1D9DD3EE985F37B0A5A134ADC0EA9C548D
  • 9. Prevent ASP.NET cookies on static content  Setup a new website in IIS, map to the same code folder.  Map static.yoursite.com host header to that website.  Prefix all css, js, image links with http://static.yoursite.com/  Add a Global.asax‟s EndResponse event on the new website. HttpContext context = HttpContext.Current; if (context.Request.Url.Host == “static.yoursite.com”) { List<string> cookiesToClear = new List<string>(); foreach (string cookieName in context.Request.Cookies) { HttpCookie cookie = context.Request.Cookies[cookieName]; cookiesToClear.Add(cookie.Name); } foreach (string name in cookiesToClear) { HttpCookie cookie = new HttpCookie(name, string.Empty); cookie.Expires = DateTime.Today.AddYears(-1); context.Response.Cookies.Set(cookie);
  • 10. System.net optimization  If you are using HttpWebRequest or WebClient or any other TCP/IP operation, increase the max connection limit.  Default is 2 per IP.  WCF service calls are limited by this setting.  Unless you suspect rogue clients, set some reasonably high number on webservers, but moderate number on desktop clients.
  • 11. System.net default setting is suboptimal Bottleneck Max 2 concurrent calls
  • 12. ASP.NET Profile Provider  Anonymous Provider creates one anonymous user in database on every first hit to Profile object‟s Custom properties:  The SP aspnet_Profile_GetProperties gets called when you access Profile object for first time in a request. And this SP is slooooooooow!
  • 13. ASP.NET Profile Provider  The slow SP that gets fired when you access custom Profile properties: CREATE PROCEDURE [dbo].[ aspnet_Profile_GetProperties] @ApplicationName nvarchar(256), @UserId … AS BEGIN DECLARE @ApplicationId uniqueidentifier SELECT @ApplicationId = NULL SELECT @ApplicationId = ApplicationId FROM dbo.aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName … … … IF (@@ROWCOUNT > 0) BEGIN UPDATE dbo.aspnet_Users SET LastActivityDate=@CurrentTimeUtc WHERE UserId = @UserId END END
  • 14. ASP.NET Profile Provider  The slow SP‟s execution plan
  • 15. ASP.NET Profile Provider  Don‟t update LastActivityDate when Profile object is loaded. Do it only when Profile object is updated.  Update once every hour or so. If LastActivityDate < DateTime.Now.AddHours(-1) then update. No need to do per request or too frequently.  Hard code the Application ID to avoid one lookup inside the SP.  tinyurl.com/omarGetProp
  • 16. ASP.NET Membership  ASP.NET Membership Provider Stored Procs use default transaction isolation level, which is Serializable. Transaction (Process ID ##) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Timeout Expired. The Timeout Period Elapsed Prior To Completion Of The Operation Or The Server Is Not Responding. ALTER PROCEDURE [dbo].[aspnet_Profile_GetProperties] @ApplicationName nvarchar(256), @UserName nvarchar(256), @CurrentTimeUtc datetime AS BEGIN SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  • 17. ASP.NET Membership Query  Common queries that brings down hell:  Select * from aspnet_users where UserName = „blabla‟  Select * from aspnet_membership where Email = “someone@somewhere.com”  What‟s wrong with these queries?
  • 18. ASP.NET Membership Queries  Let's look at the indexes:  Table: aspnet_users  Clustered Index = ApplicationID, LoweredUserName  NonClustered Index = ApplicationID, LastActivityDate  Primary Key = UserID  Table: aspnet_membership  Clustered Index = ApplicationID, LoweredEmail  NonClustered = UserID  Table: aspnet_Profile  Clustered Index = UserID  DO NOT use Email or UserName fields in WHERE clause. They are not part of the index instead LoweredUserName and LoweredEmail fields are in conjunction with ApplicationID field. All queries must have ApplicationID in the WHERE clause.
  • 19. A love story  .NET 3.0 was released, woohoo! WCF! Lambda Expressions!! Linq to SQL!!!  Upgraded to .NET 3.0, top to bottom.  Major deployment over the weekend.  Monday 9 AM, peak traffic.  No response from site.
  • 20. ~100% on all web servers
  • 21. Linq to SQL is not suitable for high volume web applications  Linq to SQL is not optimized for web application.  No disconnected entity support.  Entities are not serializable.  Linq to sql expressions consume high CPU when compiled to SQL. var query = from widget in dc.Widgets where widget.ID == id && widget.PageID == pageId select widget; var widget = query.SingleOrDefault();
  • 22. How bad Linq to SQL is? Source: JD Conley‟s blog
  • 23. Fixing Linq to SQL  Convert all queries to Compiled Queries.  tinyurl.com/omarLINQ
  • 24. Linq to SQL transaction deadlocks  Large table, high read and medium write, causes query timeouts, high locks, transaction deadlock because of SERIALIZATION isolation level. Transaction (Process ID ##) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Timeout Expired. The Timeout Period Elapsed Prior To Completion Of The Operation Or The Server Is Not Responding. using (var db = new YourDataContext2()) { db.Connection.Open(); db.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;"); var user = db.aspnet_Users.First(); var pages = user.Pages.ToList(); }
  • 25. Linq to SQL transaction deadlocks  Connection does not close!  Bug in Data Context code.  Solution is to override the Dispose. class YourDataContext2 : YourDataContext, IDisposable { public new void Dispose() { if (base.Connection != null) if (base.Connection.State != System.Data.ConnectionState.Closed) { base.Connection.Close(); base.Connection.Dispose(); } base.Dispose(); } }
  • 26. Content Delivery Network (CDN)  CDN cache and deliver content from their servers that are closest to users‟ computers. The closer servers are to end user, the shorter roundtrip time for every request. For ex, Akamai has servers almost every city in the world.  Content from CDN nodes get served faster with lower latency than coming from your servers.  CDN Nodes have better caching and compression algorithms.  CDN nodes can offload your server and network from delivering static files. Thus better throughput for dynamic content.
  • 27. Content Delivery Network 0.7 sec Washington, DC 2.2 sec Australia
  • 28. Without CDN www.yoursite.com
  • 29. How CDN works static.yoursite.com www.yoursite.com
  • 30. Two types of CDN  Static – you upload the files to CDN and they give you an URL. E.g. yoursite.cachefly.net/logo.gif  Dynamic – Host your dynamic application behind the CDN. For ex, Edgecast and Panther Express.  Very cheap - $0.2/GB
  • 31. How Dynamic CDN works www.yoursite.com
  • 32. How Dynamic CDN works www.yoursite.com Static content cached, Compressed automatically
  • 33. How Dynamic CDN works www.yoursite.com Static content cached, Compressed automatically
  • 34. 13 disasters for production websites 1. Faulty hard drive supplied by supplier, data corruption within weeks. 2. Controller malfunctions and corrupts all disks in the same controller. 3. RAID malfunction. 4. CPU overheated and burned out. 5. Firewall went down. 6. Remote Desktop stopped working after a patch installation. 7. Remote Desktop max connection exceeded. Cannot login anymore to servers. 8. Database got corrupted while we were moving the production database from one server to another over the network. 9. One developer deleted the production database accidentally while doing routine work. 10. Support crew at hosting service formatted our running production server instead of a corrupted server that we asked to format. 11. Windows got corrupted and was not working until we reinstalled. 12. DNS goes down. Don’t get domain from GoDaddy. 13. Internet backbone goes down in different part of the world. http://tinyurl.com/omar13
  • 35. Conclusion  ASP.NET out of the box, does not scale for millions of hits.  Must make the hacks at code, database and configuration level to get it to scale.  That‟s reality for any technology, not ASP.NET specific.