10 releases (breaking)

new 0.8.1 Jun 4, 2026
0.8.0 Mar 2, 2026
0.7.0 Jan 30, 2026
0.6.0 Aug 15, 2025
0.1.0 Feb 28, 2024

#10 in #consistent-hashing

Download history 22361/week @ 2026-03-03 183299/week @ 2026-03-10 145159/week @ 2026-03-17 123313/week @ 2026-03-24 120741/week @ 2026-03-31 201815/week @ 2026-04-07 189693/week @ 2026-04-14 220580/week @ 2026-04-21 233945/week @ 2026-04-28 153450/week @ 2026-05-05 185154/week @ 2026-05-12 175724/week @ 2026-05-19 177085/week @ 2026-05-26 119325/week @ 2026-06-02

679,448 downloads per month
Used in 70 crates (2 directly)

Apache-2.0

51KB
403 lines

pingora-ketama

A Rust port of the nginx consistent hashing algorithm.

This crate provides a consistent hashing algorithm which is identical in behavior to nginx consistent hashing.

Using a consistent hash strategy like this is useful when one wants to minimize the amount of requests that need to be rehashed to different nodes when a node is added or removed.

Here's a simple example of how one might use it:

use pingora_ketama::{Bucket, Continuum};

fn main() {
    // Set up a continuum with a few nodes of various weight.
    let mut buckets = vec![];
    buckets.push(Bucket::new("127.0.0.1:12345".parse().unwrap(), 1));
    buckets.push(Bucket::new("127.0.0.2:12345".parse().unwrap(), 2));
    buckets.push(Bucket::new("127.0.0.3:12345".parse().unwrap(), 3));
    let ring = Continuum::new(&buckets);

    // Let's see what the result is for a few keys:
    for key in &["some_key", "another_key", "last_key"] {
        let node = ring.node(key.as_bytes()).unwrap();
        println!("{}: {}:{}", key, node.ip(), node.port());
    }
}
# Output:
some_key: 127.0.0.3:12345
another_key: 127.0.0.3:12345
last_key: 127.0.0.2:12345

We've provided a health-aware example in pingora-ketama/examples/health_aware_selector.rs.

For a carefully crafted real-world example, see the pingora-load-balancing crate.

Dependencies

~70–255KB