๐ Hazelcast: cheat sheet
๐จ The analogyWorker bees sharing honey in numbered cells, each copied to a second bee, so work goes to the honey.
๐ The regular explanationHazelcast is an in-memory data platform for the JVM that spreads data over a cluster in 271 partitions with backups, runs code and queries where the data lives, and stays available by default, with a Raft-based CP Subsystem (Enterprise) for locks and counters.
- The gardener knocks on one bee's door and asks who looks after what. Ada hands her a map of the whole hive.
- She wants to store Raj's jar. The label points to cell 42, which Cy looks after, so she walks straight to Cy.
- Cy stores the jar and tells the gardener it's done.
- Bo's jar is in a different cell, so the gardener goes to Ada this time. Each jar goes to its own bee.
- The gardener hands Ada a jar labelled Ana. It belongs in cell 7, which Ada looks after, so Ada puts it in.
- Ada makes the twin of cell 7 with Bo, who keeps the spare copy. She hasn't said "done" yet.
- Bo says the twin is ready, and only then does Ada tell the gardener it's done. A crash now loses nothing.
- With the fast way, the gardener hands Ada Bo's jar and Ada says "done" straight away, before any twin exists.
- Only afterwards does Ada send the twin to Bo. It's quicker, but if she had crashed in between, Bo's jar would be lost even though she said done.
- Ana's jar sits in cell 7 with Ada, and Bo keeps the twin. Everything is in order.
- Ada goes missing. Bo and Cy call out to her and hear nothing back.
- Bo is holding the twin of cell 7, so he takes it over. The hive updates its map and tells everyone, the gardener too.
- Cell 7 has no twin any more, so Bo copies it to Cy. That copying is called migration, and it runs in the background.
- The gardener asks for Ana's jar. Bo has it, so she gets it as if nothing happened.
- Another gardener puts Ana's jar on the shelf with the bee who looks after it.
- Our gardener checks her pocket jar first. It's empty.
- So she walks to the hive, the bee hands her Ana's jar, and she keeps a copy in her pocket jar.
- Next time she finds the copy in her pocket jar and doesn't walk to the hive at all.
- The other gardener changes the real jar. The bee sends word, and the pocket copy is marked out of date.
- Her pocket jar has nothing valid now, so she fetches the new jar from the hive and keeps a fresh copy.
- The apple jar holds 10 spoonfuls and sits in Ada's cell.
- The slow way. The gardener asks for the whole jar, and Ada carries it over.
- She takes a spoonful at home and carries the jar back. That's two trips, and meanwhile someone else might have taken a spoonful too.
- The better way. The gardener sends a short work order, "take one spoonful, tell me what's left", and Ada does it right at the jar.
- Everything works. Mia stores Ana's jar, and the bees on both sides know about it.
- A fence goes up. The left bees and the right bee can't reach each other, and each side thinks the other is gone.
- Both sides carry on. Mia changes Ana's jar on the left, and Raj changes it differently on the right.
- The fence comes down. The smaller half walks over and offers its version of Ana's jar.
- The rule is "keep the one already there", so the bigger half keeps its own jar, and the right bee's change is thrown away.
- Ada, who looks after a huge amount of honey, dozes off for a long time. The gardener calls and nobody answers.
- Bo and Cy keep checking on Ada. She's silent, and the wait gets longer than they can accept.
- Bo and Cy decide Ada is gone and cross her off the hive's list, even though she is only asleep.
- Bo takes over Ada's cells from the twins he kept, and new twins are made with Cy. Everyone is busy carrying honey.
- The gardener asks again, and this time Bo hands over Ana's jar.
- Ada wakes up and has to join the hive again as if new. Her cells are shared out again, and more carrying begins.
- Mia asks for Ana's jar, which her own bee looks after. She reaches it without leaving her garden.
- Bo's jar is looked after by Bo, in another garden. That takes one trip.
- A gardener who visits from outside always makes a trip, even for the nearest bee. In return, the hive keeps running when she goes home.
๐งฉ Key questions
- Ada owns cell 7 and Bo keeps its twin, with the careful way of copying. The gardener hands Ada a jar. When does Ada say "done"?
- After the twin has been made with Bo. The careful way waits for the twin, so one lost bee can't lose a jar the gardener was told is safe.
- Ada vanishes, and her cell's twin is with Bo. What does the hive do?
- Bo takes over cell 7, the hive updates its map and then makes a new twin. The twin is there for this. The only risk is a second loss before the new twin is finished.
- The gardener wants one spoonful from a huge jar. Which way is better, and why?
- Sending a short work order to the owner is better. The jar stays put, and no one can slip in between. Work goes to the honey, not the honey to the work, so one trip carries only the answer.
- A fence splits the hive and both halves change Ana's jar. When it comes down, with the keep-the-one-already-there rule, what happens?
- The bigger half keeps what it had, and the smaller half's change is lost. The rule decides, not the bees. A different rule, like the newest wins, would give a different result, which is why you pick one on purpose.
- Why does a hive with a council of bees stop answering when a fence cuts the council off from its majority?
- Because the council would rather wait than risk two different answers. Waiting costs time, while two bees both holding the nursery key could cause real harm.