put down the hammer

Upload: senor-smiles

Post on 30-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Put Down the Hammer

    1/53

    Put Down the Hammer

    Tyler McMullen

    On using the right tool for the job.

  • 8/14/2019 Put Down the Hammer

    2/53

    Tyler McMullen

    Put Down the Hammer

    Case Study: Tag Autocompleter

  • 8/14/2019 Put Down the Hammer

    3/53

    Tyler McMullen

    Put Down the Hammer

    The Nave Solution

  • 8/14/2019 Put Down the Hammer

    4/53

    Tyler McMullen

    Put Down the Hammer

    The Nave Solution

    Tag.find:conditions=>[textLIKE?,...]

  • 8/14/2019 Put Down the Hammer

    5/53

    Tyler McMullen

    Put Down the Hammer

    The Nave Solution

    Tag.find:conditions=>[textLIKE?,...]TagController#autocomplete

  • 8/14/2019 Put Down the Hammer

    6/53

    Tyler McMullen

    Put Down the Hammer

    Thats not so bad, is it?

  • 8/14/2019 Put Down the Hammer

    7/53Tyler McMullen

    Put Down the Hammer

    What is important for an autocompleter?

  • 8/14/2019 Put Down the Hammer

    8/53Tyler McMullen

    Put Down the Hammer

    SPEED

  • 8/14/2019 Put Down the Hammer

    9/53Tyler McMullen

    Put Down the Hammer

    Lets enumerate the steps to get the tags.

  • 8/14/2019 Put Down the Hammer

    10/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

  • 8/14/2019 Put Down the Hammer

    11/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

  • 8/14/2019 Put Down the Hammer

    12/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    Router

    h

  • 8/14/2019 Put Down the Hammer

    13/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    h

  • 8/14/2019 Put Down the Hammer

    14/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    h

  • 8/14/2019 Put Down the Hammer

    15/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    MySQL full table scan

    P D h H

  • 8/14/2019 Put Down the Hammer

    16/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    MySQL full table scan

    Map rows to ActiveRecord objects

    P D h H

  • 8/14/2019 Put Down the Hammer

    17/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    MySQL full table scan

    Map rows to ActiveRecord objects

    Render JSON/HTML

    P t D th H

  • 8/14/2019 Put Down the Hammer

    18/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    MySQL full table scan

    Map rows to ActiveRecord objects

    Render JSON/HTML

    Respond

    P t D th H

  • 8/14/2019 Put Down the Hammer

    19/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    MySQL full table scan

    Map rows to ActiveRecord objects

    Render JSON/HTML

    Respond

    P t D th H

  • 8/14/2019 Put Down the Hammer

    20/53Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    MySQL full table scan

    Map rows to ActiveRecord objects

    Render JSON/HTML

    Respond

    Unnecessary steps that take up most of the time!

    P t D th H

  • 8/14/2019 Put Down the Hammer

    21/53

    Tyler McMullen

    Put Down the Hammer

    How can we do this more efficiently?

    P t D th H

  • 8/14/2019 Put Down the Hammer

    22/53

    Tyler McMullen

    Put Down the Hammer

    First, ditch the database.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    23/53

    Tyler McMullen

    Put Down the Hammer

    ActiveRecord and MySQL are overkill.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    24/53

    Tyler McMullen

    Put Down the Hammer

    Use a Trie.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    25/53

    Tyler McMullen

    Put Down the Hammer

    Use a Trie.

    (Shameless Plug: http://github.com/tyler/trie)

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    26/53

    Tyler McMullen

    Put Down the Hammer

    B EE

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    27/53

    Tyler McMullen

    Put Down the Hammer

    B

    A

    E

    E

    R

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    28/53

    Tyler McMullen

    Put Down the Hammer

    B AE

    E

    R

    A R T

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    29/53

    Tyler McMullen

    Put Down the Hammer

    Use the right data structure for the job.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    30/53

    Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Tag.find

    MySQL full table scan

    Map rows to ActiveRecord objects

    Render JSON/HTML

    Respond

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    31/53

    Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Trie#children(prefix)

    Render JSON/HTML

    Respond

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    32/53

    Tyler McMullen

    Put Down the Hammer

    Ajax Call

    Nginx

    RouterAction

    Trie#children(prefix)

    Render JSON/HTML

    Respond

    Our next victims!

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    33/53

    Tyler McMullen

    Put Down the Hammer

    Next step is to sidestep the Rails app altogether.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    34/53

    Tyler McMullen

    Put Down the Hammer

    If not Rails... then what?!

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    35/53

    Tyler McMullen

    Put Down the Hammer

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    36/53

    Tyler McMullen

    Put Down the Hammer

    Rack is awesome for little web services.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    37/53

    Tyler McMullen

    Put Down the Hammer

    Fast

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    38/53

    Tyler McMullen

    Put Down the Hammer

    FastLow Memory Usage

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    39/53

    Tyler McMullen

    Put Down the Hammer

    FastLow Memory UsageEasy

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    40/53

    Tyler McMullen

    classAutocompleterdefinitialize@trie=Trie.load_file(file,/(\w+)\|(\d+)/)end

    defcall(env)req=Rack::Request.new(env)[email protected](req.params['query'])[0,5][200,{"ContentType"=>"text/html"},[words.to_json]]

    endend

    Put Down the Hammer

    http://apidock.com/ruby/Hash/new/classhttp://apidock.com/ruby/Hash/new/class
  • 8/14/2019 Put Down the Hammer

    41/53

    Tyler McMullen

    What about routing?

  • 8/14/2019 Put Down the Hammer

    42/53

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    43/53

    Tyler McMullen

    location/autocomplete{

    proxy_passhttp://autocompleter;}

    (You are using Nginx, right?)

    Put Down the Hammer

    http://autocompleter/http://autocompleter/
  • 8/14/2019 Put Down the Hammer

    44/53

    Tyler McMullen

    Ajax Call

    Nginx

    RouterAction

    Trie#children(prefix)

    Render JSON/HTML

    Respond

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    45/53

    Tyler McMullen

    Ajax Call

    Nginx

    Rack ServiceTrie#children(prefix)

    Render JSON/HTML

    Respond

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    46/53

    Tyler McMullen

    Does it really matter?

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    47/53

    Tyler McMullen

    Its handling 200 requests per second.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    48/53

    Tyler McMullen

    Its handling 200 requests per second.On a single Thin instance.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    49/53

    Tyler McMullen

    Its handling 200 requests per second.

    On a single Thin instance.With a response time of 0.006s.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    50/53

    Tyler McMullen

    Its handling 200 requests per second.

    On a single Thin instance.With a response time of 0.006s.

    Takes up 18mb of ram.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    51/53

    Tyler McMullen

    Its handling 200 requests per second.

    On a single Thin instance.

    With a response time of 0.006s.Takes up 18mb of ram.

    And

  • 8/14/2019 Put Down the Hammer

    52/53

    Tyler McMullen

    Its good for your users and your budget.

    Put Down the Hammer

  • 8/14/2019 Put Down the Hammer

    53/53

    Questions?