9 releases (breaking)

0.8.0 Mar 2, 2026
0.6.0 Aug 15, 2025
0.5.0 May 9, 2025
0.4.0 Nov 1, 2024
0.1.0 Feb 28, 2024

#472 in Caching

Download history 37712/week @ 2025-11-20 41190/week @ 2025-11-27 64921/week @ 2025-12-04 52614/week @ 2025-12-11 31175/week @ 2025-12-18 28112/week @ 2025-12-25 42127/week @ 2026-01-01 77948/week @ 2026-01-08 119175/week @ 2026-01-15 137524/week @ 2026-01-22 195901/week @ 2026-01-29 292104/week @ 2026-02-05 266951/week @ 2026-02-12 148382/week @ 2026-02-19 146461/week @ 2026-02-26 166196/week @ 2026-03-05

785,679 downloads per month
Used in 62 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

~0–1.6MB