scaling rails apps - valentin mihov
TRANSCRIPT
Скалиране на Rails Приложения
Валентин Михов2013
За мен
● 5 години опит със Ruby on Rails
● скалиране до милиони заявки на ден
● big data processing
Може ли Rails да скалира?
Как работи Ruby on Rails?
Browser
WEBrick
Makes request
Rails App
Parses the request Writes response
Sends response
Избор на правилен app server
One machine concurrent setup
Browser
App Server
Makes request
Rails App
Parses the request Writes response
Sends response
App ServerApp Server
App ServerApp Server
Pool of workers
Many machines concurrent setupBrowser
Makes request Sends response
App ServerApp Server
App ServerApp Server
App Server
Load Balancer
App ServerApp Server
App ServerApp Server
App Server
App ServerApp Server
App ServerApp Server
App Server
Balances between machines
Instance/VM/Box Instance/VM/Box Instance/VM/Box
Slow clients
Връзката е бавна тъй като канала е Internet
Може да има network latency
Browser
App ServerApp Server
App ServerApp Server
App Server
Load Balancer
App ServerApp Server
App ServerApp Server
App Server
App ServerApp Server
App ServerApp Server
App Server
Използвайте "буфери" за трафикаBrowser
App ServerApp Server
App ServerApp Server
App Server
ELB/haproxy/nginx
App ServerApp Server
App ServerApp Server
App Server
App ServerApp Server
App ServerApp Server
App Server
nginxnginx nginx
Изберете внимателно Ruby VM
● GC може да заема *МНОГО* време
● green threads vs true concurrency vs GIL
● много зависи от приложението
● ruby 1.8 не се поддържа официално
● GC tweaks○ RUBY_GC_MALLOC_LIMIT
○ RUBY_HEAP_SLOTS_INCREMENT
○ RUBY_HEAP_MIN_SLOTS
Ruby Enterprise Edition
● Може бе най-добрата 1.8 имплементация
● Tweakable GC params
● copy-on-write friendly GC
● Използва TCMalloc - по-бърз malloc
JRuby
● Изграден върху JVM
● Използва JVM GC - много бърз
● Не поддържа native extensions
● Unicorn не се поддържа
● Трябва да се ползва TomCat, JBoss,
GlassFish, etc.
● Няма GIL + използва native threads
● GC-то може да се тунингова
● Не всички gem-ове се поддържат
● Странна поддръжка на unicode
● Може би най-добрия избор за нов проект
MRI 2.0
Unicorn::OobGC
Time
Request 1 Request 2 Request 3GC GC
Time
Request 1 Request 2 Request 3GC GC
Monitoring
NewRelic - БЕЗЦЕННО!
Полезни проекти/услуги
● Unicorn - app server
● nginx - web server
● haproxy - load balancer
● AWS - real-time scaling infrastructure
● NewRelic - kick ass monitoring
● REE - optimized ruby VM
● memcached - сесии, кеширане
● Първо измервайте, после оптимизирайте
● Работещ продукт - най-важната
оптимизация
● В повечето случай bottleneck-а е data IO
● Rails скалира не по-лошо от всеки един
друг framework
● Всичко e computer science
И не забравяйте!
Въпроси?