Introduction to Unicorn - Paul Grayson

Introduction to Unicorn - Paul Grayson

B044a0f039af800f4df09bf3b2465f18?s=128

Las Vegas Ruby Group

June 06, 2012
Tweet

Transcript

  1. Introduction to Unicorn, a Rack HTTP server Paul Grayson, 6

    June 2012
  2. 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]
  3. 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.
  4. 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
  5. Traditional Rack servers (Mongrel, Thin) • Multiple processes & ports

    • A proxy distributes clients 1 2 3 4
  6. Traditional Rack servers (Mongrel, Thin) • Multiple processes & ports

    • A proxy distributes clients 1 2 3 4
  7. Traditional Rack servers (Mongrel, Thin) • Multiple processes & ports

    • A proxy distributes clients 1 2 3 4
  8. Traditional Rack servers (Mongrel, Thin) • Multiple processes & ports

    • A proxy distributes clients 1 2 3 4
  9. Traditional Rack servers (Mongrel, Thin) Slow requests will slow down

    other clients! 1 2 3 4
  10. Traditional Rack servers (Mongrel, Thin) Slow requests will slow down

    other clients! 2 3 4 1
  11. Traditional Rack servers (Mongrel, Thin) Slow requests will slow down

    other clients! 2 4 1 3
  12. [show thin example]

  13. Unicorn • Multiple processes, ONE shared socket • Unicorns accept

    connections when idle 1 2 3 4 Next! Next!
  14. Unicorn 1 3 4 2 • Multiple processes, ONE shared

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

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

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

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

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

    socket • Unicorns accept connections when idle
  20. [show unicorn example]

  21. 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]
  22. 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
  23. 32-bit server, >200MB RAM per unicorn Conclusion: a decent server

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