GridFS Binary Chunking Architecture
GridFS splits binary files exceeding the 16MB document boundary into standardized 255KB chunks across two collections: fs.files and fs.chunks.
BSON Binary Byte Layout Inspector
Explore how MongoDB serializes JSON documents into binary BSON with length headers for O(1) field navigation.
Field "_id" • ObjectId (12 bytes)
ObjectId: 4-byte timestamp, 5-byte random value, 3-byte incrementing counter.
1. Formal Definition & Architecture Intent
GridFS is a driver-level specification and storage abstraction for storing files that exceed the 16MB BSON document size limit. Instead of storing a file in a single document, GridFS divides the file into contiguous 255KB chunks stored in a chunks collection (fs.chunks) and stores file metadata in a companion collection (fs.files).
2. Explain Like I'm 5 (Mental Model)
If you have a 100MB video, you cannot fit it into a single 16MB box. GridFS slices the video into 400 small 255KB sandwich bags, numbers them from 0 to 399, and puts a label in an index book so you can reassemble them in order.
3. Real-World Architecture Example
Storing a 50MB PDF report in MongoDB.
{
"_id": "ObjectId(\"65b2a1e8c4e09f3e12000050\")",
"length": 52428800,
"chunkSize": 261120,
"filename": "annual_report_2026.pdf",
"contentType": "application/pdf"
}{
"_id": "ObjectId(\"65b2a1e8c4e09f3e12000051\")",
"files_id": "ObjectId(\"65b2a1e8c4e09f3e12000050\")",
"n": 0,
"data": "BinData(0, \"...255KB of raw binary...\")"
}Drivers reconstruct the file stream on-the-fly by querying fs.chunks sorted by { files_id: 1, n: 1 }.
4. MQL Implementation Steps
mongofiles -d media_store put backup.tar.gz
db.fs.files.find({ filename: "annual_report_2026.pdf" });5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Chunk Size | Default chunk size is 255 KB (261,120 bytes). |
| Compound Unique Index | fs.chunks requires a compound unique index on { files_id: 1, n: 1 } to guarantee stream reassembly order. |
6. Deep Dive: Range Requests & Streaming Video
GridFS allows clients to stream files and perform range queries (e.g. HTTP 206 Partial Content for video seeking). Drivers can calculate the exact chunk index: chunk_n = Math.floor(byte_offset / chunkSize), fetching only the required 255KB chunk instead of loading the entire multi-gigabyte file into memory.
// Index enforced automatically on fs.chunks:
db.fs.chunks.createIndex({ files_id: 1, n: 1 }, { unique: true });7. Architectural Trade-offs
- •Enables storing files of arbitrary size (gigabytes) within MongoDB
- •Supports random byte access and streaming playback without reading the entire file
- •File chunks inherit MongoDB replication, sharding, and automatic failover
- •Serving static files from MongoDB is slower than dedicated Object Storage (AWS S3) combined with a CDN
- •Modifying a portion of a file requires updating chunks or rewriting the stream
8. Technical Interview Questions
Because GridFS divides files into deterministic 255KB chunks indexed by sequence number n, an HTTP byte-range request (e.g. bytes 500000-800000) can calculate exactly which chunks are needed (chunk 1 and 2), querying only those specific chunk documents from fs.chunks.
9. Staff-Level Ready Answer
"GridFS is MongoDB's specification for storing files exceeding 16MB. It stores file metadata in fs.files and partitions the raw binary into 255KB chunks in fs.chunks with a unique index on { files_id: 1, n: 1 }. It enables streaming and range seeks, though dedicated cloud storage like S3 is preferred for high-volume static media."
10. Quick Knowledge Check
What is the default chunk size used by MongoDB GridFS?