- Measure. If you have a serious project a cost of NewRelic account is negligible but insights it provides are very valuable. Make sure you measure request queue time, when you have large variance in request processing time (some are slow, some are fast) you may want to switch from Nginx to Haproxy as a front-end.
- Cache output. With large JSON/HTML responses you'll move from 300ms to sub-10ms. Use Russian doll caching
- Use conditional GET
- Use proper DB indexes. If they aren't enough, denormalize in RDBMS or use Shopify's
identity_cache. There are gems that suggest what indexes you are missing. Remember that indexes are useful when searching using a prefix of indexed columns, so if you have an index on(name, created_at)it will be used when searching or sorting byname. Read "Use the index Luke" it contains pretty much everything you need to know about them. - If you are managing a database yourself and it's PostgreSQL, read the configuration chapter in the docs and change the settings accordingly. Defaults aren't optimized for performance.
- DB optimization is a huge topic. If some query is unusually slow, make sure execution plan is correct. PostgreSQL optimizer sometimes messes up and uses hash join-table scan instead of nested loop. Increase stats granularity or force it to use nested loop. Read http://a.co/2GF5Sg8 if you want to know more.
- Avoid N+1, make sure to include related records in queries. In rare cases it's better to use N+1 with Russian doll caching but generally avoiding N+1 is better. There are gems to detect N+1.
- Play with number of processes and threads per process in your app server. Multi-threading helps with I/O bound tasks (querying a slow database, calling remote servers).
- Enable out-of-bound GC in your app server, so that GC won't happen during requests. It has minimal impact but still.
- Off-load long-running tasks (e.g. resizing images and calling external services) to background workers. Use Sidekiq, it's amazing.
- Don't bother with EventMachine, it sucks.
- Make a choice on what to do with JS files. Either use CDN to serve third-party libraries individually or bundle them in a separate file.
- Serve static assets from CDN.
https://www.reddit.com/r/rails/comments/5x0wd2/has_anybody_purchased_the_complete_guide_to_rails/
沒有留言:
張貼留言