{
  "id": "prob",
  "title": "Probabilistic data types",
  "url": "https://redis.io/docs/latest/develop/clients/rust/prob/",
  "summary": "Learn how to use HyperLogLog approximate cardinality with redis-rs.",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-05-18T05:44:54-07:00",
  "page_type": "content",
  "content_hash": "2a58957fa136fdcf529ffd75e6d8900e9a1b982e78876309af9818bfe6460dad",
  "sections": [
    {
      "id": "set-cardinality",
      "title": "Set cardinality",
      "role": "content",
      "text": "A HyperLogLog object calculates the approximate cardinality of a set. As you add\nitems, the HyperLogLog tracks the number of distinct set members, but it doesn't\nlet you retrieve those members or test whether a specific item was added.\n\nYou can also merge two or more HyperLogLogs to find the approximate cardinality\nof the [union](https://en.wikipedia.org/wiki/Union_(set_theory)) of the sets\nthey represent.\n\nSet cardinality: Estimate distinct item count using HyperLogLog with minimal memory usage\n\n**Difficulty:** Beginner\n\n**Available in:** C#, Go, Java (Synchronous - Jedis), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]\n\n\n\nThe main benefit that HyperLogLogs offer is their very low memory usage. They\ncan count up to 2^64 items with less than 1% standard error using a maximum\n12KB of memory."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "See the following pages to learn more:\n\n- [HyperLogLog](https://redis.io/docs/latest/develop/data-types/probabilistic/hyperloglogs)\n- [`redis-rs` command trait](https://docs.rs/redis/latest/redis/trait.Commands.html)\n- [`redis-rs` async command trait](https://docs.rs/redis/latest/redis/trait.AsyncCommands.html)"
    }
  ],
  "examples": [
    {
      "id": "set-cardinality-ex0",
      "language": "csharp",
      "code": "bool res10 = db.HyperLogLogAdd(\n            \"group:1\",\n            [\"andy\", \"cameron\", \"david\"]\n        );\n        Console.WriteLine(res10); // >>> true\n\n        long res11 = db.HyperLogLogLength(\"group:1\");\n        Console.WriteLine(res11); // >>> 3\n\n        bool res12 = db.HyperLogLogAdd(\n            \"group:2\",\n            [\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"]\n        );\n        Console.WriteLine(res12); // >>> true\n\n        long res13 = db.HyperLogLogLength(\"group:2\");\n        Console.WriteLine(res13); // >>> 4\n\n        db.HyperLogLogMerge(\n            \"both_groups\",\n            \"group:1\", \"group:2\"\n        );\n\n        long res14 = db.HyperLogLogLength(\"both_groups\");\n        Console.WriteLine(res14); // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex1",
      "language": "go",
      "code": "res10, err := rdb.PFAdd(\n\t\tctx,\n\t\t\"group:1\",\n\t\t\"andy\", \"cameron\", \"david\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res10) // >>> 1\n\n\tres11, err := rdb.PFCount(ctx, \"group:1\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res11) // >>> 3\n\n\tres12, err := rdb.PFAdd(ctx,\n\t\t\"group:2\",\n\t\t\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res12) // >>> 1\n\n\tres13, err := rdb.PFCount(ctx, \"group:2\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res13) // >>> 4\n\n\tres14, err := rdb.PFMerge(\n\t\tctx,\n\t\t\"both_groups\",\n\t\t\"group:1\", \"group:2\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res14) // >>> OK\n\n\tres15, err := rdb.PFCount(ctx, \"both_groups\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res15) // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex2",
      "language": "java",
      "code": "long res10 = jedis.pfadd(\"group:1\", \"andy\", \"cameron\", \"david\");\n        System.out.println(res10);  // >>> 1\n\n        long res11 = jedis.pfcount(\"group:1\");\n        System.out.println(res11);  // >>> 3\n\n        long res12 = jedis.pfadd(\n            \"group:2\",\n            \"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"\n        );\n        System.out.println(res12);  // >>> 1\n\n        long res13 = jedis.pfcount(\"group:2\");\n        System.out.println(res13);  // >>> 4\n\n        String res14 = jedis.pfmerge(\"both_groups\", \"group:1\", \"group:2\");\n        System.out.println(res14);  // >>> OK\n\n        long res15 = jedis.pfcount(\"both_groups\");\n        System.out.println(res15);  // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex3",
      "language": "php",
      "code": "$r->del('group:1', 'group:2', 'both_groups');\n\n$r->pfadd('group:1', ['andy', 'cameron', 'david']);\n$group1 = $r->pfcount('group:1');\necho $group1, PHP_EOL;\n// >>> 3\n\n$r->pfadd('group:2', ['kaitlyn', 'michelle', 'paolo', 'rachel']);\n$group2 = $r->pfcount('group:2');\necho $group2, PHP_EOL;\n// >>> 4\n\n$r->pfmerge('both_groups', 'group:1', 'group:2');\n$bothGroups = $r->pfcount('both_groups');\necho $bothGroups, PHP_EOL;\n// >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex4",
      "language": "python",
      "code": "res10 = r.pfadd(\"group:1\", \"andy\", \"cameron\", \"david\")\nprint(res10)  # >>> 1\n\nres11 = r.pfcount(\"group:1\")\nprint(res11)  # >>> 3\n\nres12 = r.pfadd(\"group:2\", \"kaitlyn\", \"michelle\", \"paolo\", \"rachel\")\nprint(res12)  # >>> 1\n\nres13 = r.pfcount(\"group:2\")\nprint(res13)  # >>> 4\n\nres14 = r.pfmerge(\"both_groups\", \"group:1\", \"group:2\")\nprint(res14)  # >>> True\n\nres15 = r.pfcount(\"both_groups\")\nprint(res15)  # >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex5",
      "language": "rust",
      "code": "let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .await\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").await.expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .await\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").await.expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .await\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .await\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7",
      "section_id": "set-cardinality"
    },
    {
      "id": "set-cardinality-ex6",
      "language": "rust",
      "code": "let group1_added: bool = r\n            .pfadd(\"group:1\", &[\"andy\", \"cameron\", \"david\"])\n            .expect(\"Failed to add items to group:1\");\n        println!(\"{group1_added}\"); // >>> true\n\n        let group1: usize = r.pfcount(\"group:1\").expect(\"Failed to count group:1\");\n        println!(\"{group1}\"); // >>> 3\n\n        let group2_added: bool = r\n            .pfadd(\"group:2\", &[\"kaitlyn\", \"michelle\", \"paolo\", \"rachel\"])\n            .expect(\"Failed to add items to group:2\");\n        println!(\"{group2_added}\"); // >>> true\n\n        let group2: usize = r.pfcount(\"group:2\").expect(\"Failed to count group:2\");\n        println!(\"{group2}\"); // >>> 4\n\n        let _: () = r\n            .pfmerge(\"both_groups\", &[\"group:1\", \"group:2\"])\n            .expect(\"Failed to merge HyperLogLogs\");\n        println!(\"OK\"); // >>> OK\n\n        let both_groups: usize = r\n            .pfcount(\"both_groups\")\n            .expect(\"Failed to count both_groups\");\n        println!(\"{both_groups}\"); // >>> 7",
      "section_id": "set-cardinality"
    }
  ]
}
