SlideShare a Scribd company logo
The Top 5 
Things You 
are doing 
today to 
hinder 
scalability 
Dan Wilson
Who is Dan 
Wilson 
 Runs ChallengeWave.com, 
Datacurl LLC and NCDevCon 
 Owner of Model-Glue 
 Dad of 2, Husband to 1 
 Loves all things technical 
and a good debate 
Gives bad stock tips
Top5 scalabilityissues
Our Top 5 List 
1.Monolithic Design 
2.Mis-identification/Misuse of shared 
resources 
3.Over-reliance on Shared Scopes 
4.Stupid Developer Tricks 
5.Failure to design for cacheability
Monolithic Design 
Credit: http://thecodelesscode.com/case/33
Solution: Partition Databases
Solution: Decouple Functionality
Top5 scalabilityissues
Shared Resources
Scalability Curve
Fun With Math
Cost of WebServer File 
Web Server Thread 
+ Network to File System 
+ Disk Wait Time 
+ Disk Seek Time 
+ Transfer time to Webserver 
+ Transfer time to complete HTTP Request 
______________________ 
Total Time
Cost of 1 ColdFusion File 
Web Server Cost 
+ Network Cost to CF Server 
+ CF Parsing or Classloading Time 
+ Thread Wait time 
+ Thread CPU time 
+ Transfer time for CF buffer to Webserver 
______________________ 
Total Time
Cost of ColdFusion File With Query 
Web Server Cost 
+ ColdFusion File Cost 
+ Number of Queries ( Network time to DB + Query 
Execution Plan + Query Execution + Disk Wait + Disk Seek ) 
+ Network time to transport result set to CF 
+ CF Parsing Time 
______________________ 
Total Time
Types of Shared Resources 
● Memory, CPU, Disk Space 
● Disk Transfer (Databases!!) 
● JVM Space 
● ColdFusion template cache 
● ColdFusion Threads 
● Network Traffic 
● Database/Data Store 
● Blah 
● Blah 
● Blah
DANGER! DANGER!
Over-reliance on Shared Scopes 
● Session Scope use is the largest culprit 
● Sometimes you have to deoptimize a few 
operations in order to provide a scalable 
solution (like stop caching a state query in application 
scope) 
● If you want to use a shared scope and want to 
keep an eye on scalability later, use a lookup 
factory.
But, what about sticky sessions?
Asymmetric Load
<cfcomponent> 
<cfset variables.instance = structNew() /> 
<cffunction name="init" returntype="CurrentUserLoader" output="false" 
access="public"> 
<cfargument name="UserService" type="any" required="true"/> 
<cfset variables.UserService = arguments.UserService /> 
<cfreturn this /> 
</cffunction> 
<cffunction name="destroy" output="false" access="public" returntype="void" > 
<cfset cookie.UserToken = "" /> 
<cfset structDelete( cookie, "UserToken") /> 
</cffunction> 
<cffunction name="load" output="false" access="public" returntype="any" hint=""> 
<cfset var loggedInUserToken = "" /> 
<cfif structkeyExists(cookie, "UserToken")> 
<cfset loggedInUserToken = cookie.UserToken /> 
</cfif> 
<cfreturn variables.UserService.loadUserByToken("User", loggedInUserToken ) /> 
</cffunction> 
<cffunction name="set" output="false" access="public" returntype="void" hint=""> 
<cfargument name="user" type="any" required="true"/> 
<cfset cookie.UserToken = arguments.User.getUserToken() /> 
</cffunction> 
</cfcomponent>
Make the Key 
<cfset UserToken = “#UserID#|” /> 
<cfset UserToken = UserToken & “MySharedSecret|” /> 
<cfset UserToken = UserToken & “#Email#|” /> 
<cfset UserToken = UserToken & “#DateFormat(now(), 
“yyyymmdd”)# #TimeFormat(now(), “HH:mm:ss”)#” /> 
<cfset EncryptedUserToken = Encrypt(UserToken, 
encryptionKey, “AES”, “Hex”) /> 
Use the Key 
<cfset DecryptedUserTokenList = 
Decrypt(EncryptedUserToken, encryptionKey, “AES”, 
“Hex”) />
Avoiding Server Shared Scopes
Stupid Developer Tricks 
● Looped Queries 
● Lazy-Man Pagination 
● ScheduledTask-o-rama 
● Database Abuse
Problem: Query in Loop 
<cfloop query="OldLastLogins"> 
<cfquery name="UpdateUsers" datasource="#datasource#"> 
UPDATE Users 
SET Active = 0 
WHERE UserID = #OldLastLogins.UserID# 
</cfquery> 
</cfloop>
Solution?: SQL IN Statement 
<cfquery name="UpdateUsers" datasource="#datasource#"> 
UPDATE Users 
SET Active = 0 
WHERE UserID IN #ValueList(OldLastLogins, UserID)# 
</cfquery>
Top5 scalabilityissues
Solution: Joins 
<cfquery name="UpdateUsers" datasource="#datasource#"> 
UPDATE users 
INNER JOIN lastlogin ON users.userID = lastlogin.userID 
SET Active = 0 
WHERE lastlogin < now() 
</cfquery>
Problem: EntityToQuery 
<cfscript> 
EntityToQuery( 
EntityLoad("Users", 
{ Gender =arguments.genderID} 
) 
); 
</cfscript>
Ouch, It hurts when I do that!
Top5 scalabilityissues
Get Poll Answers 
<cfset PollAnswers = entityLoad("PollAnswers", 
{ 
PollID=entPoll[1].getPollID() 
}, "")> 
<cfif (isArray(Poll) AND arrayLen(Poll) gt 0) 
AND (isArray(PollOpt) 
AND arrayLen(PollOpt) gt 0) 
AND (isArray(PollAnswers) 
AND arrayLen(PollAnswers) gt 0)>
Poll Net Result 
JDBC 
Recordcount: 87,923 
Query Time: 14,290 ms 
All that for an 
ArrayLen() GT 0!
Bad or No Indexing
Effect of adding 1 Index to Production
What is the number one 
cause of missing indexes in 
production?
Cross Joining for Fun and Profit 
INSERT INTO activity ( memberID, ActivityTypeID, ActivityDate, 
UnitOfMeasure, MetricValue, Description) 
( 
SELECT m.memberID, a.ActivityTypeID, 
Date_Add( '2014-12-31', INTERVAL -mod( RAND(3)*300, 300) DAY), 
UnitOfMeasure, MetricValue, Description 
FROM member m, activity a 
WHERE a.elapsedtime IS NOT NULL 
AND a.description IS NOT NULL 
AND mod( mod(m.memberid, 2), mod( a.activityid, 5) ) = 1 
AND m.communityid = 1 
)
Problem: Lazy Man Pagination 
<cfoutput query="GetParks" 
startrow="#StartRow#" 
maxrows="#MaxRows#"> 
#ParkName# 
</cfoutput>
Solution: SQL 
MySql, PostgreSQL: LIMIT, OFFSET 
SQL Server: TOP, ORDER BY 
ORACLE: WHERE rownum <= 10
Problem: No Encapsulation 
<cfif qExists.idexists> 
<cfquery name="qUpdate" datasource="#somedatasource#"> 
UPDATE tblKalendarCategories 
SET CategoryName = '#form.CategoryName#', 
CategoryTextColor = '#form.CategoryTextColor#' 
WHERE CategoryID = #form.CategoryID#' /> 
</cfquery> 
<cfelse> 
<cfquery name="qCreate" datasource="#somedatasource#"> 
INSERT INTO tblKalendarCategories 
( CategoryName, CategoryBGColor, CategoryTextColor ) 
VALUES 
( '#form.CategoryName#', '#form.CategoryTextColor#') 
</cfquery> 
</cfif>
Solution: Encapsulate Data Access 
DAO.read(); 
DAO.save(); 
DAO.delete(); 
DAO.create(); 
DAO.exists(); 
DAO.update();
Cacheability
Caching Layers
Caching HTTP Responses 
Request: 
GET /index.html HTTP/1.1 
Host: www.example.com 
Response: 
HTTP/1.1 200 OK 
Date: Mon, 23 May 2005 22:38:34 GMT 
Server: Apache/1.3.3.7 (Unix) (Red- 
Hat/Linux) 
Last-Modified: Wed, 08 Jan 2003 
23:11:55 GMT 
ETag: "3f80f-1b6-3e1cb03b" 
Content-Type: text/html; charset=UTF-8 
Content-Length: 131 
Accept-Ranges: bytes 
Connection: close
Caching HTTP Responses
Designing for a CDN or Reverse 
Proxy Cache 
● Use HTTP verb “GET” for cacheable content 
● Use Last-Modified and Etag headers properly 
● Encapsulate code that changes content to 
centralize cache purge activity 
● Personalize with Cookies/Javascript 
● Set up shadow DNS to bypass caching 
● Use Javascript for interactions 
● Design data for high cache hit ratios
Caching Data
NoSQL as a Cache 
{ 
"_id" : ObjectId("51fff472e09a41ac19dd1876"), 
"NAME" : "Dan", 
"KIDS" : [ 
{ 
"NAME" : "Nick", 
"AGE" : 3.5, 
"DESCRIPTION" : "crazy", 
"HAIR" : "blonde" 
}, 
{ 
"NAME" : "Jack", 
"AGE" : 1.75 
} 
] 
}
NoSql Cache Diagram
Designing for a NoSQL Cache 
● Encapsulate calls to NoSQL engine 
● Encapsulate data publish methods 
● Design Content Purge algorithms well 
● Ensure the NoSQL engine is the authoritative 
source for the data being stored
Key/Value stores
Key Value Store Cache Diagram
Designing for a Key Value Store 
Cache 
● Encapsulate data access calls so you can 
purge/reload 
● Avoid cffunction output=”true” 
● Choose correct cache expiry strategy (time, 
LRU) 
● Easy to integrate with CF ORM 
● Use to remove load hotspoting
<cfcomponent> 
<cffunction name="loadProduct" returntype="Product" access="public"> 
<cfargument name="ProductID" type="numeric" required="true"/> 
<cfset var CacheKey = makeProductCacheKey(arguments.ProductID)/> 
<cfif variables.Cache.exists(CacheKey) IS false> 
<cflock name="#CacheKey#" timeout="10"> 
<cfif variables.Cache.exists(CacheKey) IS false> 
<cfset variables.Cache.put(CacheKey, 
variables.ProductService.get(arguments.ProductID))/> 
</cfif> 
</cflock> 
</cfif> 
<cfreturn variables.Cache.get(CacheKey)/> 
</cffunction> 
<cffunction name="saveProduct" returntype="Product" access="public"> 
<cfargument name="ProductID" type="numeric" required="true"/> 
<cfargument name="Name" type="text" required="true"/> 
<cfset var CacheKey = makeProductCacheKey(arguments.ProductID)/> 
<cfset var Product = variables.ProductService.get(arguments.ProductID)/> 
<cfset Product.setName(arguments.Name)/> 
<cfset Product = variables.ProductService.save(Product)/> 
<cfset variables.Cache.set(CacheKey, Product)/> 
<cfreturn variables.Cache.get(CacheKey)/> 
</cffunction> 
<cffunction name="makeProductCacheKey" returntype="string" access="private"> 
<cfargument name="ProductID" type="numeric" required="true"/> 
<cfreturn "Product_#val(arguments.ProductID)#"/> 
</cffunction> 
</cfcomponent>
Alternate Cache Methods
Time is Money
Thanks 
Dan Wilson 
nodans.com 
twitter.com/DanWilson 
www.linkedin.com/in/profile 
ofdanwilson
Appendix and helpful links
A Note for MySQL users 
[mysqld] 
innodb_file_per_table 
1. Do a mysqldump of all databases, procedures, triggers etc 
except the mysql and performance_schema databases 
2. Drop all databases except the above 2 databases 
3. Stop mysql 
4. Delete ibdata1 and ib_log files 
5. Start mysql 
6. Restore from dump
Content Delivery Network 
PROS 
● Caches HTTP Response 
● Ajax Friendly 
● Static Content 
● TTL/Manual Purge 
● Blazing Fast 
● Can be geographically 
distributed 
● No physical infrastructure to 
manage 
● Pay for what you use 
CONS 
● Pay for what you use 
● SSL is more complicated 
● Testing is more complicated 
● Reliant on external service 
● Not a good fit for intranet 
traffic
Content Delivery Options 
www.akamai.com 
aws.amazon.com/cloudfront
Monitoring Options 
ColdFusion Server 
Monitor 
CFStat 
New Relic – SaaS 
Fusion Reactor Monitor 
Nagios
Reverse Proxy Cache (Front Cache)
Reverse Proxy Cache 
PROS 
● Caches HTTP Response 
● Ajax Friendly 
● Static Content 
● TTL/Manual Purge 
● Blazing Fast 
● Many Open Source 
Products 
● Low operating expense 
● Can be used for intranets 
CONS 
● Personalization must be 
client side (no session 
scope) 
● Content is treated as static 
● Testing is more complicated 
● Must Manage Physical 
Infrastructure 
● Often Geographically 
Localized
Key Value Store 
PROS 
● Blazing Fast 
● Can cache any in-memory 
item 
● Some caches are 
sophisticated, some 
aren't 
● Can be distributed 
● Simple interfaces 
● Integrated in CF 
CONS 
● Many Cached Items 
represent an HTTP 
request 
● What is real-time to your 
application? 
● Key management can 
be hard 
● WYCIWYG (what you 
cache is what you get)
Helpful Links 
In-Memory Cache 
Reverse Proxy 
http://www.ehcache.org 
Cache 
http://terracotta.org/coldfusio 
http://varnish-cache.org 
n 
Tutorials on Caching 
http://Scalability www.squid-cache.org 
http://memcached.org 
Blogs 
http://trafficserver.apache.org Varnish: http://bit.ly/c1puD6 
http://cfwhisperer.com 
Squid Video: http://bit.ly/9iZu1Z 
http://highscalability.com 
Aaron West: http://bit.ly/a4sYcr 
Rob Brooks-Bilson: http://bit.ly/txser 
Terracotta Webinar: http://www.terracotta.org/webcasts 
This presentation: http://tinyurl.com/cacheme

More Related Content

PDF
Top5 scalabilityissues withappendix
PDF
HTML5 JavaScript APIs
PDF
JavaScript & HTML5 - Brave New World
PPTX
Nancy + rest mow2012
PDF
Java Web Programming [9/9] : Web Application Security
PDF
UA Testing with Selenium and PHPUnit - ZendCon 2013
PDF
The Spring 4 Update - Josh Long
PDF
UA testing with Selenium and PHPUnit - TrueNorthPHP 2013
Top5 scalabilityissues withappendix
HTML5 JavaScript APIs
JavaScript & HTML5 - Brave New World
Nancy + rest mow2012
Java Web Programming [9/9] : Web Application Security
UA Testing with Selenium and PHPUnit - ZendCon 2013
The Spring 4 Update - Josh Long
UA testing with Selenium and PHPUnit - TrueNorthPHP 2013

What's hot (20)

PDF
VCL template abstraction model and automated deployments to Fastly
PDF
Advanced VCL: how to use restart
PPT
Java. Explicit and Implicit Wait. Testing Ajax Applications
PDF
UA testing with Selenium and PHPUnit - PHPBenelux Summer BBQ
ODP
Beyond PHP - it's not (just) about the code
PDF
JDD2014: What you won't read in books about implementing REST services - Jak...
PDF
Apache Commons Pool and DBCP - Version 2 Update
PPT
Android httpclient
PDF
Security and performance designs for client-server communications
PDF
Bpug mcollective 20140624
PDF
React, Redux and es6/7
PPTX
A Groovy Kind of Java (San Francisco Java User Group)
PDF
Let ColdFusion ORM do the work for you!
ODP
My app is secure... I think
PDF
$.get, a Prime on Data Fetching
PDF
Power of Simplicity in FW/1
PDF
Varnish presentation for the Symfony Zaragoza user group
PDF
HTML5: friend or foe (to Flash)?
PDF
Mule caching strategy with redis cache
PDF
Geoserver GIS Mapping Solution
VCL template abstraction model and automated deployments to Fastly
Advanced VCL: how to use restart
Java. Explicit and Implicit Wait. Testing Ajax Applications
UA testing with Selenium and PHPUnit - PHPBenelux Summer BBQ
Beyond PHP - it's not (just) about the code
JDD2014: What you won't read in books about implementing REST services - Jak...
Apache Commons Pool and DBCP - Version 2 Update
Android httpclient
Security and performance designs for client-server communications
Bpug mcollective 20140624
React, Redux and es6/7
A Groovy Kind of Java (San Francisco Java User Group)
Let ColdFusion ORM do the work for you!
My app is secure... I think
$.get, a Prime on Data Fetching
Power of Simplicity in FW/1
Varnish presentation for the Symfony Zaragoza user group
HTML5: friend or foe (to Flash)?
Mule caching strategy with redis cache
Geoserver GIS Mapping Solution
Ad

Viewers also liked (20)

PDF
Mura intergration-slides
PDF
Keep Applications Online
PPTX
Sql killedserver
PDF
Building better SQL Server Databases
PDF
Hey my web app is slow where is the problem
PDF
Dev objecttives-2015 auth-auth-fine-grained-slides
PDF
ColdFusion in Transit action
PDF
Fr sponsor talk may 2015
PDF
API Management from the Trenches
PDF
Expand Your ColdFusion App Power with AWS
PPTX
Preso slidedeck
PDF
Accessible Video Anywhere with ColdFusion an AWS
PDF
Relationships are hard
PDF
Adaptive Development in a Creative World
PDF
Java scriptconfusingbits
PPTX
Cf objective2014 software-craftsmanship
PDF
Emberjs building-ambitious-web-applications
PDF
Dependency injectionpreso
PPTX
Single page apps_with_cf_and_angular[1]
PDF
Getting started with mobile application development
Mura intergration-slides
Keep Applications Online
Sql killedserver
Building better SQL Server Databases
Hey my web app is slow where is the problem
Dev objecttives-2015 auth-auth-fine-grained-slides
ColdFusion in Transit action
Fr sponsor talk may 2015
API Management from the Trenches
Expand Your ColdFusion App Power with AWS
Preso slidedeck
Accessible Video Anywhere with ColdFusion an AWS
Relationships are hard
Adaptive Development in a Creative World
Java scriptconfusingbits
Cf objective2014 software-craftsmanship
Emberjs building-ambitious-web-applications
Dependency injectionpreso
Single page apps_with_cf_and_angular[1]
Getting started with mobile application development
Ad

Similar to Top5 scalabilityissues (20)

PPTX
Real World Lessons on the Pain Points of Node.js Applications
PDF
Web-Performance
PPTX
Coldfusion with Keith Diehl
PPTX
Scale Your Data Tier with Windows Server AppFabric
PPTX
Solving anything in VCL
PPTX
Caching & Performance In Cold Fusion
PPT
Language enhancement in ColdFusion 8
PDF
Symfony2 - from the trenches
PDF
Refreshing mule cache using oracle database change notification
PPTX
Dave Orchard - Offline Web Apps with HTML5
PDF
Ajax Performance Tuning and Best Practices
PPTX
Cache is King
PDF
Our application got popular and now it breaks
PDF
Our application got popular and now it breaks
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPTX
Presentation of OrientDB v2.2 - Webinar
PDF
Rich Portlet Development in uPortal
PDF
Build powerfull and smart web applications with Symfony2
PPTX
Cookies
PDF
Into The Box 2018 Ortus Keynote
Real World Lessons on the Pain Points of Node.js Applications
Web-Performance
Coldfusion with Keith Diehl
Scale Your Data Tier with Windows Server AppFabric
Solving anything in VCL
Caching & Performance In Cold Fusion
Language enhancement in ColdFusion 8
Symfony2 - from the trenches
Refreshing mule cache using oracle database change notification
Dave Orchard - Offline Web Apps with HTML5
Ajax Performance Tuning and Best Practices
Cache is King
Our application got popular and now it breaks
Our application got popular and now it breaks
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Presentation of OrientDB v2.2 - Webinar
Rich Portlet Development in uPortal
Build powerfull and smart web applications with Symfony2
Cookies
Into The Box 2018 Ortus Keynote

More from ColdFusionConference (20)

PDF
Api manager preconference
PDF
PDF
API Economy, Realizing the Business Value of APIs
PDF
Don't just pdf, Smart PDF
PDF
Crafting ColdFusion Applications like an Architect
PDF
Security And Access Control For APIS using CF API Manager
PDF
Monetizing Business Models: ColdFusion and APIS
PDF
Become a Security Rockstar with ColdFusion 2016
PDF
Developer Insights for Application Upgrade to ColdFusion 2016
PDF
Where is cold fusion headed
PDF
ColdFusion Keynote: Building the Agile Web Since 1995
PDF
Instant ColdFusion with Vagrant
PPT
Restful services with ColdFusion
PDF
Super Fast Application development with Mura CMS
PDF
Build your own secure and real-time dashboard for mobile and web
PDF
Why Everyone else writes bad code
PDF
Securing applications
PDF
Testing automaton
PDF
Rest ful tools for lazy experts
PDF
Herding cats managing ColdFusion servers with commandbox
Api manager preconference
API Economy, Realizing the Business Value of APIs
Don't just pdf, Smart PDF
Crafting ColdFusion Applications like an Architect
Security And Access Control For APIS using CF API Manager
Monetizing Business Models: ColdFusion and APIS
Become a Security Rockstar with ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016
Where is cold fusion headed
ColdFusion Keynote: Building the Agile Web Since 1995
Instant ColdFusion with Vagrant
Restful services with ColdFusion
Super Fast Application development with Mura CMS
Build your own secure and real-time dashboard for mobile and web
Why Everyone else writes bad code
Securing applications
Testing automaton
Rest ful tools for lazy experts
Herding cats managing ColdFusion servers with commandbox

Recently uploaded (20)

PDF
AI in Product Development-omnex systems
PPTX
L1 - Introduction to python Backend.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
history of c programming in notes for students .pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
ai tools demonstartion for schools and inter college
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Introduction to Artificial Intelligence
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
System and Network Administraation Chapter 3
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
medical staffing services at VALiNTRY
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
AI in Product Development-omnex systems
L1 - Introduction to python Backend.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PTS Company Brochure 2025 (1).pdf.......
history of c programming in notes for students .pptx
Odoo POS Development Services by CandidRoot Solutions
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
ai tools demonstartion for schools and inter college
Reimagine Home Health with the Power of Agentic AI​
Understanding Forklifts - TECH EHS Solution
Wondershare Filmora 15 Crack With Activation Key [2025
Introduction to Artificial Intelligence
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
System and Network Administraation Chapter 3
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
wealthsignaloriginal-com-DS-text-... (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41

Top5 scalabilityissues

  • 1. The Top 5 Things You are doing today to hinder scalability Dan Wilson
  • 2. Who is Dan Wilson  Runs ChallengeWave.com, Datacurl LLC and NCDevCon  Owner of Model-Glue  Dad of 2, Husband to 1  Loves all things technical and a good debate Gives bad stock tips
  • 4. Our Top 5 List 1.Monolithic Design 2.Mis-identification/Misuse of shared resources 3.Over-reliance on Shared Scopes 4.Stupid Developer Tricks 5.Failure to design for cacheability
  • 5. Monolithic Design Credit: http://thecodelesscode.com/case/33
  • 12. Cost of WebServer File Web Server Thread + Network to File System + Disk Wait Time + Disk Seek Time + Transfer time to Webserver + Transfer time to complete HTTP Request ______________________ Total Time
  • 13. Cost of 1 ColdFusion File Web Server Cost + Network Cost to CF Server + CF Parsing or Classloading Time + Thread Wait time + Thread CPU time + Transfer time for CF buffer to Webserver ______________________ Total Time
  • 14. Cost of ColdFusion File With Query Web Server Cost + ColdFusion File Cost + Number of Queries ( Network time to DB + Query Execution Plan + Query Execution + Disk Wait + Disk Seek ) + Network time to transport result set to CF + CF Parsing Time ______________________ Total Time
  • 15. Types of Shared Resources ● Memory, CPU, Disk Space ● Disk Transfer (Databases!!) ● JVM Space ● ColdFusion template cache ● ColdFusion Threads ● Network Traffic ● Database/Data Store ● Blah ● Blah ● Blah
  • 17. Over-reliance on Shared Scopes ● Session Scope use is the largest culprit ● Sometimes you have to deoptimize a few operations in order to provide a scalable solution (like stop caching a state query in application scope) ● If you want to use a shared scope and want to keep an eye on scalability later, use a lookup factory.
  • 18. But, what about sticky sessions?
  • 20. <cfcomponent> <cfset variables.instance = structNew() /> <cffunction name="init" returntype="CurrentUserLoader" output="false" access="public"> <cfargument name="UserService" type="any" required="true"/> <cfset variables.UserService = arguments.UserService /> <cfreturn this /> </cffunction> <cffunction name="destroy" output="false" access="public" returntype="void" > <cfset cookie.UserToken = "" /> <cfset structDelete( cookie, "UserToken") /> </cffunction> <cffunction name="load" output="false" access="public" returntype="any" hint=""> <cfset var loggedInUserToken = "" /> <cfif structkeyExists(cookie, "UserToken")> <cfset loggedInUserToken = cookie.UserToken /> </cfif> <cfreturn variables.UserService.loadUserByToken("User", loggedInUserToken ) /> </cffunction> <cffunction name="set" output="false" access="public" returntype="void" hint=""> <cfargument name="user" type="any" required="true"/> <cfset cookie.UserToken = arguments.User.getUserToken() /> </cffunction> </cfcomponent>
  • 21. Make the Key <cfset UserToken = “#UserID#|” /> <cfset UserToken = UserToken & “MySharedSecret|” /> <cfset UserToken = UserToken & “#Email#|” /> <cfset UserToken = UserToken & “#DateFormat(now(), “yyyymmdd”)# #TimeFormat(now(), “HH:mm:ss”)#” /> <cfset EncryptedUserToken = Encrypt(UserToken, encryptionKey, “AES”, “Hex”) /> Use the Key <cfset DecryptedUserTokenList = Decrypt(EncryptedUserToken, encryptionKey, “AES”, “Hex”) />
  • 23. Stupid Developer Tricks ● Looped Queries ● Lazy-Man Pagination ● ScheduledTask-o-rama ● Database Abuse
  • 24. Problem: Query in Loop <cfloop query="OldLastLogins"> <cfquery name="UpdateUsers" datasource="#datasource#"> UPDATE Users SET Active = 0 WHERE UserID = #OldLastLogins.UserID# </cfquery> </cfloop>
  • 25. Solution?: SQL IN Statement <cfquery name="UpdateUsers" datasource="#datasource#"> UPDATE Users SET Active = 0 WHERE UserID IN #ValueList(OldLastLogins, UserID)# </cfquery>
  • 27. Solution: Joins <cfquery name="UpdateUsers" datasource="#datasource#"> UPDATE users INNER JOIN lastlogin ON users.userID = lastlogin.userID SET Active = 0 WHERE lastlogin < now() </cfquery>
  • 28. Problem: EntityToQuery <cfscript> EntityToQuery( EntityLoad("Users", { Gender =arguments.genderID} ) ); </cfscript>
  • 29. Ouch, It hurts when I do that!
  • 31. Get Poll Answers <cfset PollAnswers = entityLoad("PollAnswers", { PollID=entPoll[1].getPollID() }, "")> <cfif (isArray(Poll) AND arrayLen(Poll) gt 0) AND (isArray(PollOpt) AND arrayLen(PollOpt) gt 0) AND (isArray(PollAnswers) AND arrayLen(PollAnswers) gt 0)>
  • 32. Poll Net Result JDBC Recordcount: 87,923 Query Time: 14,290 ms All that for an ArrayLen() GT 0!
  • 33. Bad or No Indexing
  • 34. Effect of adding 1 Index to Production
  • 35. What is the number one cause of missing indexes in production?
  • 36. Cross Joining for Fun and Profit INSERT INTO activity ( memberID, ActivityTypeID, ActivityDate, UnitOfMeasure, MetricValue, Description) ( SELECT m.memberID, a.ActivityTypeID, Date_Add( '2014-12-31', INTERVAL -mod( RAND(3)*300, 300) DAY), UnitOfMeasure, MetricValue, Description FROM member m, activity a WHERE a.elapsedtime IS NOT NULL AND a.description IS NOT NULL AND mod( mod(m.memberid, 2), mod( a.activityid, 5) ) = 1 AND m.communityid = 1 )
  • 37. Problem: Lazy Man Pagination <cfoutput query="GetParks" startrow="#StartRow#" maxrows="#MaxRows#"> #ParkName# </cfoutput>
  • 38. Solution: SQL MySql, PostgreSQL: LIMIT, OFFSET SQL Server: TOP, ORDER BY ORACLE: WHERE rownum <= 10
  • 39. Problem: No Encapsulation <cfif qExists.idexists> <cfquery name="qUpdate" datasource="#somedatasource#"> UPDATE tblKalendarCategories SET CategoryName = '#form.CategoryName#', CategoryTextColor = '#form.CategoryTextColor#' WHERE CategoryID = #form.CategoryID#' /> </cfquery> <cfelse> <cfquery name="qCreate" datasource="#somedatasource#"> INSERT INTO tblKalendarCategories ( CategoryName, CategoryBGColor, CategoryTextColor ) VALUES ( '#form.CategoryName#', '#form.CategoryTextColor#') </cfquery> </cfif>
  • 40. Solution: Encapsulate Data Access DAO.read(); DAO.save(); DAO.delete(); DAO.create(); DAO.exists(); DAO.update();
  • 43. Caching HTTP Responses Request: GET /index.html HTTP/1.1 Host: www.example.com Response: HTTP/1.1 200 OK Date: Mon, 23 May 2005 22:38:34 GMT Server: Apache/1.3.3.7 (Unix) (Red- Hat/Linux) Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT ETag: "3f80f-1b6-3e1cb03b" Content-Type: text/html; charset=UTF-8 Content-Length: 131 Accept-Ranges: bytes Connection: close
  • 45. Designing for a CDN or Reverse Proxy Cache ● Use HTTP verb “GET” for cacheable content ● Use Last-Modified and Etag headers properly ● Encapsulate code that changes content to centralize cache purge activity ● Personalize with Cookies/Javascript ● Set up shadow DNS to bypass caching ● Use Javascript for interactions ● Design data for high cache hit ratios
  • 47. NoSQL as a Cache { "_id" : ObjectId("51fff472e09a41ac19dd1876"), "NAME" : "Dan", "KIDS" : [ { "NAME" : "Nick", "AGE" : 3.5, "DESCRIPTION" : "crazy", "HAIR" : "blonde" }, { "NAME" : "Jack", "AGE" : 1.75 } ] }
  • 49. Designing for a NoSQL Cache ● Encapsulate calls to NoSQL engine ● Encapsulate data publish methods ● Design Content Purge algorithms well ● Ensure the NoSQL engine is the authoritative source for the data being stored
  • 51. Key Value Store Cache Diagram
  • 52. Designing for a Key Value Store Cache ● Encapsulate data access calls so you can purge/reload ● Avoid cffunction output=”true” ● Choose correct cache expiry strategy (time, LRU) ● Easy to integrate with CF ORM ● Use to remove load hotspoting
  • 53. <cfcomponent> <cffunction name="loadProduct" returntype="Product" access="public"> <cfargument name="ProductID" type="numeric" required="true"/> <cfset var CacheKey = makeProductCacheKey(arguments.ProductID)/> <cfif variables.Cache.exists(CacheKey) IS false> <cflock name="#CacheKey#" timeout="10"> <cfif variables.Cache.exists(CacheKey) IS false> <cfset variables.Cache.put(CacheKey, variables.ProductService.get(arguments.ProductID))/> </cfif> </cflock> </cfif> <cfreturn variables.Cache.get(CacheKey)/> </cffunction> <cffunction name="saveProduct" returntype="Product" access="public"> <cfargument name="ProductID" type="numeric" required="true"/> <cfargument name="Name" type="text" required="true"/> <cfset var CacheKey = makeProductCacheKey(arguments.ProductID)/> <cfset var Product = variables.ProductService.get(arguments.ProductID)/> <cfset Product.setName(arguments.Name)/> <cfset Product = variables.ProductService.save(Product)/> <cfset variables.Cache.set(CacheKey, Product)/> <cfreturn variables.Cache.get(CacheKey)/> </cffunction> <cffunction name="makeProductCacheKey" returntype="string" access="private"> <cfargument name="ProductID" type="numeric" required="true"/> <cfreturn "Product_#val(arguments.ProductID)#"/> </cffunction> </cfcomponent>
  • 56. Thanks Dan Wilson nodans.com twitter.com/DanWilson www.linkedin.com/in/profile ofdanwilson
  • 58. A Note for MySQL users [mysqld] innodb_file_per_table 1. Do a mysqldump of all databases, procedures, triggers etc except the mysql and performance_schema databases 2. Drop all databases except the above 2 databases 3. Stop mysql 4. Delete ibdata1 and ib_log files 5. Start mysql 6. Restore from dump
  • 59. Content Delivery Network PROS ● Caches HTTP Response ● Ajax Friendly ● Static Content ● TTL/Manual Purge ● Blazing Fast ● Can be geographically distributed ● No physical infrastructure to manage ● Pay for what you use CONS ● Pay for what you use ● SSL is more complicated ● Testing is more complicated ● Reliant on external service ● Not a good fit for intranet traffic
  • 60. Content Delivery Options www.akamai.com aws.amazon.com/cloudfront
  • 61. Monitoring Options ColdFusion Server Monitor CFStat New Relic – SaaS Fusion Reactor Monitor Nagios
  • 62. Reverse Proxy Cache (Front Cache)
  • 63. Reverse Proxy Cache PROS ● Caches HTTP Response ● Ajax Friendly ● Static Content ● TTL/Manual Purge ● Blazing Fast ● Many Open Source Products ● Low operating expense ● Can be used for intranets CONS ● Personalization must be client side (no session scope) ● Content is treated as static ● Testing is more complicated ● Must Manage Physical Infrastructure ● Often Geographically Localized
  • 64. Key Value Store PROS ● Blazing Fast ● Can cache any in-memory item ● Some caches are sophisticated, some aren't ● Can be distributed ● Simple interfaces ● Integrated in CF CONS ● Many Cached Items represent an HTTP request ● What is real-time to your application? ● Key management can be hard ● WYCIWYG (what you cache is what you get)
  • 65. Helpful Links In-Memory Cache Reverse Proxy http://www.ehcache.org Cache http://terracotta.org/coldfusio http://varnish-cache.org n Tutorials on Caching http://Scalability www.squid-cache.org http://memcached.org Blogs http://trafficserver.apache.org Varnish: http://bit.ly/c1puD6 http://cfwhisperer.com Squid Video: http://bit.ly/9iZu1Z http://highscalability.com Aaron West: http://bit.ly/a4sYcr Rob Brooks-Bilson: http://bit.ly/txser Terracotta Webinar: http://www.terracotta.org/webcasts This presentation: http://tinyurl.com/cacheme