Course contentsShow
← Computer Science
Lesson 1391 of 2,87232. File SystemsFree lesson

Sequential vs Random Access

Compare sequential file access patterns with random access using seek operations and their performance implications.

Sequential vs Random Access

What you'll learn: You'll understand the two fundamental ways programs read and write file data, and why the access pattern dramatically affects performance.


The Core Idea

When your program interacts with a file, it can access data in two ways:

Sequential access means reading or writing data in order, from beginning to end—like reading a book page by page. You process byte 1, then byte 2, then byte 3, continuously.

Random access means jumping directly to any position in the file using seek operations—like flipping to page 237 of a book. You can read byte 50,000, then jump back to byte 100, then skip to byte 1,000,000.

The Performance Story

Sequential access is fast because:

  • Operating systems can prefetch upcoming data into cache
  • Storage devices (especially hard drives) avoid expensive head movements
  • Memory buffers work efficiently when data flows predictably

Random access is slower (especially on spinning disks) because:

  • Each seek operation requires repositioning the disk head
  • Cache and prefetch strategies fail when patterns are unpredictable
  • SSDs handle random access better than HDDs but still have overhead

Analogy

Think of a cassette tape (sequential) versus a DVD with scene selection (random). Rewinding or fast-forwarding a tape takes time proportional to distance. A DVD lets you jump instantly to any chapter, but even that has slight overhead compared to just watching straight through.

When to Use Which

  • Sequential: Log files, video streaming, batch processing, backups
  • Random: Databases, memory-mapped files, searching through indexed data

Key Takeaway: Sequential access reads data in order and performs better due to predictability; random access uses seek operations to jump anywhere but incurs performance penalties, especially on mechanical storage devices.