← Back

DRAM

2 min read

DRAM (Dynamic Random-Access Memory) is the ultra-fast, volatile main system memory attached directly to the CPU on cloud servers.


What DRAM Is


How DRAM Is Accessed in Cloud Environments (e.g., AWS EC2)

Unlike NVMe SSDs or EBS network drives — which require operating system file descriptors and POSIX I/O drivers (pread/pwrite) — DRAM does not go through a disk or block driver:

  1. Direct CPU Pointer Addressing: When an AWS EC2 instance (e.g., c6i, r6i, or m6i) boots, the AWS Nitro hypervisor maps physical host DRAM channels into the virtual machine's CPU Virtual Memory Manager (MMU).
  2. Hardware Cache Alignment: Reads and writes to DRAM bypass disk layers completely and execute via direct hardware assembly instructions (MOV, LOAD, STORE) managed across CPU L1/L2/L3 hardware caches.
  3. Application Heap/Stack Allocation: Applications request DRAM directly from the OS kernel using standard memory allocators (malloc() in C/C++, Vec in Rust, or Buffer.alloc() in Node.js/TypeScript).
  4. Zero-Copy Execution: Modern engines (for ex: Turbopuffer, using Rust's rkyv) cast raw binary buffers in DRAM directly onto native struct pointers, avoiding memory copies and CPU deserialization overhead.

TypeScript Example: Allocating, Reading, and Writing DRAM on AWS

When running Node.js/TypeScript on an AWS EC2 instance, accessing DRAM is done via native in-memory allocations (Buffer or SharedArrayBuffer) which map directly into user-space RAM addresses.

typescript
import { performance } from "node:perf_hooks";
 
export class DramMemoryStore {
  private ramBuffer: Buffer;
  private capacityBytes: number;
 
  /**
   * Allocates contiguous physical DRAM via Node.js
   * off-heap memory.
   */
  constructor(sizeInMB: number = 256) {
    this.capacityBytes = sizeInMB * 1024 * 1024;
    // Buffer.allocUnsafe allocates C++ memory directly in
    // system DRAM, outside the V8 JavaScript heap
    this.ramBuffer = Buffer.allocUnsafe(this.capacityBytes);
    console.log(
      `[DRAM] Allocated ` +
        `${(this.capacityBytes / (1024 * 1024)).toFixed(2)} MB ` +
        `of raw system RAM.`,
    );
  }
 
  /**
   * Writes a binary vector or block directly to a
   * DRAM offset address.
   */
  writeToDram(offset: number, data: Buffer): void {
    if (offset + data.length > this.capacityBytes) {
      throw new Error("DRAM Allocation Overflow");
    }
    // Direct RAM memory copy (memcpy at hardware level)
    data.copy(this.ramBuffer, offset);
  }
 
  /**
   * Reads a byte slice directly from a DRAM pointer
   * offset.
   */
  readFromDram(offset: number, length: number): Buffer {
    // Zero-copy slice: creates a view over the
    // existing DRAM memory pointer
    return this.ramBuffer.subarray(offset, offset + length);
  }
}
 
// --- Benchmarking DRAM Access Speeds ---
 
function runDramBenchmark() {
  const DRAM_SIZE_MB = 100;
  const BLOCK_SIZE = 128 * 1024; // 128 KB chunk
  const dram = new DramMemoryStore(DRAM_SIZE_MB);
 
  const sampleData = Buffer.alloc(BLOCK_SIZE, 0x42); // 128 KB payload
 
  // 1. Write to DRAM
  const writeStart = performance.now();
  for (let offset = 0; offset < 50 * 1024 * 1024; offset += BLOCK_SIZE) {
    dram.writeToDram(offset, sampleData);
  }
  const writeDuration = performance.now() - writeStart;
 
  // 2. Read from DRAM
  const readStart = performance.now();
  let totalBytesRead = 0;
  for (let offset = 0; offset < 50 * 1024 * 1024; offset += BLOCK_SIZE) {
    const chunk = dram.readFromDram(offset, BLOCK_SIZE);
    totalBytesRead += chunk.length;
  }
  const readDuration = performance.now() - readStart;
 
  console.log(
    `[DRAM Write] Wrote 50 MB in ${writeDuration.toFixed(3)} ms ` +
      `(${((50 / writeDuration) * 1000).toFixed(2)} MB/s)`,
  );
  console.log(
    `[DRAM Read]  Read ${totalBytesRead / (1024 * 1024)} MB ` +
      `in ${readDuration.toFixed(3)} ms ` +
      `(${((50 / readDuration) * 1000).toFixed(2)} MB/s)`,
  );
}
 
runDramBenchmark();