Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 79 ยท Storage and Indexing

RAID Levels: Striping, Mirroring and Parity

Learn how RAID combines disks for speed and fault tolerance: striping, mirroring, XOR parity, RAID 0/1/5/6/10 compared, and a live parity-rebuild simulation in Python.

A database lives on disks, and disks fail. A single drive may run for years, but a server with twelve of them will lose one sooner or later, and a lost drive can mean lost data and hours of downtime. Disks are also the slowest part of the machine, so even healthy ones can limit how many transactions per second the database can serve.

RAID (Redundant Array of Independent Disks) attacks both problems at once. It combines several physical disks into one logical volume that the operating system and the DBMS see as a single big disk. Depending on the layout, the array can be faster than one disk, survive disk failures, or both. This lesson builds on disk storage and pages: we now look at what sits under the page, and why database administrators care which RAID level holds the data files and which holds the log.

Three building blocks

Striping Split data across disks for speed.
Mirroring Keep a full copy on another disk.
Parity Store a checksum that rebuilds a lost disk.

Every RAID level mixes these three.

1 / 2

Striping

Striping cuts the data into fixed-size chunks (stripe units, often 64 KB to 1 MB) and deals them out to the disks in turn, like cards. Chunk 0 goes to disk 0, chunk 1 to disk 1, and so on, then back to disk 0. A row of chunks, one per disk, is called a stripe.

The payoff is parallelism. A large sequential read, such as a table scan, is served by all disks at the same moment, so throughput grows roughly with the number of disks. Many small random requests also spread over the disks, so more of them run in parallel. Striping alone adds no protection: the array has no redundancy, and if any one disk dies, every large file loses chunks.

Mirroring

Mirroring writes every block to two (or more) disks. Reads can be served by either copy, so read throughput improves and the array survives a disk failure: the mirror partner still has everything. The cost is capacity. Two disks give the space of one, so 50% of what you bought is overhead. Mirroring is simple, has no calculations to perform, and recovers quickly, since a replacement disk is filled by copying its partner.

Parity

Parity is a cheaper kind of redundancy. For each stripe the array stores one extra chunk, the XOR of all the data chunks in that stripe. XOR (exclusive or) has a useful property: if you XOR everything except one value with the parity, you get the missing value back.

TEXT
data:    D0 = 1010   D1 = 0110   D2 = 1100
parity:  P  = D0 xor D1 xor D2 = 0000

disk 1 is lost:  D1 = D0 xor D2 xor P = 1010 xor 1100 xor 0000 = 0110

One parity chunk protects a whole stripe of any width, so the overhead is one disk's worth of space no matter how many disks the array has. The price is computation and extra I/O on writes, as the write-penalty section below shows.

The RAID levels

The numbered levels name the common combinations. Levels 2, 3 and 4 are mainly of historical interest, but you will meet them in textbooks.

Level Layout Usable capacity (n disks of size S) Disk failures survived Notes
RAID 0 Striping only n x S 0 Fastest, any failure destroys everything
RAID 1 Mirroring (n / 2) x S for pairs 1 per mirror pair Simple and fast reads
RAID 2 Bit-level striping with Hamming-code error correction less than (n - 1) x S 1 Obsolete, disks now correct errors internally
RAID 3 Byte-level striping, one dedicated parity disk (n - 1) x S 1 Parity disk is a bottleneck for small writes
RAID 4 Block-level striping, one dedicated parity disk (n - 1) x S 1 Parity disk is written on every write
RAID 5 Block-level striping, parity spread over all disks (n - 1) x S 1 Needs at least 3 disks
RAID 6 Striping with two independent parities (n - 2) x S 2 Needs at least 4 disks
RAID 10 (1+0) Stripe across mirrored pairs (n / 2) x S at least 1, up to n / 2 if each pair keeps one disk Needs at least 4 disks

RAID 5 fixes the weakness of RAID 4 by rotating the parity chunk to a different disk in each stripe, so no single disk takes all parity writes. RAID 6 stores two different parities (typically XOR plus a Reed-Solomon code), which lets it recover from any two simultaneous disk failures. In RAID 10 you first mirror pairs of disks and then stripe across the pairs, so you get RAID 1's fast rebuilds and RAID 0's speed. RAID 0+1 (a mirror of two stripes) looks similar but is weaker: after one failure, a whole stripe set is gone, and a second failure on the other side loses the data.

flowchart LR
  S0["Stripe 0: D0, D1, D2 and P0 on disk 3"]
  S1["Stripe 1: D3, D4, D5 and P1 on disk 2"]
  S2["Stripe 2: D6, D7, D8 and P2 on disk 1"]
  S0 --> S1 --> S2

In RAID 5 each stripe has one parity chunk (P), stored on a different disk each time.

Parity in action

The simulation below builds a RAID 5 array of four disks in Python. It stripes twelve data blocks, rotates the parity, "loses" one disk and rebuilds its contents from the survivors. Each block is a single integer, which keeps the XOR visible.

Python RAID 5: rotating parity and rebuilding a failed disk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that rebuilding never needs the failed disk: every missing block is the XOR of the same stripe's surviving blocks. Also note the last step. To update one data block you do not have to read the whole stripe. You read the old data and old parity, compute the new parity with two XORs, and write both back. That is four disk operations for one logical write.

The write penalty

Each logical write turns into several physical I/Os, the write penalty:

  • RAID 0: 1 write.
  • RAID 1 and RAID 10: 2 writes (one per copy), issued in parallel.
  • RAID 5: 4 I/Os (read old data, read old parity, write new data, write new parity).
  • RAID 6: 6 I/Os (two parities to read and write).

Big sequential writes avoid most of the penalty, because the array can compute parity for a full stripe at once without reading anything. Small random writes, the typical pattern of an OLTP database, suffer the most. That is the main reason many database vendors recommend RAID 10 for data files that take heavy updates, and why RAID 5 or 6 tends to suit read-mostly or archival data such as a warehouse.

The next example puts the capacity and fault-tolerance rules into a small query, so you can compare layouts for a given number of disks.

SQL Comparing RAID levels for a set of disks

Press Run (or Ctrl + Enter) to see the result here.

With eight 8 TB disks, RAID 5 offers 56 TB usable and RAID 6 offers 48 TB, while RAID 10 offers 32 TB. In exchange for 16 TB, RAID 10 gets the lowest write penalty and fast rebuilds.

Rebuilds, hot spares and limits

When a disk fails, the array runs degraded: reads of the missing data are reconstructed on the fly, which is slow, and in RAID 5 a second failure now means total loss. A hot spare is an idle disk that the controller switches in automatically to start the rebuild at once. A rebuild reads every surviving disk completely, which takes many hours for large drives and puts heavy load on them while they are already aging. Two risks follow:

  • An unrecoverable read error on a surviving disk during the rebuild can make some data unreadable. This is why RAID 6, or RAID 10 with its much shorter copy-based rebuild, is preferred for very large disks.
  • The write hole: if power fails between writing data and writing parity, parity no longer matches. Hardware controllers use a battery- or flash-backed cache and software arrays use a journal to close the hole.

RAID can be hardware (a controller card), software (Linux mdadm, Windows Storage Spaces, ZFS's RAID-Z) or done by a cloud provider's storage layer. Whatever the form, remember three points. RAID protects against disk failure, not against deleting a table by mistake, ransomware, bugs, or a fire in the server room, so it is never a replacement for backups. A database log is written sequentially and must be durable at commit, so a small mirrored volume (RAID 1 or 10) with a protected write cache is a common home for it. And a copy of the data on another machine, as in replication, protects against failures that RAID cannot.

Nested levels: RAID 50 and RAID 60

Levels can be nested. RAID 50 (5+0) stripes data across two or more RAID 5 groups, each with its own rotating parity. It survives one failed disk per group, rebuilds faster than one big RAID 5 and writes faster, but needs at least 6 disks and loses data if two disks in the same group fail. RAID 60 (6+0) does the same with RAID 6 groups: it tolerates two failures per group, needs at least 8 disks, and spends two disks per group on parity. Capacity is the total minus one (RAID 50) or two (RAID 60) disks per group. Compare RAID 10, which gives up half of the capacity for the fastest rebuilds.

Hardware, software and firmware RAID

A hardware RAID controller card computes parity itself and usually has a battery- or flash-backed write cache, so the host CPU is not involved and committed writes survive a power cut. Software RAID (Linux md, Windows Storage Spaces, ZFS) uses the host CPU, costs nothing and is flexible. Firmware ("fake") RAID, found on many motherboards, does part of the work in a chip and the rest in an operating-system driver: it has the drawbacks of both and is rarely a good choice for a database server.

Storage Area Networks

RAID decides how disks are combined; a storage area network (SAN) decides how they are connected. A SAN is a dedicated high-speed network that offers block-level storage to many servers: each server sees a SAN volume as if it were a local disk, so a database can format and manage it itself. This differs from NAS (network-attached storage), which shares files over a network. Common SAN technologies are Fibre Channel (FC, a dedicated fabric with the FCP protocol), iSCSI (SCSI commands over ordinary IP networks), FCoE (Fibre Channel frames over Ethernet) and NVMe-oF (NVMe over a network fabric, for the lowest latency).

  • Advantages: storage is shared and centrally managed (snapshots, replication, backups), capacity can be added without touching servers, and a failed server's volumes can be mounted by another one for fast failover.
  • Disadvantages: high cost and specialist skills, a more complex network that can become a bottleneck or single point of failure, and extra latency compared with a local disk.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does a RAID 5 array rebuild the contents of a failed disk?

    Parity is the XOR of the data blocks in a stripe, so XORing all the surviving blocks of the stripe reproduces the missing one.
  2. 2. Which level gives the most usable capacity from 6 equal disks while surviving a single disk failure?

    RAID 5 keeps (n - 1) disks of data, so 5 disks of the 6. RAID 10 and RAID 1 keep only half, and RAID 0 survives no failures.
  3. 3. Why does a small random write cost four I/Os on RAID 5?

    New parity equals old parity xor old data xor new data, so the controller must read two blocks and write two blocks.
  4. 4. True or false: with RAID 1 you no longer need backups.

    Redundancy within an array does not protect against logical errors, malware or site disasters. Backups and replicas do.

Summary

  • RAID presents several disks as one volume, built from striping (speed), mirroring (a full copy) and parity (a checksum that rebuilds a lost disk).
  • RAID 0 is fast with no protection; RAID 1 mirrors; RAID 5 stripes with rotating single parity; RAID 6 has double parity; RAID 10 stripes over mirrored pairs.
  • Parity is the XOR of a stripe's data blocks; any one missing block is the XOR of the rest.
  • Small writes cost 2 I/Os on mirrors, 4 on RAID 5 and 6 on RAID 6, so write-heavy databases usually prefer RAID 10.
  • Large disks make rebuilds long and risky; hot spares, RAID 6 and RAID 10 reduce the danger.
  • RAID is not a backup: it protects against disk failure only.

Next: File Organization.