SlideShare uma empresa Scribd logo
1 de 52
Baixar para ler offline
Porting Rails apps for
  High Availability
      Systems
   Marcelo Correia Pinheiro
          @salizzar
$ whoami
Programmed with ASP, PHP, C#, Java, Python,
Ruby, etc etc

Twitter: @salizzar

http://salizzar.net/ (always under
construction)

Locaweb PaaS/IaaS Engineering Team
(Domain Registration)
Topics
Common Architecture

Application Modularity

Useful Gems

Database Replication

Session Management

App Distribution and Deployment

Configuration Management

Load Balancing

Conclusion
Common
        Architecture
One machine running HTTP Server, App and Database
(sometimes in a dedicated server)

Production config files in application GIT repo (all of us
use git, alright?)

Deploy via Capistrano / Vlad / * ?

Sufficient for small apps with no perspective to grow
absurdly or have monitored growing

Grants HA? Hmmm... what do you do if your database
corrupts journaling? (MongoDB REAL CASE)
Common
      Architecture
Think about it and the possibility of a
nightmare due for a bad db query, zombie
processes and unexpected errors that will
frozen your app

What to do? Look at your app with a
Distributed System perspective
Application
        Modularity

Rails apps, in most cases, can be splitted to two
or more small gems or apps (web services,
checkout systems, asynch processing, etc)
Application
         Modularity
You write good code?

If yes, creating gems with it is EASY.

  How to detect: first DRY while splitting a app

  How to create: $ bundle gem <asdfg>

  How to learn more: Creating Your Own Gem in
  www.rubygems.org

If not, refactoring!
Application
        Modularity
Breaking your Rails app in smaller apps:

  increase future improvements

  first step to make distribution via packaging
  more easy

  consequently, more scalable

Look at ActiveController::Metal / sinatra /
rack-api / goliath / cramp
Application
        Modularity

Be careful:

  using multiple gems

  coupling Rails features inside your models
  (Rails.logger)
Useful Gems

Hypermedia

Asynchronous Processing (resque)

Webservers

Monitoring
Useful Gems

JSON for the win

Hypermedia-based REST API’s

  ROAR: https://github.com/apotonick/roar

  RestModel: https://github.com/rodrigues/
  rest_model
Useful Gems
# -*- encoding: UTF-8 -*-

class Domain
  attr_accessor :id, :creation_date, :expiration_date
end

module DomainRepresenter
  include Roar::Representer::JSON,
          Roar::Representer::Feature::Hypermedia

  property :id, :creation_date, :expiration_date
end

request = RestClient.get 'http://an.app/domains/example.com'
domain = Domain.new.extend DomainRepresenter
domain.from_json request.body
Useful Gems

# -*- encoding: UTF-8 -*-

class Domain < RestModel
  property :id,               type:   String
  property :creation_date,    type:   Date
  property :expiration_date,  type:   Date
  property :provisioning_id,  type:   Integer
end

request = RestClient.get 'http://an.app/domains/example.com'
domain = Domain.from_source(request.body).first
Useful Gems
Asynchronous Processing

 Resque

   resque-scheduler

   resque-retry

   resque-lock

   resque-logger
Useful Gems

resque-scheduler:

  https://github.com/bvandenbos/resque-
  scheduler

  cron-based Resque extension
Useful Gems
# resque_scheduler.yml

appointment_notification_enqueuer:
  cron:   "0 4 * * *"
  class:  AppointmentNotificationEnqueuer
  queue:  appointment_notification_enqueuer
  args:
    email:  true
    sms:    true de cron
   Incluir config
  description: Notify today appointments to customers

free_slot_monitor:
  cron:   "0/15 * * * *"
  class:  FreeSlotMonitor
  queue:  free_slot_monitor
  description:  Check for free slots in agenda
Useful Gems
# -*- encoding: UTF-8 -*-

class AppointmentNotificationEnqueuer
  @queue = :appointment_notification_enqueuer

  def self.perform(args)
    appointments = Appointment.today
    factory = NotificationFactory.new args
    appointments.each do |appointment|
      factory.enqueue appointment.id
    end
  end
end
Useful Gems
resque-retry:

  https://github.com/lantins/resque-retry

  redis backed

  retry count times until reach a limit

  retry on all or specific exceptions

  retry functionality on Resque Web Server
Useful Gems
# -*- encoding: UTF-8 -*-

class EmailNotificationWorker
  extend Resque::Plugins::Retry

  @queue = :email_notification_enqueuer
  @retry_limit = 4
  @retry_delay = 300

  def self.perform(args)
    appointment = Appointment.find id: args['id']

    mailer = MailNotification.new
    mailer.notify_appointment appointment
  end
end
Useful Gems
Useful Gems

resque-lock:

  https://github.com/defunkt/resque-lock

  Grants only one instance of a job running in
  a time
Useful Gems
# -*- encoding: UTF-8 -*-

class SmsNotificationWorker
  extend Resque::Plugins::Lock

  def self.perform(args)
    appointment = Appointment.find id: args['id']

    sender = SmsNotification.new
    sender.deliver_appointment appointment
  end
end
Useful Gems

resque-logger:

  https://github.com/salizzar/resque-logger

  Provides a logger for each Resque worker
  based on queue name

  Log Driven Development, please!
Useful Gems
# config/initializers/resque.rb

log_path = Rails.root.join 'log'

config = {
  folder:     log_path,
  class_name: Logger,
  class_args: [ 'daily', 1.gigabyte ],
  level:      Logger::INFO,
  formatter:  Logger::Formatter.new,
}

Resque.logger = config
Useful Gems
# -*- encoding: UTF-8 -*-

class EmailNotificationWorker
  extend Resque::Plugins::Retry, Resque::Plugins::Logger

  @queue = :email_notification_enqueuer
  @retry_limit = 4
  @retry_delay = 300

  def self.perform(args)
    appointment = Appointment.find id: args['id']

    logger.info(“trying to notify #{appointment.id}”)

    mailer = MailNotification.new
    mailer.notify_appointment appointment
  end
end
Useful Gems
Web Servers:

   Thin

      https://github.com/macournoyer/thin

      EventMachine powered

      Killer combo with Nginx

   Unicorn

      http://unicorn.bogomips.org

      Pre-fork based

      Great with low-latency network
Useful Gems
Monitoring

  God

     https://github.com/mojombo/god

     Several memory leaks at beginning, very stable today

     Largely used to manage Resque workers and Thin clusters

  Bluepill

     https://github.com/arya/bluepill

     More simple than God, less features

     Some issues with signal handling
Useful Gems

If memory usage is a problem, use Monit.

  http://mmonit.com/monit

  VERY stable

  HIGH expansible

  EASY configuration
Database Replication
 Relational Database Replication is not trivial

 Be a friend of a sysadm

 CAP Theorem: YOU NEED TO KNOW

 No magic: look to your architecture

 Traditional replication strategies:

   master / slave for consistency

   master for write / slave for read
Database Replication

 My experience:

   Relational Databases: no choice

   MongoDB: replica set

   Redis: master / slave
Database Replication
 For MongoDB:

   MongoMapper

     https://github.com/jnunemaker/mongomapper

     Largely used, stable

   Mongoid

     https://github.com/mongoid/mongoid

     ActiveRecord-like
Database Replication
 brotip #1342: check if your MongoDB client
 detects when master server goes down
 automagically (MongoMapper NOT does it)

   Suggested keynote: MongoDB em Produção,
   dá pra confiar?

   http://www.slideshare.net/deferraz/
   mongodb-em-producao
Session Management
 Given a problem, use the right tool PLEASE

 memcached

   http://memcached.org

   distributed memory object caching system

   key/value storage

   stores arbitrary data

   can be clustered
Session Management

 repcached

  http://repcached.lab.klab.org

  patch inside memcached to add data
  replication

  offers session sharing between memcached
  servers
Session Management
 available gems for memcache

    memcache-client

        https://github.com/mperham/memcache-client

        First Ruby client, written by Mike Perham

    memcached

        https://github.com/evan/memcached

        Ruby SWIG wrapper for libmemcached C library

    dalli

        https://github.com/mperham/dalli

        Major refactoring from memcache-client
App Distribution and
    Deployment
 Capistrano? Hnmm... talk with a sysadm about
 it. It works but lacks security severely (SSH, GCC
 in production is a invite for attack)

 Sysadms knows process monitoring tools,
 software distribution and server configuration
 better than us

 Be a friend of a sysadm

 Or LEARN A LOT to be one :)
App Distribution and
    Deployment
 Build self-packaged apps

 $ man dh_make (for Debian based distros)

   Hard or too boring? Maybe, but effort compensates

   $ dh_make --native --simple --indep --
   packagename <yourapp> --copyright blank

   $ debuild

   # dpkg -i <yourapp>_<version>_<arch>.deb
App Distribution and
    Deployment
 Generating a distro-based package with your app
 grants fast update/rollback to sysadms

   # apt-get update && apt-get install <yourapp>

   # dpkg -i <your_app_package>

 If you want to automate builds:

   Setup a repository server

   Use Bricklayer and be happy
Configuration
     Management
Keeping production configs in your app git
repo is not always a good idea

Getting config files over control is VERY
important to avoid a messy environment

Standardization is king in server configuration
Configuration
      Management

At this time, three choices:

  Puppet

  Chef

  cfengine
Configuration
    Management
Puppet

 https://github.com/puppetlabs/puppet

 Written in Ruby

 Large community adoption

 Great number of recipes

 Works well
Configuration
       Management
Chef

 https://github.com/opscode/chef

 Written in Ruby

 Created as a Puppet alternative in order to solve
 some problems with it

 Large community adoption

 Works good
Configuration
     Management
cfengine

  http://cfengine.com

  Written in C

  Largely adopted by sysadms

  HARD to learn

  F****** ROBUST (a single master node can
  manage 4K servers)
Load Balancing
WTF is Load Balancing?

  A network methodology to distribute workload across
  multiple computers in a transparent way for users

  Can be proved by /hard|soft/ware

Alternatives:

  LVS with keepalived

  HAProxy with heartbeat
Load Balancing
LVS (Linux Virtual Server)

   http://www.linuxvirtualserver.org

   Robust, high scalable solution

   Need to share HTTP sessions? It’s the choice

   Built-in Linux Kernel module since 2.4.28*, runs in layer-4 (transport
   layer)

keepalived

   http://www.keepalived.org

   Largely used with LVS by sysadms

   Uses Virtual Router Redundancy Protocol (vrrp) - RFC
Load Balancing
HAProxy

   http://haproxy.1wt.eu

   Generic solution

   Works if session sharing between servers is not required

   Runs in layer-7 (application layer), computationally more expensive

heartbeat

   http://www.linux-ha.org/wiki/Heartbeat

   Based on message exchange between clients in a cluster to detect
   failures
Conclusion
Porting Rails apps to a HA environment is a not trivial challenge, but
nothing insanely hard

   Forces you to think in application architecture, design, distribution,
   modularity

      brotip #915435: do it before coding

   Restricts you in order to make server correctly managed

   Implies you to be prudent on a deploy

   “it sucks at all” = bad excuse, my friend

App tunning later, without fear :)

In fact, in HA deploys are more faster
Questions?



No fear, answers are free as beer :)
Thank you!

Mais conteúdo relacionado

Mais procurados

Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)Ben Hall
 
Running Docker in Development & Production (DevSum 2015)
Running Docker in Development & Production (DevSum 2015)Running Docker in Development & Production (DevSum 2015)
Running Docker in Development & Production (DevSum 2015)Ben Hall
 
Migrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain PointsMigrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain PointsSteven Evatt
 
Real World Lessons on the Pain Points of Node.js Applications
Real World Lessons on the Pain Points of Node.js ApplicationsReal World Lessons on the Pain Points of Node.js Applications
Real World Lessons on the Pain Points of Node.js ApplicationsBen Hall
 
From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012Carlos Sanchez
 
Deploying Symfony | symfony.cat
Deploying Symfony | symfony.catDeploying Symfony | symfony.cat
Deploying Symfony | symfony.catPablo Godel
 
Challenges when building high profile editorial sites
Challenges when building high profile editorial sitesChallenges when building high profile editorial sites
Challenges when building high profile editorial sitesYann Malet
 
Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...
Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...
Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...Carlos Sanchez
 
Terraform AWS modules and some best practices - September 2019
Terraform AWS modules and some best practices - September 2019Terraform AWS modules and some best practices - September 2019
Terraform AWS modules and some best practices - September 2019Anton Babenko
 
Configuring Django projects for multiple environments
Configuring Django projects for multiple environmentsConfiguring Django projects for multiple environments
Configuring Django projects for multiple environmentsApptension
 
The How and Why of Windows containers
The How and Why of Windows containersThe How and Why of Windows containers
The How and Why of Windows containersBen Hall
 
A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)Flowdock
 
Lessons from running potentially malicious code inside Docker containers
Lessons from running potentially malicious code inside Docker containersLessons from running potentially malicious code inside Docker containers
Lessons from running potentially malicious code inside Docker containersBen Hall
 
Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Carlos Sanchez
 
Deploying Windows Containers on Windows Server 2016
Deploying Windows Containers on Windows Server 2016Deploying Windows Containers on Windows Server 2016
Deploying Windows Containers on Windows Server 2016Ben Hall
 
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero ClicksHow to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero ClicksCarlos Sanchez
 
App development with quasar (pdf)
App development with quasar (pdf)App development with quasar (pdf)
App development with quasar (pdf)wonyong hwang
 

Mais procurados (20)

Debugging on rails
Debugging on railsDebugging on rails
Debugging on rails
 
Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)Running Docker in Development & Production (#ndcoslo 2015)
Running Docker in Development & Production (#ndcoslo 2015)
 
Running Docker in Development & Production (DevSum 2015)
Running Docker in Development & Production (DevSum 2015)Running Docker in Development & Production (DevSum 2015)
Running Docker in Development & Production (DevSum 2015)
 
Php on Windows
Php on WindowsPhp on Windows
Php on Windows
 
Migrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain PointsMigrating PriceChirp to Rails 3.0: The Pain Points
Migrating PriceChirp to Rails 3.0: The Pain Points
 
Real World Lessons on the Pain Points of Node.js Applications
Real World Lessons on the Pain Points of Node.js ApplicationsReal World Lessons on the Pain Points of Node.js Applications
Real World Lessons on the Pain Points of Node.js Applications
 
From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012From Dev to DevOps - FOSDEM 2012
From Dev to DevOps - FOSDEM 2012
 
Deploying Symfony | symfony.cat
Deploying Symfony | symfony.catDeploying Symfony | symfony.cat
Deploying Symfony | symfony.cat
 
ABCs of docker
ABCs of dockerABCs of docker
ABCs of docker
 
Challenges when building high profile editorial sites
Challenges when building high profile editorial sitesChallenges when building high profile editorial sites
Challenges when building high profile editorial sites
 
Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...
Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...
Infrastructure testing with Jenkins, Puppet and Vagrant - Agile Testing Days ...
 
Terraform AWS modules and some best practices - September 2019
Terraform AWS modules and some best practices - September 2019Terraform AWS modules and some best practices - September 2019
Terraform AWS modules and some best practices - September 2019
 
Configuring Django projects for multiple environments
Configuring Django projects for multiple environmentsConfiguring Django projects for multiple environments
Configuring Django projects for multiple environments
 
The How and Why of Windows containers
The How and Why of Windows containersThe How and Why of Windows containers
The How and Why of Windows containers
 
A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)
 
Lessons from running potentially malicious code inside Docker containers
Lessons from running potentially malicious code inside Docker containersLessons from running potentially malicious code inside Docker containers
Lessons from running potentially malicious code inside Docker containers
 
Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012Puppet for Java developers - JavaZone NO 2012
Puppet for Java developers - JavaZone NO 2012
 
Deploying Windows Containers on Windows Server 2016
Deploying Windows Containers on Windows Server 2016Deploying Windows Containers on Windows Server 2016
Deploying Windows Containers on Windows Server 2016
 
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero ClicksHow to Develop Puppet Modules: From Source to the Forge With Zero Clicks
How to Develop Puppet Modules: From Source to the Forge With Zero Clicks
 
App development with quasar (pdf)
App development with quasar (pdf)App development with quasar (pdf)
App development with quasar (pdf)
 

Semelhante a Porting Rails Apps to High Availability Systems

Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiJérémy Derussé
 
Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Matthew McCullough
 
Meetup Performance
Meetup PerformanceMeetup Performance
Meetup PerformanceGreg Whalin
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发shaokun
 
Pyramid Deployment and Maintenance
Pyramid Deployment and MaintenancePyramid Deployment and Maintenance
Pyramid Deployment and MaintenanceJazkarta, Inc.
 
Profiling PHP with Xdebug / Webgrind
Profiling PHP with Xdebug / WebgrindProfiling PHP with Xdebug / Webgrind
Profiling PHP with Xdebug / WebgrindSam Keen
 
DevOPS training - Day 2/2
DevOPS training - Day 2/2DevOPS training - Day 2/2
DevOPS training - Day 2/2Vincent Mercier
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular applicationmirrec
 
Why we choose Symfony2
Why we choose Symfony2Why we choose Symfony2
Why we choose Symfony2Merixstudio
 
Cannibalising The Google App Engine
Cannibalising The  Google  App  EngineCannibalising The  Google  App  Engine
Cannibalising The Google App Enginecatherinewall
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvarsSam Marley-Jarrett
 
The "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/OpsThe "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/OpsErik Osterman
 
Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...
Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...
Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...Patrick Chanezon
 
Kubernetes - training micro-dragons without getting burnt
Kubernetes -  training micro-dragons without getting burntKubernetes -  training micro-dragons without getting burnt
Kubernetes - training micro-dragons without getting burntAmir Moghimi
 
Harmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetHarmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetAchieve Internet
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.jsChris Cowan
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with RailsPaul Gallagher
 
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with PythonBrian Lyttle
 

Semelhante a Porting Rails Apps to High Availability Systems (20)

Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2Google App Engine for Java v0.0.2
Google App Engine for Java v0.0.2
 
Meetup Performance
Meetup PerformanceMeetup Performance
Meetup Performance
 
Meetup Performance
Meetup PerformanceMeetup Performance
Meetup Performance
 
Scaling PHP apps
Scaling PHP appsScaling PHP apps
Scaling PHP apps
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发
 
Pyramid Deployment and Maintenance
Pyramid Deployment and MaintenancePyramid Deployment and Maintenance
Pyramid Deployment and Maintenance
 
Profiling PHP with Xdebug / Webgrind
Profiling PHP with Xdebug / WebgrindProfiling PHP with Xdebug / Webgrind
Profiling PHP with Xdebug / Webgrind
 
DevOPS training - Day 2/2
DevOPS training - Day 2/2DevOPS training - Day 2/2
DevOPS training - Day 2/2
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Why we choose Symfony2
Why we choose Symfony2Why we choose Symfony2
Why we choose Symfony2
 
Cannibalising The Google App Engine
Cannibalising The  Google  App  EngineCannibalising The  Google  App  Engine
Cannibalising The Google App Engine
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvars
 
The "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/OpsThe "Holy Grail" of Dev/Ops
The "Holy Grail" of Dev/Ops
 
Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...
Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...
Docker Azure Friday OSS March 2017 - Developing and deploying Java & Linux on...
 
Kubernetes - training micro-dragons without getting burnt
Kubernetes -  training micro-dragons without getting burntKubernetes -  training micro-dragons without getting burnt
Kubernetes - training micro-dragons without getting burnt
 
Harmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetHarmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and Puppet
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with Rails
 
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
 

Último

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
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
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
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
 
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
 
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
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
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
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
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
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
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
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
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
 
"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
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
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
 

Último (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
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.
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
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
 
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
 
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
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
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
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
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
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
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
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
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
 
"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
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
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
 

Porting Rails Apps to High Availability Systems

  • 1. Porting Rails apps for High Availability Systems Marcelo Correia Pinheiro @salizzar
  • 2. $ whoami Programmed with ASP, PHP, C#, Java, Python, Ruby, etc etc Twitter: @salizzar http://salizzar.net/ (always under construction) Locaweb PaaS/IaaS Engineering Team (Domain Registration)
  • 3. Topics Common Architecture Application Modularity Useful Gems Database Replication Session Management App Distribution and Deployment Configuration Management Load Balancing Conclusion
  • 4.
  • 5. Common Architecture One machine running HTTP Server, App and Database (sometimes in a dedicated server) Production config files in application GIT repo (all of us use git, alright?) Deploy via Capistrano / Vlad / * ? Sufficient for small apps with no perspective to grow absurdly or have monitored growing Grants HA? Hmmm... what do you do if your database corrupts journaling? (MongoDB REAL CASE)
  • 6.
  • 7. Common Architecture Think about it and the possibility of a nightmare due for a bad db query, zombie processes and unexpected errors that will frozen your app What to do? Look at your app with a Distributed System perspective
  • 8. Application Modularity Rails apps, in most cases, can be splitted to two or more small gems or apps (web services, checkout systems, asynch processing, etc)
  • 9. Application Modularity You write good code? If yes, creating gems with it is EASY. How to detect: first DRY while splitting a app How to create: $ bundle gem <asdfg> How to learn more: Creating Your Own Gem in www.rubygems.org If not, refactoring!
  • 10. Application Modularity Breaking your Rails app in smaller apps: increase future improvements first step to make distribution via packaging more easy consequently, more scalable Look at ActiveController::Metal / sinatra / rack-api / goliath / cramp
  • 11. Application Modularity Be careful: using multiple gems coupling Rails features inside your models (Rails.logger)
  • 12. Useful Gems Hypermedia Asynchronous Processing (resque) Webservers Monitoring
  • 13. Useful Gems JSON for the win Hypermedia-based REST API’s ROAR: https://github.com/apotonick/roar RestModel: https://github.com/rodrigues/ rest_model
  • 14. Useful Gems # -*- encoding: UTF-8 -*- class Domain   attr_accessor :id, :creation_date, :expiration_date end module DomainRepresenter   include Roar::Representer::JSON,           Roar::Representer::Feature::Hypermedia   property :id, :creation_date, :expiration_date end request = RestClient.get 'http://an.app/domains/example.com' domain = Domain.new.extend DomainRepresenter domain.from_json request.body
  • 15. Useful Gems # -*- encoding: UTF-8 -*- class Domain < RestModel   property :id,               type: String   property :creation_date,    type: Date   property :expiration_date,  type: Date   property :provisioning_id,  type: Integer end request = RestClient.get 'http://an.app/domains/example.com' domain = Domain.from_source(request.body).first
  • 16. Useful Gems Asynchronous Processing Resque resque-scheduler resque-retry resque-lock resque-logger
  • 17. Useful Gems resque-scheduler: https://github.com/bvandenbos/resque- scheduler cron-based Resque extension
  • 18. Useful Gems # resque_scheduler.yml appointment_notification_enqueuer:   cron:   "0 4 * * *"   class:  AppointmentNotificationEnqueuer   queue:  appointment_notification_enqueuer   args:     email:  true     sms:    true de cron Incluir config   description: Notify today appointments to customers free_slot_monitor:   cron:   "0/15 * * * *"   class:  FreeSlotMonitor   queue:  free_slot_monitor   description:  Check for free slots in agenda
  • 19. Useful Gems # -*- encoding: UTF-8 -*- class AppointmentNotificationEnqueuer   @queue = :appointment_notification_enqueuer   def self.perform(args)     appointments = Appointment.today     factory = NotificationFactory.new args     appointments.each do |appointment|       factory.enqueue appointment.id     end   end end
  • 20. Useful Gems resque-retry: https://github.com/lantins/resque-retry redis backed retry count times until reach a limit retry on all or specific exceptions retry functionality on Resque Web Server
  • 21. Useful Gems # -*- encoding: UTF-8 -*- class EmailNotificationWorker   extend Resque::Plugins::Retry   @queue = :email_notification_enqueuer   @retry_limit = 4   @retry_delay = 300   def self.perform(args)     appointment = Appointment.find id: args['id']     mailer = MailNotification.new     mailer.notify_appointment appointment   end end
  • 23. Useful Gems resque-lock: https://github.com/defunkt/resque-lock Grants only one instance of a job running in a time
  • 24. Useful Gems # -*- encoding: UTF-8 -*- class SmsNotificationWorker   extend Resque::Plugins::Lock   def self.perform(args)     appointment = Appointment.find id: args['id']     sender = SmsNotification.new     sender.deliver_appointment appointment   end end
  • 25. Useful Gems resque-logger: https://github.com/salizzar/resque-logger Provides a logger for each Resque worker based on queue name Log Driven Development, please!
  • 26. Useful Gems # config/initializers/resque.rb log_path = Rails.root.join 'log' config = {   folder:     log_path,   class_name: Logger,   class_args: [ 'daily', 1.gigabyte ],   level:      Logger::INFO,   formatter:  Logger::Formatter.new, } Resque.logger = config
  • 27. Useful Gems # -*- encoding: UTF-8 -*- class EmailNotificationWorker   extend Resque::Plugins::Retry, Resque::Plugins::Logger   @queue = :email_notification_enqueuer   @retry_limit = 4   @retry_delay = 300   def self.perform(args)     appointment = Appointment.find id: args['id']     logger.info(“trying to notify #{appointment.id}”)     mailer = MailNotification.new     mailer.notify_appointment appointment   end end
  • 28. Useful Gems Web Servers: Thin https://github.com/macournoyer/thin EventMachine powered Killer combo with Nginx Unicorn http://unicorn.bogomips.org Pre-fork based Great with low-latency network
  • 29. Useful Gems Monitoring God https://github.com/mojombo/god Several memory leaks at beginning, very stable today Largely used to manage Resque workers and Thin clusters Bluepill https://github.com/arya/bluepill More simple than God, less features Some issues with signal handling
  • 30. Useful Gems If memory usage is a problem, use Monit. http://mmonit.com/monit VERY stable HIGH expansible EASY configuration
  • 31. Database Replication Relational Database Replication is not trivial Be a friend of a sysadm CAP Theorem: YOU NEED TO KNOW No magic: look to your architecture Traditional replication strategies: master / slave for consistency master for write / slave for read
  • 32. Database Replication My experience: Relational Databases: no choice MongoDB: replica set Redis: master / slave
  • 33. Database Replication For MongoDB: MongoMapper https://github.com/jnunemaker/mongomapper Largely used, stable Mongoid https://github.com/mongoid/mongoid ActiveRecord-like
  • 34. Database Replication brotip #1342: check if your MongoDB client detects when master server goes down automagically (MongoMapper NOT does it) Suggested keynote: MongoDB em Produção, dá pra confiar? http://www.slideshare.net/deferraz/ mongodb-em-producao
  • 35. Session Management Given a problem, use the right tool PLEASE memcached http://memcached.org distributed memory object caching system key/value storage stores arbitrary data can be clustered
  • 36. Session Management repcached http://repcached.lab.klab.org patch inside memcached to add data replication offers session sharing between memcached servers
  • 37. Session Management available gems for memcache memcache-client https://github.com/mperham/memcache-client First Ruby client, written by Mike Perham memcached https://github.com/evan/memcached Ruby SWIG wrapper for libmemcached C library dalli https://github.com/mperham/dalli Major refactoring from memcache-client
  • 38. App Distribution and Deployment Capistrano? Hnmm... talk with a sysadm about it. It works but lacks security severely (SSH, GCC in production is a invite for attack) Sysadms knows process monitoring tools, software distribution and server configuration better than us Be a friend of a sysadm Or LEARN A LOT to be one :)
  • 39. App Distribution and Deployment Build self-packaged apps $ man dh_make (for Debian based distros) Hard or too boring? Maybe, but effort compensates $ dh_make --native --simple --indep -- packagename <yourapp> --copyright blank $ debuild # dpkg -i <yourapp>_<version>_<arch>.deb
  • 40. App Distribution and Deployment Generating a distro-based package with your app grants fast update/rollback to sysadms # apt-get update && apt-get install <yourapp> # dpkg -i <your_app_package> If you want to automate builds: Setup a repository server Use Bricklayer and be happy
  • 41. Configuration Management Keeping production configs in your app git repo is not always a good idea Getting config files over control is VERY important to avoid a messy environment Standardization is king in server configuration
  • 42. Configuration Management At this time, three choices: Puppet Chef cfengine
  • 43. Configuration Management Puppet https://github.com/puppetlabs/puppet Written in Ruby Large community adoption Great number of recipes Works well
  • 44. Configuration Management Chef https://github.com/opscode/chef Written in Ruby Created as a Puppet alternative in order to solve some problems with it Large community adoption Works good
  • 45. Configuration Management cfengine http://cfengine.com Written in C Largely adopted by sysadms HARD to learn F****** ROBUST (a single master node can manage 4K servers)
  • 46. Load Balancing WTF is Load Balancing? A network methodology to distribute workload across multiple computers in a transparent way for users Can be proved by /hard|soft/ware Alternatives: LVS with keepalived HAProxy with heartbeat
  • 47. Load Balancing LVS (Linux Virtual Server) http://www.linuxvirtualserver.org Robust, high scalable solution Need to share HTTP sessions? It’s the choice Built-in Linux Kernel module since 2.4.28*, runs in layer-4 (transport layer) keepalived http://www.keepalived.org Largely used with LVS by sysadms Uses Virtual Router Redundancy Protocol (vrrp) - RFC
  • 48. Load Balancing HAProxy http://haproxy.1wt.eu Generic solution Works if session sharing between servers is not required Runs in layer-7 (application layer), computationally more expensive heartbeat http://www.linux-ha.org/wiki/Heartbeat Based on message exchange between clients in a cluster to detect failures
  • 49.
  • 50. Conclusion Porting Rails apps to a HA environment is a not trivial challenge, but nothing insanely hard Forces you to think in application architecture, design, distribution, modularity brotip #915435: do it before coding Restricts you in order to make server correctly managed Implies you to be prudent on a deploy “it sucks at all” = bad excuse, my friend App tunning later, without fear :) In fact, in HA deploys are more faster
  • 51. Questions? No fear, answers are free as beer :)