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

LuaConf - Intelligent Load Balancing

LuaConf - Intelligent Load Balancing

How OpenResty and Lua are used to build 'intelligent load balancers', that can perform complex tasks such as: caching, throttling and application routing.

Xavier Denis

July 09, 2016
Tweet

Other Decks in Technology

Transcript

  1. 3

  2. 6

  3. 26

  4. 29

  5. Pods: wtf ? 35 • a pod is a fully

    isolated section of Shopify • can serve requests on its own • can fail on its own • can scale on its own
  6. SortingHat 41 • requests belong to a pod and need

    to be assigned • extracts pod information • routes the request to correct upstreams
  7. • luckily, requests are for storefront urls • uses MySQL

    table mapping domains to stores • solves the first 90% s 43 Domain Lookup
  8. s 44 What about the others? $ curl -v yeezysupply.com

    * Rebuilt URL to: yeezysupply.com/ * Trying 23.227.38.32... * Connected to yeezysupply.com (23.227.38.32) port 80 (#0) > GET / HTTP/1.1 > Host: yeezysupply.com > User-Agent: curl/7.43.0 > Accept: */* >
  9. s 45 What about the others? $ curl -v yeezysupply.com

    * Rebuilt URL to: yeezysupply.com/ * Trying 23.227.38.32... * Connected to yeezysupply.com (23.227.38.32) port 80 (#0) > GET / HTTP/1.1 > Host: yeezysupply.com > User-Agent: curl/7.43.0 > Accept: */* > hostname
  10. s 46 What about the others? $ curl -v app.shopify.com/amazon_mws/12345

    * Trying 23.227.38.38... * Connected to app.shopify.com (23.227.38.38) port 80 (#0) > GET /amazon_mws/12345 HTTP/1.1 > Host: app.shopify.com > User-Agent: curl/7.43.0 > Accept: */* >
  11. s 47 What about the others? $ curl -v app.shopify.com/amazon_mws/12345

    * Trying 23.227.38.38... * Connected to app.shopify.com (23.227.38.38) port 80 (#0) > GET /amazon_mws/12345 HTTP/1.1 > Host: app.shopify.com > User-Agent: curl/7.43.0 > Accept: */* > shop id
  12. What about the others? 49 { pattern = "^/orders/(\\d+)", http

    = HTTP_METHODS, name = "checkout_legacy", method = shop_from_param }, { pattern = "^/favicon.ico$", http = {GET = true}, name = "favicon", method = get_favicon },
  13. What about the others? 50 { pattern = "^/orders/(\\d+)", http

    = HTTP_METHODS, name = "checkout_legacy", method = shop_from_param }, { pattern = "^/favicon.ico$", http = {GET = true}, name = "favicon", method = get_favicon }, A table containing several key fields: pattern, http, name, method
  14. What about the others? 51 1. match the pattern 2.

    match the http verb 3. apply the method 4. use results to route request 5. during an error log the name
  15. What about the others? 52 • method can access full

    request information • POST data, UA, you name it • We currently have a dozen • pattern is a full regex, not a pattern (counterintuitively) • We have several hundred rules
  16. Automatic Route Dumps 55 $ ./script/dump-routes export /services > routes

    Loading 2158 routes Selecting 661 routes Ouputting 397 rules
  17. Automatic Route Dumps 56 { "pattern": "\\A\/services\/ping(?:\\.([^\/.?]+))?\\Z", "name": "services_ping", "http":

    [ "DELETE", "GET", "PATCH", "POST", "PUT", "HEAD" ], "method": "master_only" }
  18. 58

  19. 60

  20. 62

  21. 63

  22. 64

  23. EdgeCache 65 • standard caches don't work • need to

    handle order of magnitude more traffic • Need to handle sensitive information
  24. EdgeCache 66 • generates app aware cache keys • uses

    cookies to avoid mixing up privileged pages • uses a super short TTL (5s)
  25. 70

  26. Checkout Throttle 71 • caching read-heavy pages is easy (sort

    of) • caching write-heavy areas is much harder • instead throttle access to write portions of the site
  27. Flow - PID 75 PROCESS PID CONTROLLER SENSOR Controller Output

    Setpoint Process Variable Measured Process Variable Disturbances
  28. Flow - PID 76 • PID maximizes RPM, adjusts queue

    lag • On each request, look at cookie, compare to lag • If lag > cookie, request passes • If lag < cookie, and we have spare RPM, request passes • Otherwise, we bounce request
  29. Custom Balancers 78 • often lb algorithms are not well

    suited to applications • bad algorithm causes lost performance • can increase errors
  30. Mobile First 86 • mobile is the future • restrict

    non-mobile user capacity • create dedicated mobile pools • blackberry & windows get less
  31. Deterministic Hashing 88 • client maps to deterministic set •

    uses IP + shop_id • creates blast radius • Used by Google
  32. EWMA 89 • Exponentially Weighted Moving Average • request latency

    per upstream • based on Finangle (Twitter) algorithms
  33. HTTPDB 91 • not actually a db • shared dicts

    over HTTP • real-time configuration!