| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="This module implements the “Power of Two Random Choices” load balancing algorithm."><meta name="keywords" content="rust, rustlang, rust-lang, p2c"><title>tower::balance::p2c - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../../tower/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../tower/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module p2c</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Module <a href="../../index.html">tower</a>::<wbr><a href="../index.html">balance</a>::<wbr><a class="mod" href="#">p2c</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../../src/tower/balance/p2c/mod.rs.html#1-41">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>This module implements the “<a href="http://www.eecs.harvard.edu/~michaelm/postscripts/handbook2001.pdf">Power of Two Random Choices</a>” load balancing algorithm.</p> |
| <p>It is a simple but robust technique for spreading load across services with only inexact load |
| measurements. As its name implies, whenever a request comes in, it samples two ready services |
| at random, and issues the request to whichever service is less loaded. How loaded a service is |
| is determined by the return value of <a href="../../load/trait.Load.html"><code>Load</code></a>.</p> |
| <p>As described in the <a href="https://twitter.github.io/finagle/guide/Clients.html#power-of-two-choices-p2c-least-loaded">Finagle Guide</a>:</p> |
| <blockquote> |
| <p>The algorithm randomly picks two services from the set of ready endpoints and |
| selects the least loaded of the two. By repeatedly using this strategy, we can |
| expect a manageable upper bound on the maximum load of any server.</p> |
| <p>The maximum load variance between any two servers is bound by <code>ln(ln(n))</code> where |
| <code>n</code> is the number of servers in the cluster.</p> |
| </blockquote> |
| <p>The balance service and layer implementations rely on <em>service discovery</em> to provide the |
| underlying set of services to balance requests across. This happens through the |
| <a href="../../discover/trait.Discover.html"><code>Discover</code></a> trait, which is essentially a <a href="https://docs.rs/futures/0.3/futures/stream/trait.Stream.html"><code>Stream</code></a> that indicates |
| when services become available or go away. If you have a fixed set of services, consider using |
| <a href="../../discover/struct.ServiceList.html"><code>ServiceList</code></a>.</p> |
| <p>Since the load balancer needs to perform <em>random</em> choices, the constructors in this module |
| usually come in two forms: one that uses randomness provided by the operating system, and one |
| that lets you specify the random seed to use. Usually the former is what you’ll want, though |
| the latter may come in handy for reproducability or to reduce reliance on the operating system.</p> |
| </div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Balance.html" title="tower::balance::p2c::Balance struct">Balance</a></div><div class="item-right docblock-short">Efficiently distributes requests across an arbitrary number of services.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.MakeBalance.html" title="tower::balance::p2c::MakeBalance struct">MakeBalance</a></div><div class="item-right docblock-short">Constructs load balancers over dynamic service sets produced by a wrapped “inner” service.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.MakeBalanceLayer.html" title="tower::balance::p2c::MakeBalanceLayer struct">MakeBalanceLayer</a></div><div class="item-right docblock-short">Construct load balancers (<a href="struct.Balance.html"><code>Balance</code></a>) over dynamic service sets (<a href="../../discover/trait.Discover.html"><code>Discover</code></a>) produced by the |
| “inner” service in response to requests coming from the “outer” service.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.MakeFuture.html" title="tower::balance::p2c::MakeFuture struct">MakeFuture</a></div><div class="item-right docblock-short">A <a href="struct.Balance.html"><code>Balance</code></a> in the making.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="tower" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |