The Database Storage chapter in the User’s Guide walks through configuration: pick a database, set a connection string, start the server. This post goes one level deeper. What actually happens to the bytes when a file lands in the database? How does the server avoid copying gigabytes when you duplicate a file? And what keeps orphan chunks from piling up forever?
Four tables, one filesystem
The JDBC backend stores everything in four tables, auto-created on first boot:
sftp_file_storage -- one row per file or directory (adjacency-list hierarchy)
sftp_chunk_data -- the actual bytes, one BLOB per chunk, plus a ref_count
sftp_file_chunks -- ordered mapping: which chunks belong to which file
sftp_file_locks -- read/write and intent locks for concurrent access
Directories use an adjacency-list model: each row has a parent_id pointing to its parent directory’s file_id, or NULL for root-level entries. Arbitrary nesting is supported. The type column distinguishes files ('F') from directories ('D').
The separation matters. File metadata (owner, size, timestamps) lives in sftp_file_storage. The raw bytes live in sftp_chunk_data. The join table sftp_file_chunks connects them by chunk_index. This three-table split is the foundation for everything else.
Chunking: 4 MB at a time
When a client uploads a file, the server does not write the entire content into a single BLOB row. It splits the stream into 4 MB chunks (DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024 in JdbcStorageBackend). Each chunk becomes one row in sftp_chunk_data with its own chunk_id.
The write path uses a producer-consumer pipeline. The SFTP I/O thread fills a 4 MB buffer. When the buffer is full, it goes onto a bounded queue (capacity 8). A background drain thread takes chunks off the queue and inserts them into the database. At most 32 MB of file content sits in memory at any moment. When the queue is full, the I/O thread blocks — backpressure is automatic.
The entire upload runs inside a single SQL transaction with autoCommit = false. The commit only happens when the client closes the file cleanly. If the connection drops mid-transfer, the transaction rolls back and every chunk it inserted disappears. No orphans, no cleanup pass, no .tmp file left behind.
Why 4 MB? It bounds the database’s transaction-log records. A single-row insert of a 9 GB file would pin the log for the entire upload and risk filling the log volume — we learned that the hard way. Chunked inserts let the log advance. It also keeps each BLOB row within the comfort zone of every supported engine: H2, SQL Server, MySQL, and PostgreSQL each use a different native BLOB type (VARBINARY, VARBINARY(MAX), LONGBLOB, BYTEA), and the DDL adapts automatically.
Copy-on-write: the “dedup” that actually ships
The backend does not perform content-addressable deduplication. There is no SHA-256 column, no hash lookup before each insert. Every upload writes fresh chunks.
Where deduplication enters the picture is copies. When you copy a file — through the Web Portal, WebDAV, FTP, or the admin API — the server does not duplicate the BLOB data. It creates a new metadata row in sftp_file_storage, copies the sftp_file_chunks mapping rows (pointing to the same chunk_id values), and increments ref_count on every shared chunk:
UPDATE sftp_chunk_data SET ref_count = ref_count + 1
WHERE chunk_id IN (SELECT chunk_id FROM sftp_file_chunks WHERE file_id = ?)
A copy of a 100 MB file that spans 25 chunks creates exactly 25 mapping rows and increments 25 counters. Zero bytes of BLOB data are duplicated. The copy is instantaneous regardless of file size.
This is the same copy-on-write pattern used by filesystems like ZFS and Btrfs, but implemented in pure SQL. Every database engine the server supports can run it — no filesystem-level feature required.
Ref counts: what happens on delete
When a file is deleted, the server runs the inverse:
- Decrement
ref_counton every chunk the file references. - Remove the file’s mapping rows from
sftp_file_chunks. - Garbage-collect:
DELETE FROM sftp_chunk_data WHERE ref_count <= 0.
If this was the only file using those chunks, ref_count drops to zero and the BLOB rows are deleted immediately. If another file (a copy, or a versioned snapshot) still references them, the chunks survive. No background sweep, no scheduled job — cleanup is inline with the delete operation and wrapped in the same transaction.
Recursive directory deletes walk depth-first, running the decrement-and-clean cycle for every child file before removing each directory’s metadata row. The cascade is deterministic: a directory with a thousand files issues a thousand ref-count updates and one garbage-collection pass, all within a single transaction.
What this means for operations
Four practical consequences:
- Backup size reflects actual content, not copies. Ten copies of the same report share one set of chunks in the database. Your database backup captures those chunks once.
- File copies are O(metadata), not O(bytes). Copying a 5 GB file through any protocol takes the same time as copying a 5 KB file — the chunks are already there.
- Deletes are safe with shared data. You can delete one copy of a file without worrying about whether another user’s copy still needs the underlying bytes. The ref count handles it.
- Quota reports logical usage.
getUsedBytessumsfile_sizeacross a user’s files. If you copied a 500-byte file, your quota reports 1,000 bytes — the logical footprint you chose to create, not the physical storage consumed. This is deliberate: users should see what they own, not what the storage engine optimized away.
The Database Storage chapter in the User’s Guide covers everything you need to set it up: connection strings for each supported engine, driver installation, password security through the credential store, and the schema that gets auto-created on boot. This post is the “why it works that way” companion to that chapter’s “how to turn it on.”