Advertisement
Guest User

Untitled

a guest
Mar 8th, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. A lot of people at the moment are talking about intelligent routing, the supposedly high-tech solution that Heroku promised, but failed to deliver to their customers, not least among them Rap Genius[1].
  2.  
  3. While the phrase intelligent routing has been thrown around by allcomers, with even Wired getting in on the act today[2], very few people seem to have actually sat down and figured out what intelligent routing actually means.
  4.  
  5. So, what the hell is intelligent routing? How does it differ from its inferior cousin, random routing? And most importantly of all, how do we mere mortals get the same secret sauce that Heroku (doesn't) sprinkle on their lucky customers' Rails deployments for our own apps?
  6.  
  7. Note. This post talks about Rails, but it applies equally well to many other web application frameworks.
  8.  
  9. Random routing
  10. ==============
  11.  
  12. Your Rails app is deployed to a server, and to cope with the traffic you've spun up a number of instances of the app. Let's say you've got eight instances, running on ports 8000 to 8007 inclusive.
  13.  
  14. Random routing, taken literally, will distribute each incoming request for a web page to a Rails instance at random. But hold on - why did nobody ever use the phrase random routing before a few weeks ago? Oh, that's because nobody really sets up random routing in real life, they're far more likely to use Round Robin routing which isn't exactly random - it's actually sequential and completely predictable - but does still result in the same distribution pattern as random routing. Over time, each Rails instance will receive the same number of requests as any other.
  15.  
  16. Vitally, if one Rails instance is tied up with a slow-running request - a file upload perhaps, or a leisurely admin action - the instance may still be sent incoming requests. These requests will queue up behind the slow request in front of them, and take many seconds to complete which creates a poor user experience. This is despite there quite possibly being other, idle Rails processes sitting around doing nothing the whole time.
  17.  
  18. This is the gist of Rap Genius's complaint to Heroku.
  19.  
  20. Intelligent routing
  21. ===================
  22.  
  23. So how do you solve this problem? It's pretty easy actually. Enter intelligent routing. This is where a front-end web server or loadbalancer keeps track of the Rails instances with slow-running requests, and refuses to route more requests to such an instance until it's free again.
  24.  
  25. Once again, maybe it's just me but I've never once heard this load-balancing configuration referred to as "intelligent routing". That's just a buzzword that's used by people who don't understand how to set this up for themselves. But whatever you call it, here's how to set it up.
  26.  
  27. How-to set up intelligent routing for yourself.
  28. ===============================================
  29.  
  30. Here's a handy screencast written by (ironically enough) a former Heroku devops engineer. It's very informative and easy to follow.
  31. http://37signals.com/svn/posts/1073-nuts-bolts-haproxy
  32.  
  33. Install haproxy[3], use it as a front-end web server or most likely between Nginx and your Rails instances.
  34.  
  35. Here is your haproxy configuration:
  36.  
  37. defaults
  38. balance roundrobin
  39.  
  40. backend nginx
  41. timeout server 30000
  42. timeout connect 30000
  43. log 127.0.0.1 local0 debug
  44.  
  45. server thin0 127.0.0.1:8000 maxconn 1 check
  46. server thin1 127.0.0.1:8001 maxconn 1 check
  47. server thin2 127.0.0.1:8002 maxconn 1 check
  48. server thin3 127.0.0.1:8003 maxconn 1 check
  49. server thin4 127.0.0.1:8004 maxconn 1 check
  50. server thin5 127.0.0.1:8005 maxconn 1 check
  51. server thin6 127.0.0.1:8006 maxconn 1 check
  52. server thin7 127.0.0.1:8007 maxconn 1 check
  53.  
  54. The important bit is maxconn 1. This one tweak to your haproxy configuration will make haproxy behave in exactly the way that intelligent routing is meant to. Incoming requests will be routed on a round-robin basis, but no Rails instance will ever have more than one request sent to it concurrently. That means that no request will ever be queued behind a slow-running request ahead of it, unless every single Rails instance is currently busy. (Even then, haproxy will manage the queued requests efficiently and process each as quickly as possible).
  55.  
  56. It really is that simple. Now you hopefully have more of a clue how to weigh up this kerfuffle between Heroku and that rap startup too.
  57.  
  58. [1] http://rapgenius.com/James-somers-herokus-ugly-secret-lyrics
  59. [2] http://www.wired.com/wiredenterprise/2013/03/hieroku/
  60. [3] http://haproxy.1wt.eu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement