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 Redis Cluster

๐Ÿซ Redis Cluster: cheat sheet

๐ŸŽจ The analogyA school office with 16,384 numbered pigeonholes shared out among the staff, picked by a fixed sum on each name.
๐ŸŽ“ The regular explanationRedis Cluster (and Valkey cluster) splits the keyspace into 16,384 hash slots spread across several primaries, each with replicas, and smart clients route every command straight to the node that owns its key's slot.
Keys, slots and primariesStep 4 of 4
Jo: 0 to 5,460Anakey homework:ana (slot 2226): Ana0123Sam: 5,461 to 10,922Bokey homework:bo (slot 6404): Bo0123Priya: 10,923 to 16,3830123๐Ÿง‘โ€๐ŸซTeacher
  1. The school's 16,384 numbered pigeonholes are split into three stretches. Jo, Sam and Priya each look after one.
  2. The teacher has Ana's homework. The fixed sum on the name gives pigeonhole 2,226, which is in Jo's stretch, so she takes it to Jo.
  3. Bo's homework sums to 6,404, a different number in Sam's stretch, so it goes to Sam. Nobody else is bothered.
  4. Fetching Ana's homework later works the same way. Same name, same sum, same pigeonhole, so the teacher goes straight back to Jo.
The slot map and MOVEDStep 5 of 5
๐Ÿง‘โ€๐ŸซNew teacherslot map cached๐Ÿ™‹โ€โ™€๏ธJo๐Ÿง”Sam๐Ÿ‘ฉโ€๐ŸฆฑPriya
  1. A new teacher only knows where Jo sits, so she asks Jo for Ana's things in pigeonhole 14,585.
  2. Jo doesn't fetch it. She says "that range is Priya's" and points.
  3. The teacher copies the office's full list of who has which stretch onto her crib sheet.
  4. Now she goes straight to Priya, who hands over Ana's things.
  5. From now on the crib sheet sends her to the right person first time, like Sam for Bo's homework.
Replicas and automatic failoverStep 5 of 5
๐Ÿ—ƒ๏ธ Jo's desk๐Ÿ—ƒ๏ธ Sam's desk๐Ÿ—ƒ๏ธ Priya's desk๐Ÿง‘โ€๐ŸซTeacher๐Ÿ™‹โ€โ™€๏ธJoโœ• โœ•๐Ÿ’ซSamFAIL๐Ÿ‘ฉโ€๐ŸฆฑPriya๐Ÿง‘โ€๐ŸŽ“Jo's deputy๐Ÿ‘ฉโ€๐ŸŽ“Sam's deputynew primary๐Ÿง‘โ€๐ŸŽ“Priya's deputy
  1. The teacher files Bo's homework with Sam. A moment later, Sam's deputy makes a copy of it.
  2. Sam suddenly goes home ill. When Jo and Priya check in on the walkie-talkies, Sam doesn't answer.
  3. Jo and Priya compare notes. Most of the staff agree Sam is really gone, so it's official.
  4. Sam's deputy asks the others for the go-ahead to take over, and both say yes.
  5. Sam's deputy now looks after Sam's whole stretch. Teachers go to the deputy instead, and Bo's homework is there.
Live resharding and ASKStep 5 of 5
Jo's pigeonhole 2,226k1key key 1 in slot 2226: k10k2key key 2 in slot 2226: k21k3key key 3 in slot 2226: k323movedMax's pigeonhole 2,226 ยท owns slot 2226k1key key 1 in slot 2226: k10k2key key 2 in slot 2226: k21k3key key 3 in slot 2226: k323๐Ÿง‘โ€๐ŸซTeacherslot map updated
  1. Jo's pigeonhole 2,226 holds three items, and teachers come to Jo for them as usual.
  2. Max joins the office. Jo starts handing pigeonhole 2,226 over to him, one item at a time, and the first item goes across.
  3. A teacher asks Jo for that first item. It has already gone, so Jo says "ask Max, just this once". The teacher does, and leaves her crib sheet alone.
  4. The other two items go across too. Now pigeonhole 2,226 is Max's for good, and Jo tells the rest of the office.
  5. The next teacher who asks Jo hears "that pigeonhole is Max's now". This time she fixes her crib sheet and goes to Max from then on.
Multi-key commands and hash tagsStep 4 of 4
Jo: 0 to 5,4600123Sam: 5,461 to 10,9220123Priya: 10,923 to 16,383 ยท overloadednamkey {ana}:name (slot 14585): name0lunkey {ana}:lunch (slot 14585): lunch123๐Ÿง‘โ€๐ŸซTeacher
  1. The teacher wants to file Ana's name card and lunch card in one go. Their names sum to 3,495 and 15,648, two different pigeonholes, so Jo refuses the job.
  2. Now she writes the shared part in brackets on both labels. Only what's in the brackets goes into the sum, so both cards land in pigeonhole 14,585 with Priya.
  3. Because both cards share one pigeonhole, Priya can swap them together, all or nothing, with no one else involved.
  4. But if every label in the school had the same bracket, everything would pile into one pigeonhole and Priya would do all the work.
Lettuce in a Spring appStep 4 of 4
โ˜• Your Java app๐Ÿง‘โ€๐ŸซTeacher๐ŸƒRunnerchecks every 30s๐Ÿ™‹โ€โ™€๏ธJo๐Ÿง”Sam๐Ÿ‘ฉโ€๐ŸฆฑPriya
  1. When the app starts, the runner asks one of the staff it was told about for the full list of who has which stretch.
  2. The teacher just says "file Ana's homework". The runner does the sum, walks straight to Jo, and comes back with "done".
  3. Pigeonhole 14,585, where Ana's things are, has just moved to Priya, but the runner's list is old, so Sam says "that range is Priya's". The runner goes to Priya without bothering the teacher.
  4. Every so often the runner checks the list again anyway, so it hears about changes before it walks to the wrong desk.

๐Ÿงฉ Key questions

A teacher asks Jo for an item in a pigeonhole that's on Priya's stretch. What does Jo do?
She says "that range is Priya's", and the teacher goes to Priya and updates her crib sheet. Staff never fetch for each other. They point, so teachers learn to go to the right person first time.
Jo is halfway through handing pigeonhole 2,226 to Max and says "ask Max, just this once". What should the teacher do with her crib sheet?
Leave it alone: go to Max this once, but still try Jo first next time. Other items in that pigeonhole may still be with Jo. Only when Jo says it's Max's for good does the crib sheet change.
Why does the office refuse to swap Ana's name card and lunch card together when their names sum to different pigeonholes?
Each staff member can only do an all-or-nothing job inside one pigeonhole, and nobody coordinates between them. Putting the shared part in brackets makes both cards land in one pigeonhole, so the job is allowed.
Sam files an item, tells the teacher "done", then goes home ill before the deputy copies it. The deputy takes over. What happened to the item?
It can be lost: the deputy copies a moment after Sam says "done". That small gap is the price for staff answering quickly.
The school hires a new staff member. What changes?
Some pigeonholes are handed over to the new person; their numbers stay the same. Items always sum to the same pigeonhole. Only who looks after a pigeonhole changes, and the office chooses which ones move.