TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Hazelcast

๐Ÿ 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.
A smart client and the partition tableStep 4 of 4
๐Ÿฏ The hive๐Ÿง‘โ€๐ŸŒพGardenerhas the table๐ŸBee Ada๐ŸBee Bo๐ŸBee Cyowns 42
  1. The gardener knocks on one bee's door and asks who looks after what. Ada hands her a map of the whole hive.
  2. She wants to store Raj's jar. The label points to cell 42, which Cy looks after, so she walks straight to Cy.
  3. Cy stores the jar and tells the gardener it's done.
  4. Bo's jar is in a different cell, so the gardener goes to Ada this time. Each jar goes to its own bee.
Owners and backupsStep 5 of 5
๐Ÿฏ Ada's cells๐Ÿฏ Bo's cellsCell 7a1key Ana: a10b1key Bo: b1123Cell 7 twina1key Ana: a10b1key Bo: b1โœฆโœฆโœฆโœฆโœฆ123๐Ÿง‘โ€๐ŸŒพGardener๐ŸBee Adaowns 7๐ŸBee Bohas a copy๐ŸBee Cy
  1. The gardener hands Ada a jar labelled Ana. It belongs in cell 7, which Ada looks after, so Ada puts it in.
  2. Ada makes the twin of cell 7 with Bo, who keeps the spare copy. She hasn't said "done" yet.
  3. Bo says the twin is ready, and only then does Ada tell the gardener it's done. A crash now loses nothing.
  4. With the fast way, the gardener hands Ada Bo's jar and Ada says "done" straight away, before any twin exists.
  5. 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.
A member dies and the cluster healsStep 5 of 5
๐Ÿฏ Ada's cells๐Ÿฏ Bo's cells๐Ÿฏ Cy's cellsCell 7a1key Ana: a10123Cell 7 twina1key Ana: a10123New twina1key Ana: a10123๐Ÿง‘โ€๐ŸŒพGardenerโœ• โœ•๐Ÿ’ซBee Adano answer๐ŸBee Bonow owns 7๐ŸBee Cynew copy
  1. Ana's jar sits in cell 7 with Ada, and Bo keeps the twin. Everything is in order.
  2. Ada goes missing. Bo and Cy call out to her and hear nothing back.
  3. Bo is holding the twin of cell 7, so he takes it over. The hive updates its map and tells everyone, the gardener too.
  4. Cell 7 has no twin any more, so Bo copies it to Cy. That copying is called migration, and it runs in the background.
  5. The gardener asks for Ana's jar. Bo has it, so she gets it as if nothing happened.
A near cache and its invalidationStep 6 of 6
๐Ÿฏ The hivePocket jar ยท refilleda1key Ana: a10a2key Ana: a2โœฆโœฆโœฆโœฆโœฆ12Shelf of jarsa1key Ana: a10a2key Ana: a212๐Ÿง‘โ€๐ŸŒพGardener๐Ÿ‘ฉโ€๐ŸŒพOther gardener๐ŸBee Ada
  1. Another gardener puts Ana's jar on the shelf with the bee who looks after it.
  2. Our gardener checks her pocket jar first. It's empty.
  3. So she walks to the hive, the bee hands her Ana's jar, and she keeps a copy in her pocket jar.
  4. Next time she finds the copy in her pocket jar and doesn't walk to the hive at all.
  5. The other gardener changes the real jar. The bee sends word, and the pocket copy is marked out of date.
  6. Her pocket jar has nothing valid now, so she fetches the new jar from the hive and keeps a fresh copy.
Pull the data or push the codeStep 4 of 4
๐Ÿฏ Ada's cellsApple jar10key apples: 1009key apples: 918key apples: 8โœฆโœฆโœฆโœฆโœฆ23๐Ÿง‘โ€๐ŸŒพGardener๐ŸBee Adaruns on the owner
  1. The apple jar holds 10 spoonfuls and sits in Ada's cell.
  2. The slow way. The gardener asks for the whole jar, and Ada carries it over.
  3. 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.
  4. 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.
Split-brain and the merge policyStep 5 of 5
๐Ÿฏ Left half๐Ÿฏ Right halfmergeShelf, left sidea1key Ana: a10a2key Ana: a212Shelf, right sidea1key Ana: a10a3key Ana: a31a2key Ana: a2โœฆโœฆโœฆโœฆโœฆ2๐Ÿง‘โ€๐ŸŒพGardener Mia๐ŸBee Adacan't see C๐ŸBee Bo๐ŸBee Cymerged, A3 lost๐Ÿ‘ฉโ€๐ŸŒพGardener Raj
  1. Everything works. Mia stores Ana's jar, and the bees on both sides know about it.
  2. A fence goes up. The left bees and the right bee can't reach each other, and each side thinks the other is gone.
  3. Both sides carry on. Mia changes Ana's jar on the left, and Raj changes it differently on the right.
  4. The fence comes down. The smaller half walks over and offers its version of Ana's jar.
  5. 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.
A long GC pause looks like a crashStep 6 of 6
๐Ÿฏ The hive๐Ÿง‘โ€๐ŸŒพGardener๐ŸBee Adanew member๐ŸBee Bo๐ŸBee Cy
  1. Ada, who looks after a huge amount of honey, dozes off for a long time. The gardener calls and nobody answers.
  2. Bo and Cy keep checking on Ada. She's silent, and the wait gets longer than they can accept.
  3. Bo and Cy decide Ada is gone and cross her off the hive's list, even though she is only asleep.
  4. Bo takes over Ada's cells from the twins he kept, and new twins are made with Cy. Everyone is busy carrying honey.
  5. The gardener asks again, and this time Bo hands over Ana's jar.
  6. Ada wakes up and has to join the hive again as if new. Her cells are shared out again, and more carrying begins.
Embedded member or separate clientStep 3 of 3
๐Ÿก Mia's garden๐Ÿก Bo's garden๐Ÿง‘โ€๐ŸŒพGardener Mia๐ŸHer own beeowns ana๐ŸBee Boowns bo๐Ÿ‘ฉโ€๐ŸŒพVisiting gardener
  1. Mia asks for Ana's jar, which her own bee looks after. She reaches it without leaving her garden.
  2. Bo's jar is looked after by Bo, in another garden. That takes one trip.
  3. 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.