SlideShare uma empresa Scribd logo
1 de 31
Boosting the performance of
       your Rails apps




      Jakarta RoR meetup, January 2013

               Matt Kuklinski
               CTO, Gopher
Where to start?
• There are many ways to improve RoR app
  speed – some easy, some hard
• Concentrate on methods that give you
  the biggest bang for the buck

• This presentation shows a few different
  methods that should give you a good
  performance return for your time
  investment
1. DB Indexes
• Your app will be constrained by database
  performance
• Appropriate DB indexes can give you 100x
  performance gains on large tables
• Not all rails developers realise how
  important this is
• It’s easy to add indexes:
class AddIndexToClientIndustry < ActiveRecord::Migration
  def change
    add_index :client_industries, :client_id
  end
end
Example with index
CREATE INDEX
addresses_addressable_id_addressable_type_idx
ON addresses USING btree
(addressable_id, addressable_type);

t1 =   Time.now
 c =   Company.find(178389)
 a =   c.addresses.first
t2 =   Time.now
puts   "---Operation took #{t2-t1} seconds---”

Result with index:
---Operation took 0.012412 seconds---
Now without the index
DROP INDEX
addresses_addressable_id_addressable_type_idx;

t1 =   Time.now
 c =   Company.find(178389)
 a =   c.addresses.first
t2 =   Time.now
puts   "---Operation took #{t2-t1} seconds---”

Result without index:
---Operation took 0.378073 seconds---

0.378073 / 0.012412 = 30.46 times slower without the index
Index Tips
• add indexes to all referential attributes,
  and other attributes that you regularly
  search or sort over if they contain a lot of
  distinct values

• don't add too many indexes - each one
  increases the DB size and reduces the
  performance of insert and update queries
2. Minimise amount of DB queries
• RoR makes it easy to program quickly

• The downside:
  RoR makes it easy for the number of
  database queries per request to explode
  out of control
Example: N+1 queries
• Let’s say we have a Client model, and each Client can
  have one or more industries through ClientIndustry.

• We want to show a list of clients, and their primary
  industries:

<% @clients.each do |client| %>
  <tr>
    <td><%= client.id %></td>
    <td><%= client.business_name %></td>
    <td><%= client.industries.first.name %></td>
  </tr>
<% end %>
Be careful doing this:
# app/controllers/clients_controller.rb
def index
    @clients = Client.all
end


If you have 50 clients, then 51 DB queries will be run:
Processing by ClientsController#index as HTML
SELECT "clients".* FROM "clients"
SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON
"industries"."id" = "client_industries"."industry_id" WHERE
"client_industries"."client_id" = 1 LIMIT 1
SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON
"industries"."id" = "client_industries"."industry_id" WHERE
"client_industries"."client_id" = 2 LIMIT 1
SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON
"industries"."id" = "client_industries"."industry_id" WHERE
"client_industries"."client_id" = 3 LIMIT 1
…
Solution: Eager Loading
# app/controllers/clients_controller.rb
def index
    @clients = Client.includes(:industries).all
end



Now just 2 or 3 queries are performed instead of 51

Processing by ClientsController#index as HTML
SELECT "clients".* FROM "clients"
SELECT "client_industries".* FROM "client_industries" WHERE
"client_industries"."client_id" IN (1, 2, 3)
SELECT "industries".* FROM "industries" WHERE "industries"."id" IN (1, 5,
7, 8, 4)
3. Minimise memory usage
• Only use gems that you actually need

• Don’t load objects into memory unless
  you need to use them

• When processing massive datasets, split
  them into batches
Example: find_each
An example using real data:
Using find:
t1 = Time.now
Company.where(:country_id=>1).find do |c|
  puts "do something!" if ['Mattski
Test'].include?(c.common_name)
end
t2 = Time.now
puts "---Operation took #{t2-t1} seconds---”


Result:
1 query, taking 46.65 seconds
Now using find_each:
t1 = Time.now
Company.where(:country_id=>1).find_each do |c|
  puts "do something!" if ['Mattski
Test'].include?(c.common_name)
end
t2 = Time.now
puts "---Operation took #{t2-t1} seconds---"


Result:
>100 queries, taking 15.53 seconds in total (3x faster)

Sometimes more queries is
better!
3. Caching
• Can make a huge difference to
  performance
• Lots of options:
  – page caching
  – action caching
  – fragment caching
  – Memcached, Redis
• Tip: get your data model correct first.
  Caching can hide structural problems
What is Memcached?
• Free & open source, high-performance,
  distributed memory object caching system.

• Memcached is an in-memory key-value store
  for small chunks of arbitrary data (strings,
  objects) from results of database calls, API
  calls, or page rendering.

• www.memcached.org
What is Redis?
• Redis is an open source, advanced key-
  value store. It is often referred to as a
  data structure server since keys can
  contain strings, hashes, lists, sets and
  sorted sets.

• redis.io
Pre-calculate summary data
Example:
• We have tables holding sales, sales_reps and teams
• We need to provide live monthly and daily charts showing
   cumulative sales per sales_rep, per team, and for the company as
   a whole
• We could produce a complicated ruby method that results in a
   query like this:
 select date(sales.created_at) as sale_date
       ,sales_reps.name
       ,sum(sales.amount) as daily_sales
   from sales
   join sales_reps on sales_reps.id = sales.sales_rep_id
  where sales.created_at > '2013-01-01’
  group by 1,2;
But that’s not very efficient if we have 300 sales reps and managers
checking all their charts every few minutes. How can we speed it up?
Solution:
• Summarise the data in a sales_metrics table with good indexes and
  use observers and delayed_jobs to recalculate the sales data in
  near-real time.

• Then we can do:
sales_rep.sales_metrics.where(:date>'2013-01-01')

To get an optimised query like this:
select date
      ,sales_rep_id
      ,daily_sales
  from sales_metrics
 where sales_metrics.date >= '2013-01-01’

Now instead of 300 sales reps, imagine having 20,000 daytraders
checking their daily stock portfolio charts… It has to be pre-
calculated.
6. Make web requests fast
• You have a limited number of processes
  available to serve web requests, so they
  need to be fast
• Ideally, web processes should finish
  within milliseconds. 1-2 seconds is slow.
  10+ seconds is very slow.
• If you have slow web requests then your
  rails app wont be able to support many
  simultaneous users.
Solution: use background processes
• Use background processes such as delayed jobs for long-running
  jobs. This will free your web processes up to handle more
  requests.

• What types of things?
   –   sending email
   –   running reports
   –   processing images
   –   obtaining information from third party APIs

• Suggestion: use priorities so that important background processes
  get actioned before less important ones if there is a build up in
  jobs

• Note: Rails 4 will support background processing out of the box
7. Monitor performance
• Make sure to monitor the performance of
  your apps so that you can pinpoint which
  areas are running slowly.

• New Relic is an excellent tool for monitor
  rails apps
What does this tell me?
- Response time is good.
     - There’s no request queuing.
- I can scale back the web processes
What does this tell me?
-   Performance is not that great
    -  The database is being overworked
-   There may be some inefficient DB queries
The slowness is almost entirely caused by the
SearchController. This is a target for optimisation.
8. Use an in-memory DB
• Databases are fast when the searching
  and sorting is done in memory

• They slow down a lot when they have to
  go to disk
Solution: keep your DB trim
• Try to limit the size of the DB so that it
  fits entirely within memory
• Move non-essential information out of the
  main DB into a secondary DB or elsewhere
  (i.e. audit logs, inactive accounts, old
  email logs, etc.)
• Consider using non-relational databases if
  you have massive storage requirements
9. Manage your load
• load balancing
  – essential for public web apps
  – Cloud hosting providers help to manage this for
    you

• database replication
  – run heavy reports on a replicated database so
    that the performance of the main database isn’t
    affected

• database sharding
More performance tips
• Use a content distribution network for static
  files.
  – AWS CloudFront, etc.

• Make the UI asynchronous
  – use AJAX lazy loading for anything that takes
    more than 1-2 seconds to load

• Use a Service Oriented Architecture, so that
  some actions can be done in parallel on a
  different hosting stack
Thanks for your interest!

• Contact
  – www.linkedin.com/in/matthewkuklinski
  – @mattkuklinski

  – www.gopher.co.id
  – www.gopher.co.nz

Mais conteúdo relacionado

Mais procurados

(BDT317) Building A Data Lake On AWS
(BDT317) Building A Data Lake On AWS(BDT317) Building A Data Lake On AWS
(BDT317) Building A Data Lake On AWSAmazon Web Services
 
AWS Webcast - Managing Big Data in the AWS Cloud_20140924
AWS Webcast - Managing Big Data in the AWS Cloud_20140924AWS Webcast - Managing Big Data in the AWS Cloud_20140924
AWS Webcast - Managing Big Data in the AWS Cloud_20140924Amazon Web Services
 
Migrate a successful transactional database to azure
Migrate a successful transactional database to azureMigrate a successful transactional database to azure
Migrate a successful transactional database to azureIke Ellis
 
Brk3043 azure sql db intelligent cloud database for app developers - wash dc
Brk3043 azure sql db   intelligent cloud database for app developers - wash dcBrk3043 azure sql db   intelligent cloud database for app developers - wash dc
Brk3043 azure sql db intelligent cloud database for app developers - wash dcBob Ward
 
IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...
IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...
IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...In-Memory Computing Summit
 
Handling of Large Data by Salesforce
Handling of Large Data by SalesforceHandling of Large Data by Salesforce
Handling of Large Data by SalesforceThinqloud
 
Azure Databricks is Easier Than You Think
Azure Databricks is Easier Than You ThinkAzure Databricks is Easier Than You Think
Azure Databricks is Easier Than You ThinkIke Ellis
 
AWS Webcast - Data Integration into Amazon Redshift
AWS Webcast - Data Integration into Amazon RedshiftAWS Webcast - Data Integration into Amazon Redshift
AWS Webcast - Data Integration into Amazon RedshiftAmazon Web Services
 
SharePoint Databases: What you need to know (201512)
SharePoint Databases: What you need to know (201512)SharePoint Databases: What you need to know (201512)
SharePoint Databases: What you need to know (201512)Alan Eardley
 
AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...
AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...
AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...Amazon Web Services
 
Introduction to Windows Azure and Windows Azure SQL Database
Introduction to Windows Azure and Windows Azure SQL DatabaseIntroduction to Windows Azure and Windows Azure SQL Database
Introduction to Windows Azure and Windows Azure SQL DatabaseVikas Sahni
 
Modern Data Architectures for Business Outcomes
Modern Data Architectures for Business OutcomesModern Data Architectures for Business Outcomes
Modern Data Architectures for Business OutcomesAmazon Web Services
 
Business Intelligence with SQL Server
Business Intelligence with SQL ServerBusiness Intelligence with SQL Server
Business Intelligence with SQL ServerPeter Gfader
 
Cassandra at eBay - Cassandra Summit 2013
Cassandra at eBay - Cassandra Summit 2013Cassandra at eBay - Cassandra Summit 2013
Cassandra at eBay - Cassandra Summit 2013Jay Patel
 
SRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpaces
SRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpacesSRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpaces
SRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpacesAmazon Web Services
 
AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...
AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...
AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...Amazon Web Services
 
How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013
How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013
How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013Amazon Web Services
 
Building Serverless Web Applications - DevDay Los Angeles 2017
Building Serverless Web Applications - DevDay Los Angeles 2017Building Serverless Web Applications - DevDay Los Angeles 2017
Building Serverless Web Applications - DevDay Los Angeles 2017Amazon Web Services
 

Mais procurados (20)

(BDT317) Building A Data Lake On AWS
(BDT317) Building A Data Lake On AWS(BDT317) Building A Data Lake On AWS
(BDT317) Building A Data Lake On AWS
 
AWS Webcast - Managing Big Data in the AWS Cloud_20140924
AWS Webcast - Managing Big Data in the AWS Cloud_20140924AWS Webcast - Managing Big Data in the AWS Cloud_20140924
AWS Webcast - Managing Big Data in the AWS Cloud_20140924
 
Migrate a successful transactional database to azure
Migrate a successful transactional database to azureMigrate a successful transactional database to azure
Migrate a successful transactional database to azure
 
Brk3043 azure sql db intelligent cloud database for app developers - wash dc
Brk3043 azure sql db   intelligent cloud database for app developers - wash dcBrk3043 azure sql db   intelligent cloud database for app developers - wash dc
Brk3043 azure sql db intelligent cloud database for app developers - wash dc
 
IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...
IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...
IMCSummit 2015 - Day 2 Developer Track - The Internet of Analytics – Discover...
 
Handling of Large Data by Salesforce
Handling of Large Data by SalesforceHandling of Large Data by Salesforce
Handling of Large Data by Salesforce
 
Azure Databricks is Easier Than You Think
Azure Databricks is Easier Than You ThinkAzure Databricks is Easier Than You Think
Azure Databricks is Easier Than You Think
 
AWS Webcast - Data Integration into Amazon Redshift
AWS Webcast - Data Integration into Amazon RedshiftAWS Webcast - Data Integration into Amazon Redshift
AWS Webcast - Data Integration into Amazon Redshift
 
SharePoint Databases: What you need to know (201512)
SharePoint Databases: What you need to know (201512)SharePoint Databases: What you need to know (201512)
SharePoint Databases: What you need to know (201512)
 
Azure SQL Database
Azure SQL DatabaseAzure SQL Database
Azure SQL Database
 
AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...
AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...
AWS re:Invent 2016| GAM301 | How EA Leveraged Amazon Redshift and AWS Partner...
 
Introduction to Windows Azure and Windows Azure SQL Database
Introduction to Windows Azure and Windows Azure SQL DatabaseIntroduction to Windows Azure and Windows Azure SQL Database
Introduction to Windows Azure and Windows Azure SQL Database
 
Modern Data Architectures for Business Outcomes
Modern Data Architectures for Business OutcomesModern Data Architectures for Business Outcomes
Modern Data Architectures for Business Outcomes
 
Business Intelligence with SQL Server
Business Intelligence with SQL ServerBusiness Intelligence with SQL Server
Business Intelligence with SQL Server
 
Cassandra at eBay - Cassandra Summit 2013
Cassandra at eBay - Cassandra Summit 2013Cassandra at eBay - Cassandra Summit 2013
Cassandra at eBay - Cassandra Summit 2013
 
SRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpaces
SRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpacesSRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpaces
SRV406 How to Step Off the PC Refresh Treadmill with Amazon WorkSpaces
 
AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...
AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...
AWS re:Invent 2016: Fireside chat with Groupon, Intuit, and LifeLock on solvi...
 
How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013
How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013
How Amazon.com is Leveraging Amazon Redshift (DAT306) | AWS re:Invent 2013
 
Building Serverless Web Applications - DevDay Los Angeles 2017
Building Serverless Web Applications - DevDay Los Angeles 2017Building Serverless Web Applications - DevDay Los Angeles 2017
Building Serverless Web Applications - DevDay Los Angeles 2017
 
Hosting Tableau on AWS
Hosting Tableau on AWSHosting Tableau on AWS
Hosting Tableau on AWS
 

Semelhante a Boosting the Performance of your Rails Apps

Enterprise Data World 2018 - Building Cloud Self-Service Analytical Solution
Enterprise Data World 2018 - Building Cloud Self-Service Analytical SolutionEnterprise Data World 2018 - Building Cloud Self-Service Analytical Solution
Enterprise Data World 2018 - Building Cloud Self-Service Analytical SolutionDmitry Anoshin
 
Designing, Building, and Maintaining Large Cubes using Lessons Learned
Designing, Building, and Maintaining Large Cubes using Lessons LearnedDesigning, Building, and Maintaining Large Cubes using Lessons Learned
Designing, Building, and Maintaining Large Cubes using Lessons LearnedDenny Lee
 
Performance Optimization of Cloud Based Applications by Peter Smith, ACL
Performance Optimization of Cloud Based Applications by Peter Smith, ACLPerformance Optimization of Cloud Based Applications by Peter Smith, ACL
Performance Optimization of Cloud Based Applications by Peter Smith, ACLTriNimbus
 
Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...
Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...
Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...Denny Lee
 
Getting Started with Amazon Redshift
Getting Started with Amazon RedshiftGetting Started with Amazon Redshift
Getting Started with Amazon RedshiftAmazon Web Services
 
Secrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsSecrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsTarik Essawi
 
Webinar: Introducing the MongoDB Connector for BI 2.0 with Tableau
Webinar: Introducing the MongoDB Connector for BI 2.0 with TableauWebinar: Introducing the MongoDB Connector for BI 2.0 with Tableau
Webinar: Introducing the MongoDB Connector for BI 2.0 with TableauMongoDB
 
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...Amazon Web Services
 
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 20197 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019Dave Stokes
 
How to Achieve Scale with MongoDB
How to Achieve Scale with MongoDBHow to Achieve Scale with MongoDB
How to Achieve Scale with MongoDBMongoDB
 
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...BI Brainz
 
Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...
Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...
Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...Precisely
 
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...Codemotion
 
Webminar - Novedades de MongoDB 3.2
Webminar - Novedades de MongoDB 3.2Webminar - Novedades de MongoDB 3.2
Webminar - Novedades de MongoDB 3.2Sam_Francis
 
Webinar : Nouveautés de MongoDB 3.2
Webinar : Nouveautés de MongoDB 3.2Webinar : Nouveautés de MongoDB 3.2
Webinar : Nouveautés de MongoDB 3.2MongoDB
 
ADV Slides: When and How Data Lakes Fit into a Modern Data Architecture
ADV Slides: When and How Data Lakes Fit into a Modern Data ArchitectureADV Slides: When and How Data Lakes Fit into a Modern Data Architecture
ADV Slides: When and How Data Lakes Fit into a Modern Data ArchitectureDATAVERSITY
 
Large Data Volume Salesforce experiences
Large Data Volume Salesforce experiencesLarge Data Volume Salesforce experiences
Large Data Volume Salesforce experiencesCidar Mendizabal
 
SQL Analytics for Search Engineers - Timothy Potter, Lucidworksngineers
SQL Analytics for Search Engineers - Timothy Potter, LucidworksngineersSQL Analytics for Search Engineers - Timothy Potter, Lucidworksngineers
SQL Analytics for Search Engineers - Timothy Potter, LucidworksngineersLucidworks
 
bigquery.pptx
bigquery.pptxbigquery.pptx
bigquery.pptxHarissh16
 

Semelhante a Boosting the Performance of your Rails Apps (20)

Enterprise Data World 2018 - Building Cloud Self-Service Analytical Solution
Enterprise Data World 2018 - Building Cloud Self-Service Analytical SolutionEnterprise Data World 2018 - Building Cloud Self-Service Analytical Solution
Enterprise Data World 2018 - Building Cloud Self-Service Analytical Solution
 
Designing, Building, and Maintaining Large Cubes using Lessons Learned
Designing, Building, and Maintaining Large Cubes using Lessons LearnedDesigning, Building, and Maintaining Large Cubes using Lessons Learned
Designing, Building, and Maintaining Large Cubes using Lessons Learned
 
Performance Optimization of Cloud Based Applications by Peter Smith, ACL
Performance Optimization of Cloud Based Applications by Peter Smith, ACLPerformance Optimization of Cloud Based Applications by Peter Smith, ACL
Performance Optimization of Cloud Based Applications by Peter Smith, ACL
 
Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...
Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...
Building and Deploying Large Scale SSRS using Lessons Learned from Customer D...
 
Getting Started with Amazon Redshift
Getting Started with Amazon RedshiftGetting Started with Amazon Redshift
Getting Started with Amazon Redshift
 
Secrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsSecrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archs
 
Webinar: Introducing the MongoDB Connector for BI 2.0 with Tableau
Webinar: Introducing the MongoDB Connector for BI 2.0 with TableauWebinar: Introducing the MongoDB Connector for BI 2.0 with Tableau
Webinar: Introducing the MongoDB Connector for BI 2.0 with Tableau
 
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
 
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 20197 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
7 Database Mistakes YOU Are Making -- Linuxfest Northwest 2019
 
How to Achieve Scale with MongoDB
How to Achieve Scale with MongoDBHow to Achieve Scale with MongoDB
How to Achieve Scale with MongoDB
 
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
Analysing and Troubleshooting Performance Issues in SAP BusinessObjects BI Re...
 
Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...
Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...
Engineering Machine Learning Data Pipelines Series: Streaming New Data as It ...
 
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...
 
Webminar - Novedades de MongoDB 3.2
Webminar - Novedades de MongoDB 3.2Webminar - Novedades de MongoDB 3.2
Webminar - Novedades de MongoDB 3.2
 
Webinar : Nouveautés de MongoDB 3.2
Webinar : Nouveautés de MongoDB 3.2Webinar : Nouveautés de MongoDB 3.2
Webinar : Nouveautés de MongoDB 3.2
 
ADV Slides: When and How Data Lakes Fit into a Modern Data Architecture
ADV Slides: When and How Data Lakes Fit into a Modern Data ArchitectureADV Slides: When and How Data Lakes Fit into a Modern Data Architecture
ADV Slides: When and How Data Lakes Fit into a Modern Data Architecture
 
Large Data Volume Salesforce experiences
Large Data Volume Salesforce experiencesLarge Data Volume Salesforce experiences
Large Data Volume Salesforce experiences
 
Breaking data
Breaking dataBreaking data
Breaking data
 
SQL Analytics for Search Engineers - Timothy Potter, Lucidworksngineers
SQL Analytics for Search Engineers - Timothy Potter, LucidworksngineersSQL Analytics for Search Engineers - Timothy Potter, Lucidworksngineers
SQL Analytics for Search Engineers - Timothy Potter, Lucidworksngineers
 
bigquery.pptx
bigquery.pptxbigquery.pptx
bigquery.pptx
 

Último

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 

Último (20)

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 

Boosting the Performance of your Rails Apps

  • 1. Boosting the performance of your Rails apps Jakarta RoR meetup, January 2013 Matt Kuklinski CTO, Gopher
  • 2. Where to start? • There are many ways to improve RoR app speed – some easy, some hard • Concentrate on methods that give you the biggest bang for the buck • This presentation shows a few different methods that should give you a good performance return for your time investment
  • 3. 1. DB Indexes • Your app will be constrained by database performance • Appropriate DB indexes can give you 100x performance gains on large tables • Not all rails developers realise how important this is • It’s easy to add indexes: class AddIndexToClientIndustry < ActiveRecord::Migration def change add_index :client_industries, :client_id end end
  • 4. Example with index CREATE INDEX addresses_addressable_id_addressable_type_idx ON addresses USING btree (addressable_id, addressable_type); t1 = Time.now c = Company.find(178389) a = c.addresses.first t2 = Time.now puts "---Operation took #{t2-t1} seconds---” Result with index: ---Operation took 0.012412 seconds---
  • 5. Now without the index DROP INDEX addresses_addressable_id_addressable_type_idx; t1 = Time.now c = Company.find(178389) a = c.addresses.first t2 = Time.now puts "---Operation took #{t2-t1} seconds---” Result without index: ---Operation took 0.378073 seconds--- 0.378073 / 0.012412 = 30.46 times slower without the index
  • 6. Index Tips • add indexes to all referential attributes, and other attributes that you regularly search or sort over if they contain a lot of distinct values • don't add too many indexes - each one increases the DB size and reduces the performance of insert and update queries
  • 7. 2. Minimise amount of DB queries • RoR makes it easy to program quickly • The downside: RoR makes it easy for the number of database queries per request to explode out of control
  • 8. Example: N+1 queries • Let’s say we have a Client model, and each Client can have one or more industries through ClientIndustry. • We want to show a list of clients, and their primary industries: <% @clients.each do |client| %> <tr> <td><%= client.id %></td> <td><%= client.business_name %></td> <td><%= client.industries.first.name %></td> </tr> <% end %>
  • 9. Be careful doing this: # app/controllers/clients_controller.rb def index @clients = Client.all end If you have 50 clients, then 51 DB queries will be run: Processing by ClientsController#index as HTML SELECT "clients".* FROM "clients" SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON "industries"."id" = "client_industries"."industry_id" WHERE "client_industries"."client_id" = 1 LIMIT 1 SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON "industries"."id" = "client_industries"."industry_id" WHERE "client_industries"."client_id" = 2 LIMIT 1 SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON "industries"."id" = "client_industries"."industry_id" WHERE "client_industries"."client_id" = 3 LIMIT 1 …
  • 10. Solution: Eager Loading # app/controllers/clients_controller.rb def index @clients = Client.includes(:industries).all end Now just 2 or 3 queries are performed instead of 51 Processing by ClientsController#index as HTML SELECT "clients".* FROM "clients" SELECT "client_industries".* FROM "client_industries" WHERE "client_industries"."client_id" IN (1, 2, 3) SELECT "industries".* FROM "industries" WHERE "industries"."id" IN (1, 5, 7, 8, 4)
  • 11. 3. Minimise memory usage • Only use gems that you actually need • Don’t load objects into memory unless you need to use them • When processing massive datasets, split them into batches
  • 12. Example: find_each An example using real data: Using find: t1 = Time.now Company.where(:country_id=>1).find do |c| puts "do something!" if ['Mattski Test'].include?(c.common_name) end t2 = Time.now puts "---Operation took #{t2-t1} seconds---” Result: 1 query, taking 46.65 seconds
  • 13. Now using find_each: t1 = Time.now Company.where(:country_id=>1).find_each do |c| puts "do something!" if ['Mattski Test'].include?(c.common_name) end t2 = Time.now puts "---Operation took #{t2-t1} seconds---" Result: >100 queries, taking 15.53 seconds in total (3x faster) Sometimes more queries is better!
  • 14. 3. Caching • Can make a huge difference to performance • Lots of options: – page caching – action caching – fragment caching – Memcached, Redis • Tip: get your data model correct first. Caching can hide structural problems
  • 15. What is Memcached? • Free & open source, high-performance, distributed memory object caching system. • Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. • www.memcached.org
  • 16. What is Redis? • Redis is an open source, advanced key- value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. • redis.io
  • 17. Pre-calculate summary data Example: • We have tables holding sales, sales_reps and teams • We need to provide live monthly and daily charts showing cumulative sales per sales_rep, per team, and for the company as a whole • We could produce a complicated ruby method that results in a query like this: select date(sales.created_at) as sale_date ,sales_reps.name ,sum(sales.amount) as daily_sales from sales join sales_reps on sales_reps.id = sales.sales_rep_id where sales.created_at > '2013-01-01’ group by 1,2; But that’s not very efficient if we have 300 sales reps and managers checking all their charts every few minutes. How can we speed it up?
  • 18. Solution: • Summarise the data in a sales_metrics table with good indexes and use observers and delayed_jobs to recalculate the sales data in near-real time. • Then we can do: sales_rep.sales_metrics.where(:date>'2013-01-01') To get an optimised query like this: select date ,sales_rep_id ,daily_sales from sales_metrics where sales_metrics.date >= '2013-01-01’ Now instead of 300 sales reps, imagine having 20,000 daytraders checking their daily stock portfolio charts… It has to be pre- calculated.
  • 19. 6. Make web requests fast • You have a limited number of processes available to serve web requests, so they need to be fast • Ideally, web processes should finish within milliseconds. 1-2 seconds is slow. 10+ seconds is very slow. • If you have slow web requests then your rails app wont be able to support many simultaneous users.
  • 20. Solution: use background processes • Use background processes such as delayed jobs for long-running jobs. This will free your web processes up to handle more requests. • What types of things? – sending email – running reports – processing images – obtaining information from third party APIs • Suggestion: use priorities so that important background processes get actioned before less important ones if there is a build up in jobs • Note: Rails 4 will support background processing out of the box
  • 21. 7. Monitor performance • Make sure to monitor the performance of your apps so that you can pinpoint which areas are running slowly. • New Relic is an excellent tool for monitor rails apps
  • 22. What does this tell me?
  • 23. - Response time is good. - There’s no request queuing. - I can scale back the web processes
  • 24. What does this tell me?
  • 25. - Performance is not that great - The database is being overworked - There may be some inefficient DB queries
  • 26. The slowness is almost entirely caused by the SearchController. This is a target for optimisation.
  • 27. 8. Use an in-memory DB • Databases are fast when the searching and sorting is done in memory • They slow down a lot when they have to go to disk
  • 28. Solution: keep your DB trim • Try to limit the size of the DB so that it fits entirely within memory • Move non-essential information out of the main DB into a secondary DB or elsewhere (i.e. audit logs, inactive accounts, old email logs, etc.) • Consider using non-relational databases if you have massive storage requirements
  • 29. 9. Manage your load • load balancing – essential for public web apps – Cloud hosting providers help to manage this for you • database replication – run heavy reports on a replicated database so that the performance of the main database isn’t affected • database sharding
  • 30. More performance tips • Use a content distribution network for static files. – AWS CloudFront, etc. • Make the UI asynchronous – use AJAX lazy loading for anything that takes more than 1-2 seconds to load • Use a Service Oriented Architecture, so that some actions can be done in parallel on a different hosting stack
  • 31. Thanks for your interest! • Contact – www.linkedin.com/in/matthewkuklinski – @mattkuklinski – www.gopher.co.id – www.gopher.co.nz