Monday 11 March 2024

How an in-memory database achieve durability?

 

Durability in ACID and DBMS means that once you finish a transaction, the changes you made to the database persist always even if something goes wrong with the computer. Basically, your data stays safe even if the system crashes or the power goes out.

 

How in-memory systems achieve durability?

In-memory database management systems primarily store data in memory and utilize disk for recovery and logging purposes.

 

Examples

H2 Database: H2 is a Java-based open-source relational database management system. It supports both in-memory databases and persistent disk-based databases. H2 offers features such as ACID transactions, SQL support, and compatibility with other database systems. It finds applications in embedded systems, testing environments, and small to medium-sized applications.

 

Apache Derby: Apache Derby is an open-source relational database management system implemented in Java. It supports both in-memory databases and persistent disk-based databases. Derby is fully transactional and supports SQL queries, stored procedures, and triggers. It is commonly used in embedded systems, desktop applications, and small-scale web applications.

 

Here are some common techniques used by in-memory databases to achieve durability.

 

1. Write Ahead Logging (WAL)

Write-ahead logging (WAL) is a method used in databases to ensure that changes to data stay even if the system crashes, which is important for maintaining durability, a key aspect of database reliability.

 

In WAL, every change made to the database is first recorded in a log file on the disk before it's applied to the actual data in the system's memory(disk). This log file keeps track of all the transactions, serving as a backup that can be used to recover data if there's a crash.

 

WAL is also known as a transaction log, indicating that a transaction is considered complete only when the data is safely written into this log. WAL provides a way to replay transactions, allowing the database to be restored to a consistent state after a failure.

 

Here's how it operates:

WAL Log: The database system maintains a dedicated file known as the Write-Ahead Log (WAL), which records all changes.

 

Log First: Before any changes are made to the actual data, they're first recorded in the WAL. This ensures that the log accurately reflects the intended updates.

 

Durability Assurance: The WAL is stored in stable storage, usually the disk, guaranteeing that the log persists even if there's a crash during the update process.

 

Recovery from Crashes: If the system crashes, the database can use the WAL to recover. It replays the log entries, redoing the intended changes to bring the database back to a consistent state reflecting the last committed transactions.

 

Advantages of WAL

Since data updates aren't immediately written to the main database files, WAL enables quicker write operations. The actual data updates happen periodically based on some dbms configuraitons.

 

Addressing Concerns of Infinite Growth: Managing Transaction Logs in WAL

Continuously writing all transactions to a single WAL file would result in an ever-expanding and eventually unmanageable file size. Here are a few methods used by databases to effectively handle WAL size:

 

Checkpointing: Databases regularly execute checkpoints. During a checkpoint, all pending changes in the WAL are applied to the main data files. This essentially tidies up the WAL by eliminating entries that are no longer necessary for recovery. Checkpoints ensure the WAL doesn't grow excessively large.

 

Segmentation: Some systems utilize segmented WALs. Instead of one large file, the WAL is split into multiple segments. Once a segment reaches a specific size limit, it's either archived or recycled. This prevents the entire WAL from becoming unwieldy.

 

By employing these strategies, databases can maintain a manageable size for the WAL while still ensuring data durability through write-ahead logging.

 

2. Periodic Snapshots to Disk

Another method is to regularly take the snapshots of the data that's stored in the computer's memory and save them on the hard drive. This helps if something goes wrong and the system crashes. You can use these snapshots to get the database back to how it was when the last snapshot was taken.

 

3. Battery-backed RAM (BBRAM)

BB-RAM is like regular memory (RAM) in your computer but with a small battery attached to it. The RAM stores all the information while your computer is on, and if the power suddenly goes out, the battery kicks in to keep that information safe. Some systems use both RAM and battery-backed hardware to make sure your data stays safe. They store the data in fast-access RAM at first, but then regularly save it onto more permanent storage, like a solid-state drive (SSD) or hard disk drive (HDD), using the battery's power.

                                                  System Design Questions

No comments:

Post a Comment