Patterns in Distributed
      Computing
          Mike Perham
     http://mikeperham.com
Me?



DataFabric
seti@home
distributed.net

     folding@home
They have nothing
  to do with us.
Say Hello to
        Production


App1      App2        App3



           DB
You have a Recurring Task
Cron? Which machine?
Add a dedicated slice?


       App1    App2      App3


Jobs
                DB
Problem:
Scaling past One Machine

   App1   App2   App3



           DB
Assumption:
Our solution is a redundant
     set of processes
Let’s get Formal
Difficulties

Asynchrony

Locality

Failure

Byzantine?
Hurdle:
Group Membership

 App1   App2   App3
Group Membership

Truly Anonymous is impossible

Algorithm scalability?

   10 nodes = 100 messages = 20kb

   1000 nodes = 1,000,000 messages = 200mb!

Can run often!
Now that everyone is in the
       same room...
Hurdle:
         Consensus

   the process of agreeing on one
result among a group of participants.
             (wikipedia)
Leader Election
Leader Election
Leader Election


Breaking symmetry

Performance vs Reliability
Consensus


Once we have consensus, everything else
follows

But at what cost?
Cold Reality:
       This stuff is
 unpredictable, hard to
test, full of nasty edge
          cases.
So what do we do?
Get Real!
Start making Trade-offs.
What are your actual
reliability requirements?
Reliability Scale
Formally
                    Correct
                      Code


Reliability Scale
Formally
 My
                           Correct
Code
                             Code


       Reliability Scale
Formally
  My
                                    Correct
 Code
                                      Code

(and probably
  yours too)    Reliability Scale
Formally
  My
                                    Correct
 Code             Memcached           Code

(and probably
  yours too)    Reliability Scale
Single Point of Failure?
       Oh noes!
Politics

   http://github.com/mperham/politics

   a Ruby library providing utilities and
algorithms for solving common distributed
           computing problems.
Problem



Database frequently scanned for events of
interest, don’t want to create event dupes
TokenWorker
Leader Election via Memcached
TokenWorker Notes

M processes, 1 becomes leader for the given
time period and performs the work

Fault tolerant, not scalable

As reliable as memcached

The trick is the memcached::add API
4 module Aggregator
 5   class Engine
 6     include Politics::TokenWorker
 7
 8     def initialize
 9       register_worker 'dash-aggregator', :iteration_length =>
60.seconds, :servers => memcached_servers
10     end
11
12     def start
13       process do
14         MetricAggregator.new.aggregate
15       end
16     end
See TokenWorker ScreenCast
Problem


Have a known space to scan regularly, divisible
into N parts

Have 12 databases which need to be scanned
for events every two minutes
StaticQueueWorker
Peer-to-Peer Work Coordination
StaticQueueWorker Notes

 Work is divided into N buckets

 M-1 processes can be working on those N
 buckets concurrently

 Scalable and fault tolerant

 Peers discover via Bonjour and communicate via
 DRb
16 module Politics
   17   class QueueWorkerExample
   18     include Politics::StaticQueueWorker
   19     TOTAL_BUCKETS = 20
   20
   21     def initialize
   22       register_worker ‘queue-example', TOTAL_BUCKETS, :iteration_length =>
60, :servers => memcached_servers
   23     end
   24
   25     def start
   26       process_bucket do |bucket|
   27         puts "PID #{$$} processing bucket #{bucket}/#{TOTAL_BUCKETS} at
#{Time.now}..."
   28         sleep 1.5
   29       end
   30     end
See StaticQueueWorker ScreenCast
What if you need the good
  stuff, with no SPOF?
Paxos

a family of protocols for solving consensus
  in a network of unreliable processors.
                (wikipedia)
Paxos


The Holy Grail of Distributed Algorithms

Google has spent tens of man-years on their C+
+ implementation.

“Paxos Made Live”
Paxos
Phase 1:
                       C
Prepare



           A           B
Paxos
Phase 1:
                       C
Prepare
               24


           A           B
                24
Paxos
Phase 1:
                           C
Prepare
               24
                    “Ok”

                “Ok”
           A               B
                24
Paxos
Phase 2:
                        C
 Accept
               24/A


           A            B
                 24/A
Paxos
Phase 2:
                             C
 Accept
               24/A
                      “Ok”

                  “Ok”
           A                 B
                 24/A
Paxos Notes


Happy path is easy.

Recovering from failures is hard.

Slow, because we need consensus.

Started implementing, quickly stopped.
The Future


Plan to continue work on Paxos, if there is
interest

Other ideas for utilities around distributed
computing themes, contact me
Summary

Formal

  Group Membership and Consensus

Informal

  TokenWorker and StaticQueueWorker

Paxos
EOF
github.com/mperham/politics
      mikeperham.com
    mperham@gmail.com

9X5u87KWa267pP7aGX3K

Editor's Notes