Random UUIDs are killing your database performance
You switched from integer IDs (1, 2, 3…) to UUIDs (a1b2-3c4d-…) for security or distributed generation.
Then your database writes get slower, sometimes much slower.
Here’s why:
Index Fragmentation.
Most database indexes are B-Trees (balanced, sorted trees). The physical location of your data matters.
1./ 𝐒𝐞𝐪𝐮𝐞𝐧𝐭𝐢𝐚𝐥 𝐈𝐃𝐬
When you insert sequential integers (1, 2, 3), new data always goes to the rightmost leaf page of the index.
Writes are predictable and sequential.
Cache hits are maximized.
Pages stay 100% full.
This is the speed limit of your database.
2./ 𝐑𝐚𝐧𝐝𝐨𝐦 𝐔𝐔𝐈𝐃𝐯4
UUIDv4 values are uniformly random. This means a new insert can land anywhere in the tree structure.
Because the inserts are scattered:
- The database must constantly load random pages from disk to memory (Random I/O).
- Page Splitting => When a target page is full, the database has to split it in half to make room, leaving you with two half-empty pages.
- 'Swiss Cheese' Effect => Your index becomes larger and full of holes, wasting RAM and disk space.
This can degrade write throughput by 20–90% once your index size exceeds your available RAM.
3./ 𝐔𝐔𝐈𝐃𝐯7
Stop using UUIDv4 for primary keys. Use UUIDv7 (Standardized in RFC 9562).
UUIDv7 embeds a timestamp at the start of the ID, making it sortable.
This gives you the best of both worlds:
- Distributed generation => (No central counter needed).
- Monotonic inserts => They behave like sequential integers in a B-Tree, eliminating fragmentation.
- Security => Prevents trivial ID enumeration (attackers can't guess that user 101 follows user 100), though note that it does reveal the record's creation time.
You get the utility of UUIDs without the performance penalty.
• • •
Missing some Tweet in this thread? You can try to
force a refresh
While others scroll, you could understand how Flash Attention achieves 3x speedup, how LoRA cuts fine-tuning costs by 90%, and how MoE makes models efficient.
➕ What's covered:
➡️ Lecture 1: Transformer Fundamentals
→ Tokenization and word representation
→ Self-attention mechanism explained
→ Complete transformer architecture
→ Detailed implementation example
➡️ Lecture 2: Advanced Transformer Techniques
→ Position embeddings (RoPE, ALiBi, T5 bias)
→ Layer normalization and sparse attention
→ BERT deep dive and finetuning
→ Extensions of BERT
From Stanford Online:
Rigorous instruction. Latest techniques. Free access.
Perfect for:
→ ML engineers building with LLMs
→ AI engineers understanding transformers
→ Researchers working on language models
→ Anyone learning beyond API calls
This weekend: learn the techniques that separate good engineers from great ones.
(I will put the playlist in the comments.)
♻️ Repost to save someone $$$ and a lot of confusion.
✔️ Follow @techNmak for more AI/ML insights.
9 core patterns for building Fault-Tolerant Applications
Fall seven times, stand up eight.
Follow @techNmak for more :)
[1.] Circuit Breaker
◾ Acts like an electrical circuit breaker.
◾ When a service experiences repeated failures, the circuit breaker 'trips' & stops sending requests to that service for a period of time.
◾ This allows the failing service to recover without being overwhelmed.
The main circuit breaker states -
◾ Closed: Requests are allowed to pass through.
◾ Open: Requests are immediately rejected with an error.
◾ effective for protecting against cascading failures & isolating problematic services.
[2.] Retry
◾ When a request fails, the system automatically retries it a certain number of times before giving up.
◾ This can help overcome transient errors like network glitches or temporary unavailability.
◾ Improves system availability and can mask transient errors.
◾ Be mindful of retry storms (where excessive retries overload the system) and implement exponential backoff (increasing the time between retries).
Are you preparing for a system design interview? Then you should not miss this post.
Read more in the 🧵
Follow - @techNmak
Are you preparing for a system design interview? 👇
Remember,
'Think Tradeoffs, Not Just Tech'
[1.] CAP Theorem
Consistency vs. Availability vs. Partition Tolerance
Choose two => Consistent data, high availability or tolerance to network failures.
(It's actually a choice between C & A)
[2.] Latency vs. Throughput
Fast response times vs. high data processing volume
[3.] ACID vs. BASE
Strict transaction guarantees vs. flexible consistency models
[4.] Monolithic vs. Microservices
Single, unified application vs. distributed, independent services
[5.] SQL vs. NoSQL
Structured data and complex queries vs. flexible schemas and scalability
[6.] Push vs. Pull
Data delivery initiated by server vs. requested by client
[7.] Caching Strategies
Tradeoffs between different cache eviction policies (LRU, LFU, etc.)
Balancing faster data access with potential staleness and increased complexity.
[8.] Statefulness vs. Statelessness
Maintaining session state vs. stateless interactions for scalability
[9.] Optimistic vs. Pessimistic Locking
Optimistic locking assumes no conflicts, favoring speed and concurrency. Pessimistic locking prevents conflicts by acquiring locks upfront, sacrificing performance for data integrity.
[10.] Data Locality vs. Data Distribution
Keeping data close for faster access vs. distributing for resilience and parallel processing