"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
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
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