RAID: spreading data across many disks
About this lesson
A file system needs a disk that is big, fast and does not lose data, but every real disk is limited and every disk eventually fails. RAID joins several disks so the system sees one. Follow six blocks across four disks: dealt out by striping, protected by an XOR parity block, updated by a small write that costs four disk operations, rebuilt after a disk dies, and finally rearranged so the parity rotates (RAID 5). The Linux lessons on the file system and disks show how arrays look from the command line; here we look inside the layer that maps every block.
Many disks, one disk
One disk has limits: one disk's speed, one disk's size, and when it breaks its data is gone. RAID (a redundant array of inexpensive disks) joins several disks so programs see one. Here the RAID layer joins four.
RAID redundant array of inexpensive disks
(Patterson, Gibson and Katz, Berkeley, 1988)
outside looks like one disk: a row of numbered blocks
inside several disks + a layer that maps each block
Linux the md driver (Linux software RAID) joins
disks into one device, /dev/md0
here 4 disks, 2 rows each, blocks of 4096 bytesRead the lesson step by step
Many disks, one disk
One disk has limits: one disk's speed, one disk's size, and when it breaks its data is gone. RAID (a redundant array of inexpensive disks) joins several disks so programs see one. Here the RAID layer joins four.
RAID redundant array of inexpensive disks (Patterson, Gibson and Katz, Berkeley, 1988) outside looks like one disk: a row of numbered blocks inside several disks + a layer that maps each block Linux the md driver (Linux software RAID) joins disks into one device, /dev/md0 here 4 disks, 2 rows each, blocks of 4096 bytesStriping: deal blocks out
Striping (RAID 0) deals blocks out one by one. Block b goes to disk b mod 3 (remainder of b ÷ 3) and row b div 3 (whole-number division). Blocks 0-2 fill row 0; blocks 3-5 fill row 1. Disk 3 waits.
block b -> disk = b % 3, row = b // 3 block 0 disk 0 row 0 bits 0110 block 1 disk 1 row 0 bits 1100 block 2 disk 2 row 0 bits 0011 block 3 disk 0 row 1 bits 1010 block 4 disk 1 row 1 bits 0101 block 5 disk 2 row 1 bits 0001 (the figure shows 4 bits of each 4096-byte block)Faster, but fragile
Reading blocks 0, 1 and 2 keeps three disks busy at once, so a big read runs up to three times as fast. But nothing is stored twice: if any one disk dies, part of the data is simply gone.
RAID 0 with N disks (OSTEP, Figure 38.8) capacity N x B blocks all of it sequential N x S MB/s every disk at once failures 0 one dead disk = data lost here: blocks 0, 1, 2 come from disks 0, 1, 2 togetherParity: one extra disk
Disk 3 stores parity: the XOR of each row. XOR gives 1 where a column holds an odd number of 1s. Row 0: 0110 XOR 1100 XOR 0011 = 1001. Now any one lost block can be worked out.
XOR, bit by bit: odd number of 1s -> 1, even -> 0 row 0 0110 1100 0011 P0 = 1001 row 1 1010 0101 0001 P1 = 1110 every column of a row now holds an even number of 1s RAID 4: data like RAID 0 on 3 disks, parity on disk 3Line 1: a small write
Line 1 writes new bits 1001 to block 4, so row 1's parity must change too. First the RAID layer reads two old blocks: block 4 (0101) from disk 1 and parity P1 (1110) from disk 3.
line 1 pwrite(fd, buf, BS, 4 * BS); write block 4 (offset 4 x 4096 bytes) block 4 -> disk 1, row 1; its parity P1 is on disk 3 read 1 old block 4 0101 (disk 1) read 2 old P1 1110 (disk 3)New parity, two writes
New parity = old parity XOR old block XOR new block: 1110 XOR 0101 XOR 1001 = 0010. Then two writes: 1001 to block 4 and 0010 to P1. One small write cost four disk operations.
Pnew = (Cold XOR Cnew) XOR Pold (OSTEP 38.1) = (0101 XOR 1001) XOR 1110 = 0010 check: 1010 XOR 1001 XOR 0001 = 0010 write 1 block 4 = 1001 (disk 1) write 2 P1 = 0010 (disk 3) total 2 reads + 2 writes = 4 disk I/OsA disk dies
Disk 1 dies, and blocks 1 and 4 die with it. With striping alone that would be the end. With parity the array keeps running in degraded mode: one disk down, but no data lost yet.
disk 1 failed: block 1 and block 4 unreadable array degraded: still answers every read and write (Linux md marks the device faulty and carries on) RAID 4 and RAID 5 survive 1 failed disk, no more: a second failure now would lose dataLine 2: reading a lost block
Line 2 reads block 1, which was on the dead disk. The RAID layer reads the rest of row 0 and XORs it: 0110 XOR 0011 XOR 1001 = 1100. That is block 1, rebuilt on the fly.
line 2 pread(fd, buf, BS, 1 * BS); read block 1 -> disk 1, row 0: dead block 0 0110 (disk 0) block 2 0011 (disk 2) P0 1001 (disk 3) XOR 1100 = block 1, as it wasRebuilding a new disk
A new disk replaces disk 1. The RAID layer rebuilds it row by row, each lost block the XOR of the rest of its row: block 1 = 1100, block 4 = 1001. Until it finishes, a second failure would lose data.
row 0 block 1 = B0 XOR B2 XOR P0 = 0110 XOR 0011 XOR 1001 = 1100 row 1 block 4 = B3 XOR B5 XOR P1 = 1010 XOR 0001 XOR 0010 = 1001 Linux md: a hot spare is rebuilt ('recover') from the parity and the other data blocksParity disk: the bottleneck
Every small write also updates parity, and in RAID 4 all parity lives on disk 3. Writes to block 0 and block 5 use different data disks, but both need disk 3, so they must wait in line.
RAID 4 small writes at the same time: write block 0 data disk 0 parity disk 3 write block 5 data disk 2 parity disk 3 <- same disk the parity disk does 2 I/Os (read + write) per write, so random small writes run at R/2 MB/s in total, however many disks you add (OSTEP 38.6)RAID 5: rotate the parity
RAID 5 moves the parity along, row by row. Row 1's parity goes to disk 2 and its blocks shift: 3 to disk 3, 4 to disk 0, 5 to disk 1. Writes to blocks 0 and 5 now share no disk.
RAID 5 (OSTEP Figure 38.7 pattern, 4 disks) disk 0 disk 1 disk 2 disk 3 row 0 B0 B1 B2 P0 row 1 B4 B5 P1 B3 write block 0 disks 0 + 3 write block 5 disks 1 + 2 -> both at once random small writes: N/4 x R MB/s (OSTEP 38.7)Choosing a RAID level
Each level trades disks for safety and speed. With N disks, RAID 0 keeps all N for data and survives no failure. RAID 1 mirrors, keeping N/2. RAID 4 and RAID 5 keep N - 1 and survive one; RAID 5 spreads the writes.
with N disks of B blocks (OSTEP Figure 38.8) capacity survives random write RAID 0 N x B 0 N x R RAID 1 N x B / 2 1 N/2 x R RAID 4 (N-1) x B 1 R/2 RAID 5 (N-1) x B 1 N/4 x R the whole lesson as a Python simulation: from functools import reduce from operator import xor N = 4 # 3 data disks + 1 parity disk (RAID 4) def where(b): # logical block -> (disk, row) return b % (N - 1), b // (N - 1) data = [0b0110, 0b1100, 0b0011, 0b1010, 0b0101, 0b0001] disks = [[None, None] for _ in range(N)] for b, v in enumerate(data): d, r = where(b) disks[d][r] = v for r in range(2): # parity = XOR of the row disks[N - 1][r] = reduce(xor, (disks[d][r] for d in range(N - 1))) d, r = where(4) # small write: block 4 = 0b1001 old, p = disks[d][r], disks[N - 1][r] # 2 reads disks[d][r], disks[N - 1][r] = 0b1001, p ^ old ^ 0b1001 # 2 writes disks[1] = None # disk 1 dies block1 = reduce(xor, (disks[d][0] for d in (0, 2, 3))) print("block 1 =", format(block1, "04b")) print("parity =", [format(disks[3][r], "04b") for r in range(2)]) prints: block 1 = 1100 parity = ['1001', '0010']
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01An array stripes blocks over 3 disks, one block per chunk (RAID 0). Where does logical block 7 go?
Disk = 7 mod 3 = 1 and row = 7 div 3 = 2 (integer division). Blocks 6, 7 and 8 fill row 2 on disks 0, 1 and 2.
02A row holds data blocks 0110, 1100 and 0011. What is its parity block?
XOR bit by bit: each column with an odd number of 1s gives 1. 0110 XOR 1100 = 1010, and 1010 XOR 0011 = 1001.
03In RAID 4 a program overwrites one block. How many disk operations does the array perform?
New parity = old parity XOR old block XOR new block, so the array must first read the two old values and then write the two new ones: two reads and two writes.
04Disk 1 of a RAID 4 array dies. How does the array still answer a read of a block that was on disk 1?
Every row keeps an even number of 1s in each bit column, so the missing block is the XOR of everything else in its row. The array runs degraded, but it survives only one failure.
05Why does RAID 5 handle many small random writes better than RAID 4?
In RAID 4 every small write needs the one parity disk, so they queue there (R/2 in total). RAID 5 spreads parity over every disk, so small writes run in parallel, about N/4 x R in OSTEP's model.
Glossary
- RAID
- Redundant array of inexpensive disks: several disks joined by a RAID layer so the system sees one bigger, faster or safer disk. Named by Patterson, Gibson and Katz in 1988.
- Striping (RAID 0)
- Dealing consecutive blocks round-robin over the disks: block b goes to disk b mod N. Fast and uses all the space, but one failed disk loses data.
- Mirroring (RAID 1)
- Writing every block to two disks. Survives a disk failure but gives up half the space.
- Parity
- An extra block in each row holding the XOR of the row's data blocks, so any one missing block can be worked out from the others.
- XOR
- Exclusive or: bit by bit, the result is 1 when an odd number of the inputs are 1. XOR of a row together with its parity is always zero.
- Small write
- Overwriting one block of a parity array. The parity must change too: read the old block and old parity, then write both new ones.
- Degraded mode
- An array running with a failed disk. It still answers every request by rebuilding lost blocks on the fly, but one more failure would lose data.
- RAID 5
- Parity like RAID 4, but each row's parity block sits on a different disk, so no single disk takes every parity update.
Notes and sources
- Real blocks hold 4096 bytes and parity is worked out on every one of their 32768 bits. The figure shows 4 bits of each block, as OSTEP does in its own example; the C program writes those bits as the first byte, 9 (binary 1001).
- The RAID layer can be software in the kernel (the Linux md driver behind /dev/md0) or a hardware RAID card with its own processor and memory. Either way the program just reads and writes numbered blocks.
- Here one chunk is one block. Real arrays usually stripe bigger chunks of several blocks (Linux md calls this chunk_size), which changes which disk a block lands on but not the idea.
- RAID 1 (mirroring) is the other way to survive a failure: every block is written to two disks. It is simple and fast to read, but it gives up half the space. RAID 6 keeps two parity blocks per row and survives two failures.
- RAID 5 layouts differ in where each row's data starts. The figure follows the pattern of OSTEP's Figure 38.7, where data starts on the disk after the parity.
- RAID protects against a disk dying, not against mistakes: a deleted or overwritten file is deleted on every disk at once. It is not a backup.
Quiz Unlocks after the last step.