Storage Principles
Computer storage is the part of a computing system that retains instructions, intermediate results, and persistent data. Its design is governed by a fundamental trade-off among speed, capacity, and cost. Fast memory is expensive and limited in size, while inexpensive storage can hold much more data but responds more slowly. Most modern systems address this trade-off by combining several storage technologies into a hierarchy.
A typical computer moves data through multiple levels of memory:
- CPU registers hold small quantities of values that the processor is actively using. They are built inside the CPU and have the shortest access times, but they are also the most expensive per bit.
- Cache memory bridges the gap between the processor and main memory. L1 cache is usually located directly within the CPU, while L2 and L3 caches provide larger, slightly slower storage. Cache memory generally uses SRAM and is designed to exploit the principle of locality, meaning that programs often access a relatively small region of data repeatedly.
- Main memory, commonly called RAM, supplies the working space for running programs. It is slower than cache but much faster than disks or SSDs. RAM is volatile, so its contents disappear when power is removed.
- Secondary storage includes SSDs, HDDs, optical media, and other persistent devices. It retains data without continuous power and offers far greater capacity than main memory.
The operating system and hardware attempt to place frequently needed data near the top of this hierarchy. A successful data-access pattern can therefore be almost invisible to the application, while an inefficient one may repeatedly trigger expensive transfers from slower media.
How RAM Stores Data
RAM allows any selected location to be accessed directly rather than requiring the system to search through a sequence, which is what “random access” means in this context. Two major technologies are widely used.
Static RAM
SRAM stores each bit in a small latch made from several transistors. As long as power remains available, the latch preserves its state without regular refreshing. SRAM is fast, but its relatively complex cell requires more transistors and more silicon area than DRAM.
Because of its speed, SRAM is commonly used for CPU registers and cache memory.
Dynamic RAM
DRAM stores each bit using a transistor and a tiny capacitor. The presence or absence of electrical charge represents a binary value. Capacitors naturally leak charge, so all memory cells must be periodically refreshed to restore their stored values.
DRAM offers a much higher density and a lower cost per bit than SRAM, making it suitable for large main-memory modules. However, its refresh requirements introduce additional overhead and make DRAM somewhat slower than cache memory.
ROM and Flash Memory
Read-only memory is used for data that must remain available when a device is powered off. Traditional ROM has a fixed or rarely modified contents, while modern firmware systems often rely on rewritable flash memory. A computer’s BIOS or UEFI firmware is a typical example.
Flash memory is nonvolatile and can be electrically erased and rewritten, but each rewrite consumes part of the cell’s useful life. This limitation is important for firmware updates, embedded devices, and flash-based SSDs.
Magnetic and Solid-State Storage
Hard Disk Drives
An HDD stores information through magnetic recording. Its main components include rotating platters, read/write heads, and an actuator arm. During writing, the head changes the magnetic orientation of small regions on a platter to encode binary data. During reading, it detects changes in the magnetic field and converts those signals into electrical data.
The mechanical nature of an HDD introduces physical delays. Access time includes the time required to position the head over the correct track, wait for the desired sector to rotate beneath it, and transfer the data. Spinning, seeking, and queuing can therefore dominate the response time of an application that performs many small reads.
HDDs remain useful for large-scale, sequential workloads because they provide high capacity at a relatively low cost. Backup systems, archives, and cold storage are common examples.
Solid-State Drives
An SSD has no moving parts. Instead, it stores data in NAND flash cells connected to a controller. In many types of NAND flash, a floating-gate transistor holds the cell’s state by retaining a controlled number of electrons. The controller reads these states and presents the SSD as a conventional block device.
Flash memory is organized into pages and larger erase blocks. A page can often be read and programmed quickly, but erasing a block before rewriting it requires a more involved operation. The SSD controller manages these differences through techniques such as wear leveling, garbage collection, overprovisioning, and error correction.
SSDs generally provide much lower latency, higher random-access performance, greater shock resistance, and lower power consumption than HDDs. Their limitations include finite write endurance and higher cost per unit of storage. Excessive writes can eventually wear out a cell, although modern controllers distribute writes to extend the usable lifetime of the device.
Addressing and Data Layout
Storage systems require a consistent method for identifying where data resides.
Byte-Addressed Memory
Main memory is usually byte-addressed. Each address refers to one byte, consisting of eight bits. Programs access data through logical addresses rather than physical locations. The processor’s memory management unit, or MMU, uses page tables to translate a virtual address into a physical address.
This translation provides several benefits:
- Each process receives its own protected address space.
- Programs are isolated from unauthorized access to one another’s memory.
- Physical memory can be allocated dynamically.
- Data that is not currently needed can be moved to secondary storage.
Block-Oriented Devices
SSDs and HDDs are usually accessed in blocks, such as 512-byte or 4 KiB units. Even when an application requests only a small amount of data, the storage device may transfer an entire block. Efficient software therefore benefits from batching reads and writes and avoiding unnecessary random I/O.
The file system adds another layer of organization by mapping files and directories onto device blocks. Metadata, journals, and allocation structures help the system track file contents, recover from interrupted operations, and maintain consistency after a crash.
Virtual Memory and Swapping
Virtual memory allows a process to use a logical address space larger than the physical RAM currently installed. When a required page is not in memory, the operating system may load it from storage. If no physical page is available, it can evict a less recently used page and write it to a swap area.
Paging provides protection and flexibility, but it does not eliminate the cost of storage access. A page fault caused by swapping can be dramatically slower than a normal memory read. Programs with good locality and predictable working sets usually experience better performance because they require fewer page faults and less disk activity.
Implications for Software Design
Understanding storage helps explain why application architecture has a major effect on performance.
Keep hot data close to the CPU. Reusing recently accessed objects reduces cache misses and improves locality. Compact data structures, appropriate object lifetimes, and careful allocation patterns can all contribute to this goal.
Minimize unnecessary I/O. Reading and writing large, sequential chunks is usually more efficient than issuing many tiny operations. Batching requests, using suitable buffer sizes, and avoiding repeated writes can reduce both latency and wear on SSDs.
Measure the complete access path. A database operation may involve cache lookups, memory copies, file-system metadata, device reads, and write-ahead logging. Profiling the full path reveals bottlenecks that a focus on one component may miss.
Distinguish persistence from durability. Keeping data in RAM makes it fast but does not guarantee survival after a power failure. Durable systems require data to be copied to persistent storage and, where necessary, protected with checksums, replication, or transactional commit protocols.
Storage performance is not determined by a single device speed. It emerges from the interaction among hardware, the memory hierarchy, the file system, and the workload. Effective systems place each kind of data where its access frequency, lifetime, and durability requirements allow it to be handled with the least overhead.