ITCamp 2011 - Alessandro Pilotti - Optimizing ASPNet and PHP apps on IIS 7.5
1. Optimizing ASP.Net and PHP
Web Applications on IIS 7.5
Alessandro Pilotti
info@cloudbasesolutions.com
MVP / IIS
MCSD (VS 6.0 & .Net), MCAD, MCSE, MCDBA,
MCT, RHCE, SCJP2
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
2. IT Camp 2011
⢠Thanks for coming!
⢠ITCamp is made possible by our sponsors:
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
3. Session agenda
⢠IIS 7.5 Architecture
⢠WebPI
⢠FastCGI
⢠Wincache Extensions for PHP
⢠Output Caching
⢠Application Request Routing
⢠AppFabric Caching
⢠Session management in PHP & SQL Server
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
4. Architecture - IIS 6
Web Request ⢠Monolithic
Authentication ⢠Extension via
NTLM Basic Anon ISAPI ony
âŚ
CGI
Determine Static
Handler File
ASP.NET
ISAPI
âŚ
PHP
Send Response
Log Compress
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
5. Architecture IIS 7.5
Web Request ⢠Features separated in
NTLM
Basic
modules
Authentication
Anon â More than 40
Authorization
⌠⢠Modules fit in a
ResolveCache CGI
âŚ
common pipeline
Static
File ⢠Modules employ
Determine Handler
ISAPI common API
âŚ
UpdateCache ⢠Managed or
Compress
unmanaged
Send Response Log
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
6. Anatomy of a Request in IIS 7.x
⢠HTTP .sys
receives the
request
⢠WAS
⢠applicationHost
.config
⢠WWW service
⢠HTTP .sys config
⢠W3WP
⢠WP execution
⢠Response
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
7. Request â W3WP details
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
8. WAS and W3SVC
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
9. versioning
⢠IIS 5.0
â Windows 2000
⢠IIS 5.1
â Windows XP
⢠IIS 6.0
â Windows Server 2003
⢠IIS 7.0
â Windows Vista / Server 2008
⢠IIS 7.5
â Window 7 / Server 2008 R2
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
10. Web Platform Installer 3
⢠Single tool to easily deploy:
â Frameworks
â Extensions
â Tools
â Applications
⢠E.g.: DNN, Joomla, etc
⢠Always updated
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
11. WEB PI 3
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
12. Web PI 3
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
13. Web PI 3
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
14. Web Platform Installer
DEMO
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
15. FastCGI 1.5
⢠Very popular in the Apache environment, for PHP Perl,
,
Ruby, etc
⢠CGI is slow: every request generates a process instance!
⢠FastCGI employs the same process for many requests
⢠Supported on IIS 7.x and IIS 6.0 (the latter via fcgiext.dll
ISAPI)
⢠Written in collaboration with Zend (PHP) but is generic
⢠In IIS 7.x: Enable CGI among the IIS role services
⢠Note: Install Hotfix KB980363 on IIS 7.0 to get version
1.5!
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
17. FastCGI â version differences
Feature 5.1/6.0 7.0 7.5
Monitoring file changes Yes Yes Yes
Real-Time max Inst. tuning Yes Yes Yes
STDERR support Yes Yes Yes
SIGTERM support Yes Yes Yes
Env. variable _FCGI_X_PIPE_ Yes Yes Yes
Proc. Pool per application Yes Yes Yes
UTF-8 variable encoding Yes Yes No
IIS CPU Limit support No Yes Yes
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
18. FastCGI - PHP
⢠Install PHP 5.2 or 5.3
â During setup choose FastCGI
⢠As an alternative, deploy via WebPI
⢠WP3P.exe
â Loads iisfcgi.dll
â Executes php-cgi.exe
⢠Config: PHP.INI
â E.g.: fastcgi.impersonate = 1
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
19. PHP on IIS â other options
⢠ISAPI
â Previous choice
â Low reliability due to reentrance problems
â DLL loaded at W3SVC instance level
⢠Security issues!!
⢠CGI (not FastCGI!)
â Awful performance
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
20. PHP on IIS - Performance
⢠PHP gets compiled at runtime
â Reads source file and generates
bytecode
â Process repeated at each request
⢠Not a problem for small loads
⢠Huge bottleneck otherwise
â Various solutions available
â Now (PHP 5.x) no one available natively
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
21. PHP Accelerators
⢠Tools whose purpose is to improve
the PHP execution time
⢠Most popular available:
â APC (default on PHP 6.0)
â eAccelerator
â Zend Server
â Windows Cache Extension for PHP
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
22. WinCache for PHP
⢠Open source project developed by
Microsoft
⢠IIS Specific
⢠Requires PHP 5.2 or 5.3 x86
â Non thread safe version
⢠Provides features not available in
APC and other accelerators
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
23. WinCache for PHP
⢠Setup
â Web Platform Installer
â Direct download
â Source code
⢠PHP configuration
.ini
â extension = php_wincache.dll
â Verify setup with phpinfo()
â maxInstances = 0 for best performance!
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
24. WinCache for PHP
⢠PHP Bytecode Cache
â Subsequest calls to a script employ precompiled and
cached bytecode
⢠PHP scripts loaded in memory
â Reduced I/O, mainly for UNC Paths!
⢠Cache handled via Shared Memory
⢠Absolute path caching
â Optimizes the time required to resolve relative paths
⢠Cache based session handler
â By default PHP uses a file system based solution
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
25. WinCache for PHP
⢠User cache
â wincache_ucache_set(key, value)
â wincache_ucache_get(key)
â Etc
⢠User Cache session handler
â session.save_handler = wincache
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
26. FastCGI / PHP / WinCache
DEMO
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
27. IIS 7.x Output Caching
⢠Very efficient way to optimize an
application
⢠There 2 type of contents
â Static
â Dynamic
⢠Based on a degree of dynamism and determinism
â
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
28. IIS 7.x Output Caching
⢠IIS provides two caching policies
â User mode (IIS 7.x worker process)
â Kernel mode
⢠Cache in HTTP.sys memory
⢠Faster!
⢠Parameter based (header, QueryString)
⢠Not to be confused with ASP.Net output
caching!
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
29. Kernel Caching - limits
⢠No user mode modules or features
â E.g. Authentication
⢠varyByQueryString parameter not
supported
⢠Management:
â netsh http show cache
â Netsh http delete cache âŚ
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
30. IIS 7.x Output Caching
⢠Caching can be enabled for static and
âsemi-dynamicâ content
⢠Caching enabled for each site
â Via UI
â Via Command Line (appcmd)
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
31. Caching Rules
⢠IIS decides if âit is worthâ to cache some
content
⢠Parameters
â frequentHitTimePeriod
⢠Default 10s
â frequentHitThreshold
⢠Default 2
⢠ServerRuntime in applicationHost.config
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
32. ServiceRuntime sample
<system.webServer>
<serverRuntime enabled="true"
frequentHitThreshold="5"
frequentHitTimePeriod="00:00:20" />
</system.webServer>
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
33. Output Caching
DEMO
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
34. Application Request Routing (ARR)
2.5
⢠Based on the URL Rewrite module
⢠Enhances Availability and scalability
⢠Load balancing
⢠Health monitoring
⢠Client Affinity
⢠Works at level 5 ISO/OSI
â Can work with a NLB (level 3) solution
⢠ARR 2 introduces a caching option
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
39. AppFabric
⢠Extension with two separated main
areas
â WCF and WF service housing
MSDN video (in Italian ď )
http://www.microsoft.com/italy/beit/Generic.aspx?
video=1a5e02e4-c881-4ef9-ac86-61dd7a370f70
â Distributed Cache
⢠Solution to increase scalability and
performance
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
40. AppFabric Cache - Scalability
⢠The problem:
â Increasing load, means distributing it on
more hosts
â ARR is not a data oriented tool
â The cache works at a data level
⢠Other advantages:
â Works also as a session provider
â It can be integrated in EF as a 2nd level cache
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
41. AppFabric Cache Architecture
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
43. AppFabric Cache Cluster
⢠Fault tolerant solution
⢠Every host keeps a replicated copy of the
data
⢠Doesnât require Windows Clustering
⢠Configuration based on the âleadHostâ
concept
â Host delegated to âmasterâ the cluster
configuration
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
44. AppFabric Cache Cluster
⢠Configuration can be kept on:
â SQL Server
⢠LeadHost not required
â Shared storage + XML file
⢠LeadHost required
â Custom provider
⢠LeadHost can be redundant
⢠Note: SQL / storage can become a single point
of failure! (valutare cluster etc)
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
45. AppFabric Cache TCP/IP
⢠Replication: 22236
⢠Arbitration: 22235
⢠Cluster: 22234
⢠Cache: 22233
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
46. AppFabric Cache Client
⢠Any .Net 3.5 o 4.0
⢠Install the âCache Clientâ feature only
⢠In Visual Studio, register:
â Microsoft.ApplicationServer.Caching.Client
â Microsoft.ApplicationServer.Caching.Core
⢠Assembly available in
â %systemroot%System32AppFabric
⢠Sostituire con SysWOW64 se x86 su x64
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
47. AppFabric Cache client session
⢠Enabling AppCache as a session provider
in web.config
<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProviderâ>
<providers>
<add name="AppFabricCacheSessionStoreProviderâ
type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider,
Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35â
cacheName="TestWPCâ sharedId="DemoApp1"/>
</providers>
</sessionState>
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
48. PowerShell
⢠Start-CacheCluster
⢠New-Cache
⢠Grant-CacheClientAllowedAccount
⢠Get-Cache
⢠Get-CacheStatistics
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
49. Concurrency models
⢠Optimistic
â Versioning
â Version number checked during a Put
operation
⢠Pessimistic
â Lock
â Watchout for performance issuesâŚ
â Note: Put doesnât check for locks!
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
50. AppFabric Cache
DEMO
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
51. PHP + MSSQL Sessions
⢠Scaling out an application on a web farm
requires proper session management
⢠Tipically offloaded to a database or a
state server
⢠ASP .Net offers ready made solutions
⢠PHP can be extended implementing a
small number of documented callbacks
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
52. PHP + MSSQL Sessions
⢠SQL Server Drivers for PHP 1.1
â Installable also via WebPI (recommended)
⢠In PHP.ini
â extension=php_pdo_sqlsrv.dll
â session.save_handler = user
⢠PHP handler to implement:
â session_set_save_handler
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
53. PHP + MS SQL Server Session handling
DEMO
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
54. Q&A
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro
55. Donât forget!
Get your free Azure pass! We want your feedback!
⢠30+15 days, no CC reqâd ⢠Win a WP7 smartphone
â http://bit.ly/ITCAMP11 â Fill in your feedback forms
â Promo code: ITCAMP11 â Raffle: end of the day
Premium conference on Microsoftâs Dev and ITPro technologies @itcampro / #itcampro