DEV Community

Mohammad Mahabub Alam
Mohammad Mahabub Alam

Posted on

SnapShot ๐Ÿ“ธ๐Ÿ”ซ

A snapshot is a frozen view of a database at a specific moment in time. It is like taking a digital photocopy of a document. Here document is the database and the photocopy is the snapshot. It is a read only static view of data means that you can look at the data but you can not change it within that snapshot itself.

The way it works:

Generally we think that snapshot is a massive bulky copy of the entire database but that would be incredibly slow ans expensive. Instead modern databases use a clever trick called COW (Copy on write). So when you first take a snapshot, the database doesn't actually copy any data. It simply creates a pointer map, at the exact millisecond the snapshot and the live database are looking at the same physical block of the data on the disk. The magic happens only when someone tries to change the live data.
For example: You have a row of data in block "A". If a user update that row the system identifies that block "A" is about to be changed. That time it copies that original old data to a specific snapshot storage area. after that it writes the newly data to the live database.

Remember that the snapshot is taken only when the original data is going to update.

  • More explanation : The snapshot pointer and the live database pointer both points to the same block "A" memory location. After that when the user wants to update block "A" with the new data, the system reads the original data from block "A" and writes it into the new physical space on the disk also can be refer as the snapshot storage. After that the snapshot pointer is updated to point the newly moved original data. The live database pointer stays where it is or moved to the newly created block to write the new data.

The snapshot pointer is used to find the data that are already snapshot.

If the system only update a pointer without copying the data than live update would overwrite the original data and your snapshot would be ruined. For that reason first it copies the old data, stores it into the snapshot storage and than the snapshot pointer update and points this memory storage.

  • Snapshot eventually slow down a database if they are kept for too long. Every time the data updates it copies the old one and than writes into the live data and update. It works double in this way. So to make it efficient modern databases used a method called ROW (Redirect on write).

  • Copy on Write (COW) : First copy the old data and than write the new one.

  • Redirect on write (ROW): As in COw it moved the old data to save it but in ROW we leave the old data as it is and write the new data somewhere else. Same as before the snapshot and live database both point to block "A". When user is trying to update the data it keeps the old data as it is and write the new data into another new memory location. The snapshot pointer stays the same place but the live database pointer is redirected to the new block where the new data is stored.

Copy on Write Redirect on Write
It require 1 read 2 write (Read oldโ†’ write old elsewhereโ†’ write new ) It require only 1 write (new data stores in a new location.)
Instant to create but slowdown overtime. Instant to create and stays fast during database updates.
Read the data faster than ROW Reading data is slow as new data took place in different locations.

Top comments (0)