"""Consistent hashing in about 90 seconds, built with the opencanviz-json helper.""" import os import sys sys.path.insert(0, "plugins/opencanviz/skills/opencanviz-json/scripts") from opencanviz import Project # noqa: E402 A_C, B_C, C_C, D_C, KEY = "#38bdf8", "#22c55e", "#f59e0b", "#a78bfa", "#f472b6" p = Project("Consistent hashing", aspect="16:9", background="#0f172a", sfx="soft", stage="steady") area = p.area() # 1. Title s1 = p.scene("Title") title_r, rest = area.cut_top(area.h * 0.58, gap=10) word = s1.letters("HASHING", cx=title_r.cx, cy=title_r.y + title_r.h * 0.62, size=170) sub = s1.text("Consistent hashing, explained", at=rest.cut_top(90)[0], fontSize=48, color="#94a3b8") s1.step("Consistent hashing.").pop(word, stagger=45, from_="below") s1.step("How caches spread keys without reshuffling everything.").reveal(sub, style="rise") s1.step("Let's start with the simple way.").hide(word, style="scatter", offset="end")\ .hide(sub, offset="end") # 2. The modulo problem s2 = p.scene("Modulo") head_r, body = area.cut_top(120) head = s2.text("server = hash(key) mod N", at=head_r, fontWeight=700) left, right = body.inset(40, 20).split(0.56) tbl = s2.table([["key", "hash", "mod 3", "mod 4"], ["k1", "9", "0", "1"], ["k2", "10", "1", "2"], ["k3", "11", "2", "3"], ["k4", "12", "0", "0"]], at=left) bars = s2.compare_bars([("Keys that move", 75, "#ef4444"), ("Keys that stay", 25, "#64748b")], at=right.inset(10, 60), unit="%") s2.step("The simple way: hash the key, mod the server count.").reveal(head, style="type") s2.step("Here are four keys and their hashes.").stagger(tbl.column(0), style="fade").stagger(tbl.column(1), style="fade", offset=300) s2.step("With three servers, each key lands on hash mod three.").stagger(tbl.column(2), style="pop", gap=120) s2.step("Add a fourth server and recompute.").stagger(tbl.column(3), style="pop", gap=120) st = s2.step("Three of the four keys moved. Every move is a cache miss.") st.tint(tbl.cells[1][3:] + tbl.cells[2][3:] + tbl.cells[3][3:], "#ef4444") st.emphasize(tbl.cells[4][3], style="pulse", offset=600) s2.step("Across many keys, three quarters move. Only one in four stays put.").reveal(bars.labels) \ .stagger(bars.bars, style="wipe", gap=300).pop(bars.numbers, stagger=300, offset=500) # 3. The ring s3 = p.scene("Ring") ring = s3.ring(center=(area.cx, area.cy - 30), r=345, color="#334155", width=6) srv = {n: ring.dot(a, size=60, color=c, label=n) for n, a, c in (("A", 30, A_C), ("B", 150, B_C), ("C", 260, C_C))} arcs = [ring.arc(260, 30, color=A_C, id="arcA"), ring.arc(30, 150, color=B_C, id="arcB"), ring.arc(150, 260, color=C_C, id="arcC")] k1 = ring.dot(80, size=30, color=KEY, label="k1", id="k1") k2 = ring.dot(200, size=30, color=KEY, label="k2", id="k2") s3.step("Instead, bend the hash space into a ring.").reveal(ring.node, style="zoom") s3.step("Each server is hashed onto the ring too.").pop(list(srv.values()), stagger=160, from_="edges") s3.step("A key hashes to a spot on the same ring.").pop([k1]) s3.step("It walks clockwise to the first server it meets.").walk(k1, ring, 150, absorb_into=srv["B"]) s3.step("So B owns everything from A up to B.").reveal(arcs[1], style="draw") s3.step("A second key walks to C.").pop([k2]).walk(k2, ring, 260, offset=600, absorb_into=srv["C"]) s3.step("Every server owns the arc just before it.").reveal(arcs[2], style="draw").reveal(arcs[0], style="draw", offset=300) # 4. Adding a server s4 = p.scene("Add a server", transition="crossfade") ring4 = s4.ring(center=(area.cx, area.cy - 30), r=345, color="#334155", width=6) srv4 = {n: ring4.dot(a, size=60, color=c, label=n) for n, a, c in (("A", 30, A_C), ("B", 150, B_C), ("C", 260, C_C))} arcs4 = [ring4.arc(260, 30, color=A_C, id="arcA"), ring4.arc(30, 150, color=B_C, id="arcB"), ring4.arc(150, 260, color=C_C, id="arcC")] d = ring4.dot(205, size=60, color=D_C, label="D", id="srvD") arc_d = ring4.arc(150, 205, color=D_C, id="arcD") s4.step("Now add server D between B and C.").reveal(ring4.node).reveal(list(srv4.values())).reveal(arcs4).pop([d]) s4.step("D takes over only the keys between B and D.").reveal(arc_d, style="draw") s4.step("Only those keys move. A and B keep theirs.").emphasize(arc_d, style="flash") # 5. Results s5 = p.scene("Results", transition="crossfade") top, bottom = area.inset(60, 40).cut_top(260) a_r, b_r = top.row(2, w=320, h=150, gap=420) client, cache = s5.box("Clients", at=a_r, stroke=KEY), s5.box("Ring of caches", at=b_r, stroke=B_C) pipe = s5.arrow(client, cache) bars5 = s5.compare_bars([("Keys moved, mod N", 75, "#ef4444"), ("Keys moved, ring", 25, "#22c55e")], at=bottom.inset(160, 20), unit="%") s5.step("Requests keep flowing while the cluster grows.").reveal(client).reveal(cache).draw(pipe).stream(pipe, n=4) s5.step("Mod N moved three quarters of the keys.").reveal(bars5.labels[0]).stagger(bars5.bars[:1], style="wipe") \ .pop(bars5.numbers[:1], offset=500) s5.step("The ring moved only one key in four. Grow the cluster, keep the cache warm.").reveal(bars5.labels[1]).stagger(bars5.bars[1:], style="wipe") \ .pop(bars5.numbers[1:], offset=500) s5.step("That is consistent hashing.").pulse(bars5.numbers[1], scale=1.15) p.save(os.path.join(os.path.dirname(os.path.abspath(__file__)), "consistent_hashing.json"))