Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Unicorn - Paul Grayson

Introduction to Unicorn - Paul Grayson

Las Vegas Ruby Group

June 06, 2012
Tweet

Transcript

  1. Background - Rack • Rack is an interface between web

    servers and application code. • It is all Ruby: app is accessed via a method • Rails 3 is on Rack • [show example Rack app]
  2. Background - sockets • HTTP connection life cycle 1.Server listens

    on a TCP port 2.Client connects 3.Server accepts the connection 4.HTTP Request/response etc.
  3. Background - processes • Processes – Independent program copies –

    Copy-on-write saves RAM – Processes may share system resources ...such as sockets. Process 1 Process 2 sockets open files streams local variables execution state local variables execution state
  4. Unicorn 1 3 4 2 • Multiple processes, ONE shared

    socket • Unicorns accept connections when idle
  5. Unicorn 3 4 2 1 Next! • Multiple processes, ONE

    shared socket • Unicorns accept connections when idle
  6. Unicorn 3 4 1 2 • Multiple processes, ONE shared

    socket • Unicorns accept connections when idle
  7. Unicorn 4 2 1 3 Next! • Multiple processes, ONE

    shared socket • Unicorns accept connections when idle
  8. Unicorn 4 1 2 3 • Multiple processes, ONE shared

    socket • Unicorns accept connections when idle
  9. Unicorn 1 2 3 4 • Multiple processes, ONE shared

    socket • Unicorns accept connections when idle
  10. Unicorn – painless restarts with no downtime • Based on

    obscure UNIX signals: USR2: start new copy WINCH: gracefully stop workers QUIT: graceful shutdown – Lots of other signals, e.g. reopening logs • Write your own scripts to do it • [show example]
  11. Unicorn does not solve all problems • Use something in

    front of ruby for – slow clients – static files • Unicorn will occasionally die, so monitor it (because it is Ruby?) • Rails still uses a lot of memory
  12. 32-bit server, >200MB RAM per unicorn Conclusion: a decent server

    setup nginx buffering, static files unicorn unicorn unicorn unicorn monit, etc.