Cache and Buffer Management: CPU, OS, and Database Layers

Separate CPU cache coherence from operating-system page caching and database buffers, with a MESI example and file read/write paths.

published: reading time: 7 min read author: Geek Workbench updated: November 8, 2026
Quick Summary

CPU caches, operating-system page caches, and database buffers all keep data close to where it is used, but they solve different problems. This guide separates the layers, walks through a simplified MESI write and Linux file I/O paths, and explains how PostgreSQL buffers relate to the OS page cache.

Cache and Buffer Management: CPU, OS, and Database Layers

“Cache” and “buffer” can refer to several different layers. A CPU cache keeps memory lines near a processor core; the operating system page cache keeps file data in RAM; a database buffer pool keeps database pages available to the database engine. They all reduce the cost of waiting for slower storage, but their units, owners, and consistency rules differ.

Which layer do you mean?

Layer Data unit Managed by Main question
CPU cache Cache line Processor hardware How do cores coordinate reads and writes to shared memory?
Operating-system page cache File page or folio Kernel Can a file read or write use data already held in RAM?
Database buffer pool Database page Database engine Is the page already in the database’s own buffer cache?

This article covers the CPU-coherence and file/database paths separately. For an application cache, eviction choices, or refresh coordination, see Cache Patterns, Cache Eviction Policies, and Cache Stampede Prevention.

CPU cache coherence: a simplified MESI example

Processors keep recently used memory in small caches. When multiple cores hold the same cache line, a coherence protocol coordinates writes so one core does not keep using a stale copy. MESI names four line states: Modified, Exclusive, Shared, and Invalid. This example follows the basic state idea; real processor protocols and transitions can add states or differ in their implementation.

Suppose Core A and Core B both read the same line. Each can hold a clean copy in the Shared state. If Core A wants to write, it asks for ownership. The coherence mechanism invalidates Core B’s copy; once ownership is granted, Core A can write the line in Modified state. In that state, Core A holds the current value and memory may still have the older value. A later request from another core must be served through the coherence protocol.

sequenceDiagram
    participant A as Core A cache
    participant H as Coherence mechanism
    participant B as Core B cache
    A->>H: Read line X
    H-->>A: Shared copy
    B->>H: Read line X
    H-->>B: Shared copy
    A->>H: Request write ownership
    H->>B: Invalidate line X
    B-->>H: Invalidation acknowledged
    H-->>A: Grant exclusive ownership
    A->>A: Write line X; state becomes Modified

A practical consequence is false sharing. Two threads can update unrelated variables that happen to occupy the same cache line. The line then moves between cores as each writes. Keep independently written, heavily contended data apart when profiling shows this is a bottleneck; padding every data structure can waste memory and should not be done by default.

For Intel 64 and IA-32, see the Intel Software Developer’s Manual. MESI is an architecture-specific coherence model, not a description of every processor’s internal protocol.

Operating-system page cache

On Linux, ordinary file reads, writes, and memory mappings use the page cache. A read can be served from a page already in RAM; on a miss, the kernel obtains the file data from storage and makes it available through the page cache. The kernel documentation describes this path in its page cache overview.

Writes commonly update cached file pages and mark them dirty. The kernel can write dirty data back later. Applications that need a durability boundary use synchronization operations such as fsync, but the exact guarantees depend on the filesystem, device, and how those layers handle flush requests. A successful buffered write alone should not be treated as proof that data has reached persistent media.

flowchart TD
    R[Application read] --> L{Page in OS cache?}
    L -->|Yes| RH[Copy or map cached data]
    L -->|No| S[Read file data from storage]
    S --> P[Place data in page cache]
    P --> RH

    W[Application write] --> C[Modify cached file page]
    C --> D[Mark page dirty]
    D --> B[Kernel writeback]
    B --> S2[Filesystem and storage]
    D --> F[Application requests synchronization]
    F --> B

The page cache is not the same thing as the CPU cache. The CPU may cache the memory holding a page-cache page, while the kernel manages whether that page represents file data and when it is written back.

Database buffer pool and the page cache

A database buffer pool is managed by the database engine. PostgreSQL, for example, uses shared_buffers for database pages shared by server processes. If the requested page is not in that pool, PostgreSQL reads it through the operating system’s file I/O path. With ordinary buffered I/O, the OS page cache may hold file data too, so the same database content can be represented at both layers.

The two caches have different jobs: PostgreSQL manages database pages and tracks database-level state, while the operating system manages file-backed memory and storage I/O. A buffer-pool hit does not imply that data was fetched from disk on the last miss; the OS page cache may have satisfied the file read. Conversely, database statistics and OS statistics measure different things.

PostgreSQL’s resource-consumption documentation describes shared_buffers; the pg_buffercache documentation explains how to inspect buffer-cache state. Consult the docs for the PostgreSQL release you run because available settings and views can change.

Read and write path at a glance

Event Database buffer pool Operating-system page cache Persistent storage
Database page already buffered Serves the page from its own buffer May not need a file read No read for this request
Page absent from database buffers Requests the page from its file path May serve cached file data or obtain it from storage Read occurs if lower layers cannot satisfy it
Database modifies a page Tracks the modified, or dirty, page File I/O and writeback depend on the database and OS path Data-file write can happen later under database control

Database durability also involves the database’s logging and synchronization rules. Buffering data pages is not the same as committing a transaction. For PostgreSQL, use its WAL reliability documentation when reasoning about write ordering and durability.

What to measure

Choose metrics that match the layer you are investigating:

  • CPU cache and coherence: profile cache misses and cache-line contention with tools appropriate to the processor and operating system. A high miss count alone does not prove false sharing.
  • OS page cache: compare file I/O, memory pressure, dirty-page activity, and storage latency. Linux exposes virtual-memory counters through /proc/vmstat; their meanings are documented by the kernel and can vary by kernel version.
  • Database buffers: inspect database cache statistics, read activity, dirty buffers, and query latency. For PostgreSQL, pg_buffercache provides a view of shared-buffer contents but is an inspection tool, not a cache-tuning target by itself.

Use a representative workload and change one relevant setting at a time. If the database is waiting on locks or CPU, increasing a cache may not help. If storage reads dominate and the workload repeatedly touches the same pages, then test a buffer or memory change against the same query mix.

Further Reading

Category

Related Posts

ASLR & Stack Protection

Explore ASLR, PIE, stack canaries, and NX on Linux with GCC, then inspect a harmless C program to see what each mitigation does and where it falls short.

#operating-systems #aslr-stack-protection #computer-science

Assembly Language Basics: Writing Code the CPU Understands

Learn x86-64 Linux assembly with the System V ABI: track registers, assemble and run a small program, debug it in GDB, then compare AArch64.

#operating-systems #assembly-language-basics #computer-science

Boolean Logic & Gates

Understanding AND, OR, NOT gates and how they combine into arithmetic logic units — the building blocks of every processor.

#operating-systems #boolean-logic-gates #computer-science