Are soft deletes better than hard deletes?

ยท

2 min read

- What's hard delete:
Performing a delete query to delete a record permanently from the table.

- What's soft delete:
Flagging a record as deleted in a table, instead of actually deleting the record.

Image preview

- What happens when we do a hard delete?

  1. A search query is made to find in which leaf node the record exists.

  2. The entire node is then moved into RAM.

  3. The record is deleted from the leaf node inside RAM.

  4. Then the leaf node has to be rebalanced inside the disk to satisfy the B+ tree rule.

This is considerably high number of operations when we are looking to save every milliseconds.

One engineering optimization is to just flag the record and return the response. So in the application layer, we can omit the records having this flag while reading.

Later when the activity is low on the server, we can perform a batch program to delete all the records having this flag set to true.

Advantages:
- Low latency โœ“
- Recovery of the deleted record is easily possible โœ“

Disadvantages:
- Increase complexity
- If not taken out after a threshold time, stale and unwanted records are taking up space in the database.

Everything is a tradeoff, depending on the application we have to choose. It's better to be aware that these kinds of designs and processes exist!

The End!

Linkedin GitHub
๐Ÿš€โ˜๏ธ๐Ÿ–ค

#akshayrr_engineering #database

ย