Sub topic 9
RAID in Brief
RAID (Redundant Array of Independent Disks) combines several physical disks into one logical volume. The goals are higher throughput, protection against disk failure, or both. This chapter assumes you know how a disk is organized (platters, tracks, sectors) and how access time is computed. It covers the levels that GATE CS questions use: 0, 1, 2 to 6 at a glance, and 10.
Two ideas behind every level
Every RAID level is built from some mix of two techniques.
- Striping splits data into fixed-size chunks (stripe units) and places consecutive chunks on different disks in round-robin order. Several disks can then serve one large request in parallel, or serve several small requests at once.
- Redundancy stores extra information so the data survives a failure. The extra information is either a full copy (mirroring) or a computed value (parity).
Parity is usually the bitwise XOR of the data chunks in a stripe. If one chunk is lost, XOR of the surviving chunks and the parity chunk reproduces it.
from functools import reduce
def parity(chunks):
return reduce(lambda a, b: a ^ b, chunks)
d0, d1, d2 = 0b1010, 0b0110, 0b1100
p = parity([d0, d1, d2]) # 0b0000
rebuilt_d1 = parity([d0, d2, p])
print(bin(p), bin(rebuilt_d1)) # 0b0 0b110
assert rebuilt_d1 == d1The main levels
RAID 0: striping, no redundancy
Data is striped across all N disks. Capacity is N disks and read and write bandwidth scale with N. If any one disk fails, the whole volume is lost, so the failure probability is higher than for a single disk.
RAID 1: mirroring
Every block is written to two (or more) disks. Usable capacity is N/2 for two-way mirroring. Reads can be served by either copy, so read throughput can double. Writes must go to both copies, so write throughput matches a single disk. It survives one disk failure, and can survive more if the failures hit different mirror pairs.
RAID 2, 3 and 4
- RAID 2 uses bit-level striping with Hamming-code error correction. It is rarely used in practice.
- RAID 3 uses byte-level striping with one dedicated parity disk.
- RAID 4 uses block-level striping with one dedicated parity disk. Small reads are fast because they touch one disk. Every write must update the parity disk, so that disk becomes a bottleneck.
RAID 5: distributed parity
RAID 5 is block-level striping where the parity block rotates across all disks. This removes the single parity-disk bottleneck of RAID 4. Usable capacity is N − 1 disks. It survives one disk failure.
RAID 6: double parity
RAID 6 stores two independent parity blocks per stripe, so it survives two simultaneous disk failures. Usable capacity is N − 2 disks. The second parity uses a different code than plain XOR, so computing it costs more CPU.
RAID 10 (1+0): striped mirrors
RAID 10 mirrors disks in pairs and then stripes across the pairs. Usable capacity is N/2. It combines the speed of striping with the simple recovery of mirroring. It needs at least four disks.
Capacity and fault tolerance at a glance
| Level | Technique | Usable capacity (N disks) | Failures tolerated (guaranteed) |
|---|---|---|---|
| 0 | Striping | N | 0 |
| 1 | Mirroring | N/2 | 1 |
| 4 | Striping + dedicated parity | N − 1 | 1 |
| 5 | Striping + distributed parity | N − 1 | 1 |
| 6 | Striping + double parity | N − 2 | 2 |
| 10 | Striped mirrors | N/2 | 1 |
def usable_capacity(level, n, disk_tb):
if level == 0:
return n * disk_tb
if level in (1, 10):
return n // 2 * disk_tb
if level in (4, 5):
return (n - 1) * disk_tb
if level == 6:
return (n - 2) * disk_tb
raise ValueError("unsupported level")
for lvl in (0, 1, 5, 6, 10):
print(lvl, usable_capacity(lvl, 6, 2))
# 0 12
# 1 6
# 5 10
# 6 8
# 10 6The small-write problem in parity RAID
A write to a single block in RAID 4 or 5 cannot simply overwrite that block, because the parity would become stale. The controller performs a read-modify-write:
- Read the old data block.
- Read the old parity block.
- Compute new parity = old parity XOR old data XOR new data.
- Write the new data block.
- Write the new parity block.
That is 2 reads and 2 writes for one logical write, so four disk operations. Mirroring needs only two writes. This is why RAID 5 is weak for write-heavy random workloads, and why RAID 10 is preferred for databases with many small writes.
Failure, rebuild and the write hole
When a disk fails, the array runs in degraded mode. In RAID 5, every read of a block on the failed disk requires reading all the surviving disks and XOR-ing them, so performance drops. A rebuild onto a replacement disk reads every surviving disk in full. During the rebuild, a second failure in RAID 5 loses the volume. This is the main reason RAID 6 is favored for large disks.
The write hole is a consistency problem. If power fails after the data block is written but before the parity block is updated, the stripe is inconsistent. A later rebuild would then compute wrong data. Controllers use battery-backed caches or journals to close the hole. This connects to the journaling chapter later in this tutorial.
Common mistakes
- RAID is not a backup. Mirroring copies deletions, corruption and ransomware encryption to every disk.
- RAID 10 does not guarantee surviving two failures. It survives them only if they hit different mirror pairs.
- RAID 5 reads are fast, but its small writes cost four I/Os.
- Capacity formulas assume equal disk sizes. Mixed sizes are limited by the smallest disk.
Worked example
You have 8 disks of 4 TB each. Compare the options.
- RAID 0: 32 TB usable, no fault tolerance.
- RAID 5: 7 × 4 = 28 TB, survives 1 failure.
- RAID 6: 6 × 4 = 24 TB, survives 2 failures.
- RAID 10: 4 × 4 = 16 TB, survives 1 guaranteed, up to 4 in the best case.
For a read-mostly archive, RAID 6 gives a good balance of capacity and safety. For a write-heavy database, RAID 10 gives the best random write performance.
Exercise
A RAID 5 array has 5 disks. A program writes one 4 KB block that lies on a single disk. How many disk I/Os does the controller perform, and why? What is the usable capacity if each disk is 1 TB?
Show answer
Four I/Os: read the old data, read the old parity, write the new data, write the new parity. The new parity is computed as old parity XOR old data XOR new data, so the other disks in the stripe need not be read. Usable capacity is (5 − 1) × 1 TB = 4 TB.
Exercise
A RAID 10 array has 6 disks arranged as 3 mirror pairs. What is the largest number of disk failures it can survive, and what is the number it is guaranteed to survive?
Show answer
At most 3 failures, if each hits a different mirror pair. Only 1 is guaranteed, because a second failure could hit the partner of an already failed disk and destroy that pair's data.