Top Tweets for #msync
it’s just a memecoin

Over last weekend I revisited my old notes on Database Internals and how exactly we should go if we were to build our database. Why do we need sophisticated Database Management Systems (DBMS) when operating systems already provide powerful tools like memory-mapped files (mmap)?
Brief Look
mmap helps to take the content that is available on the disk and map it into virtual memory within our process address space. Once, it is available in Virtual Memory, our process can jump to any offset in that address space in memory with the OS in the charge to decide which offset is which.
Since the process can now jump to any offset in that address space in memory, the OS is in charge of determining whether or not the object you need is in memory. If not, it fetches the needed page and brings it into memory.
Here DBMS doesn’t handle any of the data management; it simply opens the file and OS moves the data back and forth for all of us. Now, before going any further.
Lack Of Control
The OS makes decisions about page eviction without understanding the database’s needs. When using mmap, the operating system’s virtual memory manager (VMM) takes control of page eviction decisions.
This becomes problematic for database systems because the VMM operates at a lower level of abstraction and lacks crucial context about the database’s operations and requirements. At the hardware level, the VMM interacts with the Memory Management Unit (MMU) to manage the translation of virtual addresses to physical addresses.
When memory pressure increases, the VMM must decide which pages to evict from physical memory to make room for new pages. It typically uses algorithms like Least Recently Used (LRU) or its variants to make these decisions.
However, these general-purpose algorithms don’t account for the specific access patterns and importance of different pages in a database context. For instance, a database might have:
- Hot pages: Frequently accessed data that should remain in memory.
- Index pages: Critical for query performance but not necessarily accessed frequently.
- Transaction log pages: Must be written in a specific order for crash recovery.
The VMM, operating blindly to these distinctions, might evict critical index pages or hot data, leading to poor performance. It might also evict pages in an order that compromises the database’s consistency guarantees.
Transaction Safety
The OS doesn’t know about transaction boundaries or the correct order for writing pages. Transaction safety is a critical requirement for database systems, ensuring that operations are atomic, consistent, isolated, and durable (ACID).
When using mmap, the operating system’s lack of awareness about transaction boundaries and write ordering can severely compromise these guarantees.
At a low level, database transactions often involve modifying multiple pages. These modifications must be written to disk in a specific order to maintain consistency, especially in the event of a system crash. This ordering is typically managed through techniques like:
Write-Ahead Logging (WAL): Transaction changes are first recorded in a log before being applied to the actual data pages.
Two-Phase Commit: Ensures all participants in a distributed transaction are ready before committing.
Force-at-commit: Ensures all modified pages are written to disk when a transaction is committed.
The mmap system, however, operates independently of these database-level concepts. When pages mapped by mmap are modified, the operating system marks them as dirty.
These dirty pages are then written back to disk asynchronously by the OS’s page-out daemon or when explicitly requested through msync().
Stalls: Page faults can cause thread-blocking
Page faults are a fundamental mechanism in virtual memory systems, allowing the operating system to manage memory efficiently by loading data from disk only when needed.
However, in the context of database systems using mmap, page faults can lead to significant performance issues due to thread blocking.
When a thread accesses a virtual memory address that isn’t currently mapped to physical memory, a page fault occurs. This process involves several low-level steps:
- The CPU generates a page fault exception.
- The operating system’s page fault handler is invoked.
- The handler determines the location of the required page on disk.
- It initiates a disk I/O operation to read the page.
- The thread is put into a waiting state until the I/O completes.
- Once the page is loaded, the page table is updated.
The thread is rescheduled and can continue execution.
To implement these optimizations without relying on mmap, a database system might use a custom buffer management system. We will take a look into Buffer Management System in maybe next tweet.
Here’s a simplified example of the database operations using mmap implementation in C:
Conclusion
While mmap offers an appealing simplicity, our today's check reveals why most of the database engineers often stay away from it. The need for fine-grained control over memory management, transaction safety, and performance optimization necessitates more sophisticated approaches.
As we've seen, building a database is not just about storing and retrieving data—it's about doing so with utmost efficiency, reliability, and consistency.
Whether you're building the next big database system or simply using one, understanding these underlying principles can highly impact how you approach data management in your applications.
What are your thoughts on database design and memory management? Have you encountered challenges with mmap or similar systems in your projects? Share your experiences and insights in the comments below!
In future tweets, I will try to explain:
- Implementing a custom buffer management system
- Advanced techniques in database transaction management
- Optimizing database performance: Beyond basic indexing
That's it for Today.

Last Seen Hashtags on Sotwe
Most Popular Users

Elon Musk 
@elonmusk
240M followers

Barack Obama 
@barackobama
118.6M followers

Cristiano Ronaldo 
@cristiano
114.4M followers

Donald J. Trump 
@realdonaldtrump
111.7M followers

Narendra Modi 
@narendramodi
106.9M followers

Rihanna 
@rihanna
98.6M followers

NASA 
@nasa
92.1M followers

Justin Bieber 
@justinbieber
91.7M followers

KATY PERRY 
@katyperry
89.9M followers

Taylor Swift 
@taylorswift13
83.9M followers

Lady Gaga 
@ladygaga
75.4M followers

Virat Kohli 
@imvkohli
73.3M followers

Kim Kardashian 
@kimkardashian
70.8M followers

YouTube 
@youtube
68.6M followers

Neymar Jr 
@neymarjr
66.3M followers

Bill Gates 
@billgates
65.2M followers

Selena Gomez 
@selenagomez
63M followers

The Ellen Show
@theellenshow
62.1M followers

CNN 
@cnn
61.7M followers

X 
@x
60.6M followers



