You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Readme.md
+7-2Lines changed: 7 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,15 @@
1
1
#
2
2
3
+
*[#08 - Tree Indexes: B+Trees (CMU Intro to Database Systems)](https://www.youtube.com/watch?v=scUtG_6M_lU)
4
+
*[SQLite: How it works](https://www.youtube.com/watch?v=ZSKLA81tBis)
3
5
*[Write a database from scratch](https://www.youtube.com/playlist?list=PLWRwj01AnyEtjaw-ZnnAQWnVYPZF5WayV)
4
6
5
-
##
7
+
*[Understanding B-Trees: The Data Structure Behind Modern Databases](https://www.youtube.com/watch?v=K1a2Bk8NrYQ)
8
+
9
+
*[Build a NoSQL Database From Scratch in 1000 Lines of Code](https://medium.com/better-programming/build-a-nosql-database-from-the-scratch-in-1000-lines-of-code-8ed1c15ed924)
10
+
*[Writing a SQL database from scratch in Go: 1. SELECT, INSERT, CREATE and a REPL](https://notes.eatonphil.com/database-basics.html)
6
11
7
-
* Build Your Own Database From Scratch
12
+
##
8
13
9
14
```
10
15
1. Persistence. How not to lose or corrupt your data. Recovering from a crash.
Zstandard (Zstd) is a fast compression algorithm that provides better compression ratios than gzip while being faster. It’s great for high-performance databases, logs, network transmission, and storage.
4
+
5
+
📌 Why Use Zstd in Golang?
6
+
• 🔥 High compression ratio (better than gzip)
7
+
• ⚡ Fast decompression (ideal for real-time applications)
8
+
• 📉 Adjustable compression levels (trade-off between speed & compression)
9
+
• 🔄 Streaming support (works well for large files)
Copy file name to clipboardExpand all lines: secretary/lock.md
+106Lines changed: 106 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,3 +170,109 @@ Tradeoffs
170
170
• Harder to debug than traditional locking mechanisms.
171
171
172
172
Would you like me to refine specific parts, like concurrent range queries or node splits? 🚀
173
+
174
+
175
+
176
+
177
+
178
+
Database Locks: Types and Usage
179
+
180
+
Database locks are mechanisms used to ensure consistency, integrity, and concurrency control in multi-user environments. They prevent race conditions, dirty reads, and data corruption when multiple transactions access the same data.
181
+
182
+
⸻
183
+
184
+
Types of Database Locks
185
+
186
+
1. Pessimistic Locking
187
+
• Blocks access to a resource until a transaction is complete.
188
+
• Ensures no other transaction modifies the data while a lock is held.
189
+
• Typically used in high-contention scenarios (e.g., banking systems).
190
+
191
+
Example (MySQL FOR UPDATE)
192
+
193
+
START TRANSACTION;
194
+
SELECT * FROM accounts WHERE id = 1 FOR UPDATE; -- Locks row until COMMIT/ROLLBACK
195
+
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
196
+
COMMIT;
197
+
198
+
Use Cases:
199
+
✅ Ensures strong consistency.
200
+
❌ Can cause performance issues due to waiting/blocking.
201
+
202
+
⸻
203
+
204
+
2. Optimistic Locking
205
+
• Allows concurrent access but detects conflicts before committing.
206
+
• Uses version numbers or timestamps to check if data was modified.
207
+
• If a conflict is detected, the transaction is retried.
208
+
209
+
Example (Using Version Number)
210
+
211
+
SELECT id, balance, version FROM accounts WHERE id = 1;
212
+
UPDATE accounts SET balance = balance - 100, version = version + 1
213
+
WHERE id = 1 AND version = 1; -- Fails if version changed
214
+
215
+
Use Cases:
216
+
✅ Best for low-contention scenarios.
217
+
❌ Requires extra logic for retrying transactions.
218
+
219
+
⸻
220
+
221
+
3. Table Locks
222
+
• Locks the entire table, preventing other transactions from reading or writing.
223
+
• Used when bulk updates need consistency.
224
+
225
+
Example (MySQL Table Lock)
226
+
227
+
LOCK TABLES accounts WRITE;
228
+
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
229
+
UNLOCK TABLES;
230
+
231
+
Use Cases:
232
+
✅ Guarantees full consistency.
233
+
❌ Not scalable for multi-user applications.
234
+
235
+
⸻
236
+
237
+
4. Row-Level Locks
238
+
• Locks only specific rows affected by a transaction.
239
+
• Allows higher concurrency than table locks.
240
+
241
+
Example (PostgreSQL SELECT FOR UPDATE)
242
+
243
+
BEGIN;
244
+
SELECT * FROM orders WHERE id = 123 FOR UPDATE; -- Locks row
245
+
UPDATE orders SET status = 'shipped' WHERE id = 123;
246
+
COMMIT;
247
+
248
+
Use Cases:
249
+
✅ Efficient for concurrent updates on different rows.
250
+
❌ Can cause deadlocks if transactions lock rows in different orders.
251
+
252
+
⸻
253
+
254
+
5. Deadlocks and Handling
255
+
256
+
A deadlock occurs when two transactions hold locks and wait for each other to release them.
257
+
258
+
Example Deadlock (Two Transactions)
259
+
260
+
Transaction A: LOCK row 1 → WAIT for row 2
261
+
Transaction B: LOCK row 2 → WAIT for row 1
262
+
263
+
Preventing Deadlocks
264
+
• Access resources in a consistent order.
265
+
• Use shorter transactions to minimize lock time.
266
+
• Set timeouts on locks (e.g., SELECT FOR UPDATE NOWAIT).
267
+
268
+
⸻
269
+
270
+
Which Locking Strategy to Use?
271
+
272
+
Scenario Best Locking Strategy
273
+
High contention on updates Pessimistic Locking (FOR UPDATE)
274
+
Low contention, high concurrency Optimistic Locking (versioning)
275
+
Bulk operations Table Locks (LOCK TABLES)
276
+
Multiple transactions updating different rows Row-Level Locks
277
+
278
+
Would you like a deep dive into deadlocks, isolation levels, or specific databases (PostgreSQL, MySQL, etc.)? 🚀
Copy file name to clipboardExpand all lines: secretary/lsmtree.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,13 @@
1
+
[#04 - Database Storage: Log-Structured Merge Trees & Tuples (CMU Intro to Database Systems)](https://www.youtube.com/watch?v=IHtVWGhG0Xg&t=1372s)
2
+
3
+
https://github.com/facebook/rocksdb/wiki
4
+
5
+
https://github.com/krasun/lsmtree
6
+
https://github.com/skyzh/mini-lsm
7
+
8
+
9
+
10
+
1
11
Object stores typically do not use B-trees like databases. Instead, they use hash-based indexing or LSM-trees (Log-Structured Merge Trees) depending on the use case. Here’s why:
0 commit comments