Cannon Moyer
Cannon Moyer

Reputation: 3164

Rails Server Extremely Slow in Development

I've got a rails 5 application that is very slow after I boot the server or change a .rb file. Below is my cli output after I booted the server with rails server and loaded a simple page. Also, if I change a model, controller or helper, the server takes about the same time to respond to the next request. After the request is loaded, the pages load normally but that first request after each change can take 30 seconds which adds up quite fast. I have tried setting the following configs in `development (similar questions suggested these changes). Also, I have this development server running on a digital ocean droplet with 4GB of memory.

config.assets.debug = false
config.assets.digest = false

My output:

rails server
=> Booting Puma
=> Rails 5.2.2 application starting in development
=> Run rails server -h for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.3-p105), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/treadmill-parts-model-select" for 98.251.169.224 at 2019-04-03 
11:51:29 +0000
Cannot render console from 98.251.169.224! Allowed networks: 127.0.0.1, ::1, 
127.0.0.0/127.255.255.255
(2.1ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, 
',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  
@@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
↳ /usr/local/rvm/gems/ruby-2.5.3/gems/activerecord- 
5.2.2/lib/active_record/log_subscriber.rb:98
(2.4ms)  SELECT 'schema_migrations'.'version' FROM 'schema_migrations' ORDER 
BY 'schema_migrations'.'version' ASC
↳ /usr/local/rvm/gems/ruby-2.5.3/gems/activerecord- 
5.2.2/lib/active_record/log_subscriber.rb:98
Processing by SearchController#treadmill_model_search as HTML
  Rendering search/treadmill_model_search.html.erb within layouts/application
  Product Load (113.8ms)  SELECT DISTINCT 'products'.'brand' FROM 'products' WHERE 'products'.'product_type' = 'Treadmill' ORDER BY 'products'.'brand' ASC
  ↳ app/views/search/treadmill_model_search.html.erb:22
  Rendered search/treadmill_model_search.html.erb within layouts/application (167.6ms)
  Rendered layouts/_header.html.erb (5.8ms)
  Rendered layouts/_flash_messages.html.erb (1.1ms)
  Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 18746ms (Views: 18565.0ms | ActiveRecord: 124.7ms)`

Upvotes: 2

Views: 4185

Answers (1)

DRSisco
DRSisco

Reputation: 31

Looks like every time your rails server detects a page change it checks to see if their is a migration pending, which will take some time.

#config/environments/development.rb

  config.active_record.migration_error = false

could help with that check.

In the development environment your application's code is reloaded on every request. This slows down response time but is perfect for development since you don't have to restart the web server when you make code changes.

As with larger projects you will want to stop rails from pre-loading all the code which isn't needed especially if you are only working on a portion of your project anyways. Also in the environment

#config/environments/development.rb

  # Do not eager load code on boot.
  config.eager_load = false

You can read more on these environment config settings here

Though the 4GB of memory seems like a red flag if you aren't working on a massive project... might need to look into your initializes and what all your project is loading up on boot.

Upvotes: 2

Related Questions