ebook img

Ultra-Fast ASP.NET: Building Ultra-fast and Ultra-scalable Web Sites Using ASP.NET and SQL Server PDF

477 Pages·2010·7.497 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Ultra-Fast ASP.NET: Building Ultra-fast and Ultra-scalable Web Sites Using ASP.NET and SQL Server

Ultra-fast ASP.NET Building Ultra-fast and Ultra-scalable Web Sites Using ASP.NET and SQL Server ■ ■ ■ Richard Kiessig i Ultra-fast ASP.NET Copyright © 2009 by Richard Kiessig All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2383-2 ISBN-13 (electronic): 978-1-4302-2384-9 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Matthew Moodie Technical Reviewer: Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Copy Editors: Kim Wimpsett and Tiffany Taylor Production Assistance: Patrick Cunningham Indexer: Becky Hornyak Artist: April Milne Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- [email protected], or visit http://www.springeronline.com. For information on translations, please e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. Contents at a Glance ■About the Author .............................................................................................. xviii ■Introduction ......................................................................................................... xx ■Chapter 1: Principles and Method .........................................................................1 ■Chapter 2: Client Performance ............................................................................13 ■Chapter 3: Caching ..............................................................................................71 ■Chapter 4: IIS 7 ..................................................................................................127 ■Chapter 5: ASP.NET Threads and Sessions ........................................................161 ■Chapter 6: Using ASP.NET to Implement and Manage Optimization Techniques ............................................201 ■Chapter 7: Managing ASP.NET Application Policies ..........................................229 ■Chapter 8: SQL Server Relational Database .......................................................263 ■Chapter 9: SQL Server Analysis Services ..........................................................349 ■Chapter 10: Infrastructure and Operations ........................................................385 ■Chapter 11: Putting It All Together ....................................................................411 ■Glossary .............................................................................................................435 ■Index ..................................................................................................................439 iii ■CONTENTS Contents ■About the Author .............................................................................................. xviii ■About the Technical Reviewer ............................................................................ xix ■Introduction ......................................................................................................... xx ■Chapter 1: Principles and Method .........................................................................1 The Difference Between Performance and Scalability ................................................... 2 Why Ultra-fast and Ultra-scalable? ................................................................................. 2 Optimization .......................................................................................................................................... 3 Process .................................................................................................................................................. 4 The Full Experience ............................................................................................................................... 5 End-to-End Web Page Processing .................................................................................. 5 Overview of Principles .................................................................................................... 8 Performance Principles ......................................................................................................................... 8 Secondary Techniques .......................................................................................................................... 9 Environment and Tools Used in This Book .................................................................... 10 Software Tools and Versions ............................................................................................................... 10 Terminology ......................................................................................................................................... 11 Typographic Conventions .................................................................................................................... 11 Author’s Web Site ................................................................................................................................ 12 Summary ...................................................................................................................... 12 ■Chapter 2: Client Performance ............................................................................13 Browser Page Processing ............................................................................................. 14 Network Connections and the Initial HTTP Request ............................................................................ 14 iv ■ CONTENTS Page Parsing and New Resource Requests ......................................................................................... 16 Page Resource Order and Reordering ................................................................................................. 17 Browser Caching .......................................................................................................... 18 Network Optimizations ................................................................................................. 19 Script Include File Handling .......................................................................................... 21 Increase Parallelism by Queuing Resources Before Scripts ................................................................ 22 Minimize the Number of Script Files ................................................................................................... 23 Requesting Objects After the Rest of the Page .................................................................................... 26 Script Defer ......................................................................................................................................... 26 Server-Side Alternatives to Script ....................................................................................................... 26 Download Less ............................................................................................................. 27 Reduce the Number of Resources per Page ........................................................................................ 27 Minify Your HTML, CSS, and JavaScript .............................................................................................. 28 Maximize Compressibility .................................................................................................................... 30 Image Optimization .............................................................................................................................. 30 Web Site Icon File ................................................................................................................................ 34 General HTML, CSS, and JavaScript Optimization ............................................................................... 35 Using JavaScript to Gate Page Requests ...................................................................... 39 Submit Buttons .................................................................................................................................... 39 Links .................................................................................................................................................... 40 Using JavaScript to Reduce HTML Size ........................................................................ 41 Generate Repetitive HTML ................................................................................................................... 41 Add Repetitive Text to Your Tags ......................................................................................................... 43 Upload Less .................................................................................................................. 43 v ■CONTENTS CSS Optimizations ........................................................................................................ 45 Image Sprites and Clustering ....................................................................................... 49 Leveraging DHTML ........................................................................................................ 52 Using Ajax ..................................................................................................................... 52 Using Silverlight ............................................................................................................ 55 Building HTML Controls ....................................................................................................................... 55 Calling into Silverlight from JavaScript ............................................................................................... 58 Other Ways to Use Silverlight to Improve Performance ....................................................................... 59 Improving Rendering Speed ......................................................................................... 60 Precaching .................................................................................................................... 61 Precaching Images .............................................................................................................................. 62 Precaching CSS and JavaScript .......................................................................................................... 62 Tableless Layout Using CSS ......................................................................................... 64 Optimizing JavaScript Performance ............................................................................. 66 Summary ...................................................................................................................... 67 ■Chapter 3: Caching ..............................................................................................71 Caching at All Tiers ....................................................................................................... 72 Browser Cache ............................................................................................................. 73 Caching Static Content ........................................................................................................................ 73 Caching Dynamic Content ................................................................................................................... 76 ViewState ...................................................................................................................... 78 Protecting ViewState Data Integrity ..................................................................................................... 79 Cookies ......................................................................................................................... 85 Setting Session Cookies ...................................................................................................................... 86 Multiple Name/Value Pairs in a Single Cookie ..................................................................................... 86 Cookie Properties ................................................................................................................................ 86 vi ■ CONTENTS Silverlight Isolated Storage ........................................................................................... 96 Sample Application: “Welcome Back” ................................................................................................ 96 Deploying and Updating Silverlight Applications ............................................................................... 101 Proxy Cache ................................................................................................................ 101 Using the Cache-Control HTTP Header .............................................................................................. 102 Managing Different Versions of the Same Content............................................................................ 103 Web Server Cache ...................................................................................................... 103 Windows Kernel Cache ...................................................................................................................... 104 IIS 7 Output Caching .......................................................................................................................... 109 ASP.NET Output Caching ................................................................................................................... 110 ASP.NET Object Caching .................................................................................................................... 117 SQL Server Caching .................................................................................................... 122 Distributed Caching .................................................................................................... 123 Cache Expiration Times .............................................................................................. 124 Dynamic Content ............................................................................................................................... 124 Static Content .................................................................................................................................... 124 Summary .................................................................................................................... 125 ■Chapter 4: IIS 7 ..................................................................................................127 Application Pools and Web Gardens ........................................................................... 127 Request-Processing Pipeline ...................................................................................... 130 Windows System Resource Manager ......................................................................... 131 Common HTTP Issues ................................................................................................. 134 HTTP Redirects .................................................................................................................................. 135 HTTP Headers .................................................................................................................................... 136 Compression ............................................................................................................... 139 Enabling Compression ....................................................................................................................... 140 Setting Compression Options ............................................................................................................ 141 vii ■CONTENTS Using web.config to Configure Compression ..................................................................................... 142 Caching Compressed Content ........................................................................................................... 143 Programmatically Enabling Compression .......................................................................................... 144 HTTP Keep-Alives ....................................................................................................... 144 Optimizing Your URLs ................................................................................................. 144 Virtual Directories .............................................................................................................................. 145 URL Rewriting .................................................................................................................................... 146 Managing Traffic ......................................................................................................... 150 Using robots.txt ................................................................................................................................. 150 Site Maps ........................................................................................................................................... 151 Bandwidth Throttling ......................................................................................................................... 152 Failed Request Tracing ............................................................................................... 154 Miscellaneous IIS Performance Tuning ...................................................................... 159 Summary .................................................................................................................... 159 ■Chapter 5: ASP.NET Threads and Sessions ........................................................161 Threads Affect Scalability ........................................................................................... 161 ASP.NET Page Life Cycle ............................................................................................ 163 Application Thread Pool .............................................................................................. 165 Synchronous Page ............................................................................................................................. 165 Asynchronous Page ........................................................................................................................... 166 Load Test ........................................................................................................................................... 167 Improving the Scalability of Existing Synchronous Pages ................................................................. 170 Executing Multiple Async Tasks from a Single Page ......................................................................... 170 Handling Timeouts ............................................................................................................................. 172 Asynchronous Web Services ............................................................................................................. 173 Asynchronous File I/O ........................................................................................................................ 176 Asynchronous Web Requests ............................................................................................................ 178 viii ■ CONTENTS Background Worker Threads ...................................................................................... 179 Background Thread for Logging ........................................................................................................ 181 Task Serialization .............................................................................................................................. 186 Locking Guidelines and Using ReaderWriterLock ....................................................... 186 Session State .............................................................................................................. 187 Session IDs ........................................................................................................................................ 188 InProc Mode ....................................................................................................................................... 189 Using StateServer .............................................................................................................................. 189 Using SQL Server ............................................................................................................................... 189 Selectively Enabling Session State and Using ReadOnly Mode ......................................................... 190 Scaling Session State Support .......................................................................................................... 191 Fine-Tuning ....................................................................................................................................... 197 Full-Custom Session State ................................................................................................................ 197 Session Serialization ......................................................................................................................... 198 Alternatives to Session State ...................................................................................... 199 Summary .................................................................................................................... 199 ■Chapter 6: Using ASP.NET to Implement and Manage Optimization Techniques .............................................201 Master Pages .............................................................................................................. 201 User Controls .............................................................................................................. 204 Example ............................................................................................................................................. 205 Registering and Using the Control ..................................................................................................... 207 Placing Controls in a DLL ................................................................................................................... 207 Themes ....................................................................................................................... 208 Static Files ......................................................................................................................................... 208 Skins .................................................................................................................................................. 208 ix ■CONTENTS Setting Themes Dynamically ............................................................................................................. 208 Themable Properties ......................................................................................................................... 210 Example ............................................................................................................................................. 210 Precaching Themed Images .............................................................................................................. 211 Browser-Specific Code ............................................................................................... 212 Using Request.Browser ..................................................................................................................... 213 Browser-Specific Property Prefixes................................................................................................... 214 Caching Browser-Specific Pages ...................................................................................................... 215 Control Adapters ................................................................................................................................ 216 Browser Providers ............................................................................................................................. 218 Cloaking ............................................................................................................................................. 220 Dynamically Generating JavaScript and CSS ............................................................. 220 Example ............................................................................................................................................. 221 Accessing ASP.NET Controls from JavaScript ................................................................................... 222 Multiple Domains for Static Files ................................................................................ 223 Image Resizing ........................................................................................................... 224 Summary .................................................................................................................... 227 ■Chapter 7: Managing ASP.NET Application Policies ..........................................229 Custom HttpModules .................................................................................................. 230 Requirements for the Example HttpModule ....................................................................................... 230 Init() Method ...................................................................................................................................... 231 PreRequestHandlerExecute Event Handler ........................................................................................ 232 BeginAuthenticateRequest Event Handler ......................................................................................... 233 EndAuthenticateRequest Event Handler ............................................................................................ 235 EndRequest Event Handler ................................................................................................................ 236 Database Table and Stored Procedure .............................................................................................. 237 Registering the HttpModule in web.config ........................................................................................ 238 x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.