Upgrade to Pro — share decks privately, control downloads, hide ads and more …

About: consistent hashing

Harukasan
February 27, 2017

About: consistent hashing

Harukasan

February 27, 2017
Tweet

More Decks by Harukasan

Other Decks in Technology

Transcript

  1. ӋԍӏӑӞԍӠӧӜӏӾʂҍ҅ѥ҈
    ӬӆӏӮװɒױґҭѭѷҵ

    View full-size slide

  2. ѵѵ҉ѥѧӧӜӏԍӇҊґ

    View full-size slide

  3. Ό਺ҐӄـҴΌ਺ҐӦـӡҍðҬƾ҈ҁѥ

    Ҋѥѧī୊

    View full-size slide

  4. ׏ֿ ׏׀ ׏ׁ
    ׏ׅ ׏׆ ׏ׇ ׏ֿ־
    ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ
    ӄـ
    Ӧـӡ

    View full-size slide

  5. ׏ֿ ׏׀ ׏ׁ
    ׏ׅ ׏׆ ׏ׇ ׏ֿ־
    ׏ֿ ׏׀ ׏ׁ ׏ׂ ׏׃ ׏ׄ
    ˅૝ˤҍґѵѧҌ҆҈ҝѹѥ
    Ӧـӡ

    View full-size slide

  6. ѵҮѮ҉ѯҭҊ
    ؆!׌ҐӏӼـӟҺԍӇ
    ؆ӄӼӜӏӾҐåࢄشࠓ´҆҈ҭҥ҅
    ؆Ҍҍѭѣҭҥ҅ҴҌҵѭͮѥײѺҍåð
    ……ҊѭҠѣѥүѥүҊ¸ê

    View full-size slide

  7. €˕ӏԍӯԃҌȠʂ
    פӺӐӾԅʂץ

    View full-size slide

  8. ׏ֿ
    ׏׀
    ׏ׁ
    ׏ֿ
    ׏׀
    ׏ׁ
    %3 = 1
    %3 = 2
    %3 = 0
    ׏ׂ %3 = 1
    ӺӐӾԅʂ
    Ӧـӡ਺Ґ৒²ҴҊҭ
    ׏׃
    ׏ׄ
    %3 = 2
    %3 = 0

    View full-size slide

  9. ӺӐӾԅʂ׋
    ؆ÅҬѮχѵҫҌѱ҈ͮѷѿѧ
    ؆ӏԍӯԃ
    ؆ӦـӡҴϟՃѻҭҊѯҍŖő

    View full-size slide

  10. ӺӐӾԅʂ
    ׏ֿ
    ׏׀
    ׏ׁ
    ׏ֿ
    ׏׀
    ׏ׁ
    %3 = 1
    %3 = 2
    %3 = 0
    ׏ׂ %3 = 1
    ׏׃
    ׏ׄ
    %3 = 2
    %3 = 0

    View full-size slide

  11. ӺӐӾԅʂ
    ׏ׂ
    ׏ֿ
    ׏׀
    ׏ׁ
    ׏ֿ
    ׏׀
    ׏ׁ
    %3 = 1
    %3 = 2
    %3 = 0
    ׏ׂ %3 = 1
    ׏׃
    ׏ׄ
    %3 = 2
    %3 = 0

    View full-size slide

  12. ׏ֿ
    ׏׀
    ׏ׁ
    ׏ֿ
    ׏׀
    ׏ׁ
    %4 = 1
    %4 = 2
    %4 = 3
    ׏ׂ %4 = 0
    ӺӐӾԅʂ
    ׏ׂ
    ׏׃
    ׏ׄ
    %4 = 1
    %4 = 2
    ӦـӡҴϟՃѻҭҊѯҍŖő׎׎׎
    ׏׀¦߱ѻқ҈҉ȳѯȈѩѮˠੜ

    View full-size slide

  13. פӺӐӾԅʂץґ
    ࣮ΆѮ؆୯҂ѮӦـӡҐϟՃ؏࡟আѮŖő

    View full-size slide

  14. ӋԍӏӑӞԍӠӧӜӏӾʂ
    • @_LE@YJFYF[@Q׌،TSZLZ[FS[ @ZKLSJ@SE@SETR
    YFFZ׋LZ[YLC\[FE@DKLSJYT[TDTQZITYFQLF_LSJ T[
    WT[ZTS[KF"TYQE"LEF"FC،׮ׇׇֿׅׯ
    • ࣮ΆҊѹ҈ґFRD@DKFEҐҁңҐF[@R@ѮȵĚ

    K[[WZ׋זזJL[K\CאDTRזזPF[@R@
    • SJLSa׍W@DKFY@IILDFY_FYҤ࠾ˉ

    View full-size slide

  15. ӋԍӏӑӞԍӠӧӜӏӾʂ׋
    ؆ӧӜӏӾВ਺ґ€ɠҍåƖѻҭґѼ
    ؆ӧӜӏӾВ਺ҴԂԍӇ…ҍѹ҈࣮Ά

    View full-size slide

  16. ׏ֿ
    ׏׀
    ׏ׁ
    ӋԍӏӑӞԍӠӧӜӏӾʂ

    Ӧـӡ׍ӄـҐѿҮҀҮҴęѺӧӜӏӾ̌БҍࣹӨ
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ӧӜӏӾ̌Б

    View full-size slide

  17. ׏ֿ
    ׏׀
    ׏ׁ
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ӧӜӏӾ̌Б
    ׏ֿ
    ׏׀
    ׏ׁ
    ׏ׂ
    ׏׃
    ׏ׄ
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ӋԍӏӑӞԍӠӧӜӏӾʂ

    Ӧـӡ׍ӄـҐѿҮҀҮҴęѺӧӜӏӾ̌БҍࣹӨ

    View full-size slide

  18. ׏ֿ
    @ZK׮ ׯ
    ׏ֿ
    ׏׀
    ׏ׁ
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ۙѥÃҴબ୒
    ׏ֿ
    ׏׀
    ׏ׁ
    ׏ׂ
    ׏׃
    ׏ׄ
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ֿ˕…ҐÃҴҊҭ
    ؼ
    ӧӜӏӾ̌Б
    ӋԍӏӑӞԍӠӧӜӏӾʂ

    Ӧـӡ׍ӄـҐѿҮҀҮҴęѺӧӜӏӾ̌БҍࣹӨ

    View full-size slide

  19. ӋԍӏӑӞԍӠӧӜӏӾʂ׋
    ؆ӧӜӏӾВ਺ґ€ɠҍåƖѻҭґѼ

    ؕҌҐ҉ÅҫҌѥ
    ؆ȳѯȈҰҭҐґӧӜӏӾ̌БҐ€϶҂ѳҐґѼ

    ؕҌҐ҉ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ

    View full-size slide

  20. ׏ֿ
    ׏׀
    ׏ׁ
    ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ӧӜӏӾ̌Б
    ׏ֿ
    ׏׀
    ׏ׁ
    ׏ׂ
    ׏׃
    ׏ׄ
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ

    View full-size slide

  21. ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ
    ӧӜӏӾ̌Б
    ׏ֿ
    ׏׀
    ׏ׁ
    ׏ׂ
    ׏׃
    ׏ׄ
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ׏ֿ
    ׏׀
    ׏ׁ
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ׏ׂ
    @ZK׮ ׯ
    ׏ׂҴϟՃ

    View full-size slide

  22. ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ
    ӧӜӏӾ̌Б
    ׏ֿ
    ׏׀
    ׏ׁ
    ׏ׂ
    ׏׃
    ׏ׄ
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ׯ
    ׏ֿ
    ׏׀
    ׏ׁ
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ۙѥÃҴબ୒
    ׏ׂ
    @ZK׮ ׯ
    ȳѯȈҰ҆ҁҐґ׏ׄ҂ѳ׎

    View full-size slide

  23. ӋԍӏӑӞԍӠӧӜӏӾʂ׋
    ؆ÅҫҌѥ
    ؆ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ

    View full-size slide

  24. ӧӜӏӾ̌Б
    ׏ֿ
    @ZK׮ ׯ
    ˅૝ˤҍґ ؟ֿذ׀׋

    View full-size slide

  25. ˅૝ˤҍґ
    ӧӜӏӾ̌Б
    ׏ֿ
    ׏׀
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׏׀ҴϟՃѻҭҊ
    ؟ֿذ׀׋׃־أ

    View full-size slide

  26. ӧӜӏӾ̌Б
    ׏ֿ
    ׏׀
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׏ׁ
    @ZK׮ ׯ
    ˅૝ˤҍґ ؟׀ذׁ׋׀׃أ
    ׏ׁҴϟՃѻҭҊ

    View full-size slide

  27. ӧӜӏӾ̌Б
    ׏ֿ
    ׏׀
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׏ׁ
    @ZK׮ ׯ
    ׏ׂ
    @ZK׮ ׯ
    ˅૝ˤҍґ ؟ׁذׂ׋׀׃أ
    ׏ׂҴϟՃѻҭҊ

    View full-size slide

  28. ӋԍӏӑӞԍӠӧӜӏӾʂ׋
    ؆ÅҫҌѥ
    ؆ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ

    View full-size slide

  29. ӋԍӏӑӞԍӠӧӜӏӾʂҐ࣮Ά

    ׮F[@R@ҐńĘׯ

    View full-size slide

  30. # ϋογϡؔ਺͸32bit unsigned intͷϋογϡ஋Λฦ͢

    # CRC32, MD5ͷ্Ґ32bitͳͲͰ࣮૷

    func hash(key string) uint32 { ... }
    # ϊʔυͷϋογϡ஋Λܭࢉ
    for _, node := range nodes {
    # ͻͱͭͷϊʔυຖʹ160ݸͷϋογϡΛܭࢉ
    for i := 0; i < 160; i++ {
    k := fmt.Sprintf(“%s-%d”, node, i)


    # ϊʔυ໊ͱϋογϡ஋ͷϖΞΛ௥Ճ
    continuum.add(node, hash(k))
    }
    }
    # ϋογϡ஋Ͱιʔτ͓ͯ͘͠
    sort.Sort(continuum)
    ֿאӦـӡӶӜӯҐܭࢉ

    View full-size slide

  31. ֿאӦـӡӶӜӯҐܭࢉ
    ӧӜӏӾ̌Б
    ׏ֿ
    @ZK׮ ׯ
    ֿ҅ҐӦـӡҍରѹ

    Ό਺ҐӧӜӏӾҴܭࢉ

    ذӧӜӏӾܭࢉҍҩҭÅҬҴЕɦ
    ؕ׮׀־־¿ѱҫѥ҉ůŲׯ

    View full-size slide

  32. # ϋογϡ஋Ͱιʔτ͓͚ͯ͠͹ೋ෼୳ࡧͰ୳ࡧͰ͖Δ

    func search(key string) string {
    v := hash(key)
    i, j := 0, n
    for i < j {
    h := i + (j-i)/2
    if (continuum.values[i] > v) {
    i = h + 1
    } else {
    j = h
    }
    }
    return continuum.nodes[i]
    }
    ׀אӧӜӏӾҐ୳ࡧ

    View full-size slide

  33. ׀אӧӜӏӾҐ୳ࡧ
    ӧӜӏӾ̌Б
    _
    @ZK׮ ׯ
    ׏ֿ
    ׏׀
    ׏ׁ
    @ZK׮
    @ZK׮
    @ZK׮
    ׯ
    ׯ
    ׯ
    ׏ׂ
    @ZK׮ ׯ
    ™å୳ࡧ҉€˕ۙѥӧӜӏӾҴʹŲ

    View full-size slide

  34. ҠҊң

    ׮ӋԍӏӑӞԍӠӧӜӏӾʂׯ
    ؆ÅҫҌѥ

    ؕӧӜӏӾВ਺Ҋѹ҈ґ׃…¯ׁ׀CL[׌ׁ׀׌!؆ֿ̙
    ؆ϟՃѹҁҊѯҐӘӹـӐѮƀҌѥ

    ؆࣮ΆҤӏԍӯԃ

    View full-size slide