What Happens When You Save
When you copy items from RAM to a storage device, the operating system coordinates a multi-step process that moves data from volatile memory to persistent storage. This involves application requests, system calls, file system logic, block device drivers, and physical write operations. The flow ensures data integrity, handles addressing and buffering, and manages performance trade-offs. Understanding this sequence clarifies how files are saved, updated, or synced, and where delays or bottlenecks can appear in the storage stack.
The Storage Stack and Key Components
Data movement from RAM to storage traverses a layered software and hardware stack. Each layer adds metadata, scheduling, and error handling. Components include the application, C standard library, system call interface, file system, page cache, block I/O layer, storage driver, and the physical medium. The stack abstracts complexity while allowing optimization and control at each level.
System Call Interface
Applications initiate transfers via system calls such as write, pwrite, or fsync. These calls switch CPU mode to kernel space and provide the destination file descriptor, memory location, byte count, and desired offset. The kernel validates arguments, permissions, and available space before proceeding to lower-level operations.
File System Responsibilities
The file system maps files to storage blocks, maintains metadata like inodes and directories, and enforces consistency rules. It decides where data lands on disk, handles journaling or copy-on-write techniques, and coordinates ordering to meet durability and performance goals. Metadata updates may occur immediately or be deferred depending on the file system’s design.
Memory Management and Buffering
Because disk I/O is slower than CPU and memory, the system uses buffering to absorb writes. The page cache holds recently accessed pages and serves as a staging area for writes. Writeback policies determine when dirty pages are flushed, balancing latency, throughput, and risk of data loss in power failures or crashes.
Page Cache Behavior
- Dirty pages: Modified pages marked for eventual write-back to storage.
- Writeback throttling: Kernel may limit flush rates to avoid saturating the storage bus.
- Direct I/O: Bypasses page cache for workloads that manage their own buffering.
From Filesystem to Block Layer
Once the file system decides which blocks to write, requests move to the block I/O layer. This layer merges adjacent requests, assigns priority, and schedules queues to reduce seek time and maximize throughput. I/O schedulers use algorithms to reorder and batch operations for the underlying device’s characteristics.
Block Layer Workflow
- Request submitted from file system to block layer.
- Request merged or split to match device sector sizes.
- I/O scheduler orders requests to optimize device performance.
- Request passed to device driver for execution.
Device Drivers and Hardware Execution
The storage driver translates block-layer requests into commands the hardware understands, such as SCSI, NVMe, or SATA. It handles command queuing, timeouts, and error recovery. On the device, firmware maps logical block addresses to physical pages, applies wear-leveling for flash media, and performs garbage collection and error correction. Completion is signaled back to the operating system.
Storage Technologies and Timing
| Attribute | Verified Detail | Source Type |
|---|---|---|
| Typical write latency (HDD) | 4–10 ms | Manufacturer specifications |
| Typical write latency (SATA SSD) | 0.1–0.5 ms | Manufacturer specifications |
| Typical write latency (NVMe SSD) | 0.03–0.1 ms | Manufacturer specifications |
| Endurance (consumer SSD) | 300–600 TBW | Vendor published ratings |
Ensuring Data Integrity and Consistency
To reduce the risk of corruption, file systems use journaling, checksums, and barriers. A journal records intent before changes are applied, enabling recovery after crashes. Checksums detect silent corruption, while write barriers enforce ordering of metadata and data writes. Applications can also request synchronous writes via fsync or similar calls to guarantee on-disk persistence.
Performance Considerations and Tuning
Latency and throughput depend on hardware, file system choice, and workload patterns. Larger, sequential writes typically perform better than small random writes. Storage queue depth, controller settings, and partition alignment also influence results. System parameters like dirty_ratio, dirty_background_ratio, and vm.dirty_expire_centisecs allow administrators to tune write-back behavior for their reliability and throughput requirements.
Common Pitfalls and Misconceptions
Because writes are often buffered, applications may believe data is persistent before it reaches the storage medium. Without explicit synchronization, power loss or sudden shutdown can result in data loss or file system inconsistency. Additionally, filling a storage device beyond its over-provisioning and spare areas can degrade performance and increase write amplification. Understanding when copies are in memory versus safely stored is essential for robust software and system design.