RAID Levels: Strength in Numbers

[!NOTE] What is RAID? Redundant Array of Independent Disks. The OS sees one giant logic volume /dev/md0, but behind the scenes, a controller distributes data across multiple physical disks.


1. The Building Blocks

A. Striping (Performance)

Splitting a file into chunks (e.g., 64KB) and spreading them across disks.

  • Benefit: Parallelism. If you have 2 disks, you can read/write 2x faster.

B. Mirroring (Redundancy)

Writing the exact same data to two or more disks.

  • Benefit: If one disk fails, the other takes over instantly.

C. Parity (Efficiency)

Using math (XOR) to store recovery information instead of a full copy.

  • Formula: P = D1 XOR D2 XOR D3
  • Recovery: D2 = D1 XOR P XOR D3

2. Standard RAID Levels

RAID 0 (Striping)

  • Capacity: 100% (Sum of all disks).
  • Performance: Excellent (Read/Write).
  • Fault Tolerance: Zero. If one disk dies, ALL data is lost.

RAID 1 (Mirroring)

  • Capacity: 50% (Given 2 disks).
  • Performance: Good Read (Parallel), Single Write speed.
  • Fault Tolerance: Excellent. Can lose 1 disk.

RAID 5 (Distributed Parity)

  • Capacity: (N-1) * Size.
  • Performance: Good Read, Slow Write (Read-Modify-Write penalty for Parity).
  • Fault Tolerance: Can lose 1 disk.
  • The Write Hole: If power fails during a write, parity might not match data.

RAID 10 (Stripe of Mirrors)

  • Capacity: 50%.
  • Performance: Excellent Read/Write.
  • Fault Tolerance: Can lose 1 disk per mirror pair.
  • Cost: Expensive (Needs 4+ disks).

3. Interactive: RAID Simulator

Toggle a RAID level and simulate a disk failure.

System Healthy.

4. Code Example: Calculating Parity (XOR)

How does RAID 5 actually work? It uses the XOR property: A ^ B = P A ^ P = B B ^ P = A

```go package main import "fmt" func main() { // Two data chunks (bytes) disk1 := byte(0b11001100) // Data A disk2 := byte(0b10101010) // Data B // Calculate Parity (RAID Controller does this) parity := disk1 ^ disk2 fmt.Printf("Disk 1: %08b\n", disk1) fmt.Printf("Disk 2: %08b\n", disk2) fmt.Printf("Parity: %08b\n", parity) // --- DISASTER: DISK 2 EXPLODES --- fmt.Println("\n--- DISK 2 FAILED ---") // Recover Disk 2 using Disk 1 and Parity recoveredDisk2 := disk1 ^ parity fmt.Printf("Recovered: %08b\n", recoveredDisk2) if recoveredDisk2 == disk2 { fmt.Println("Recovery Successful!") } } ```