Skip to main content

Database Internals

"The database is the heart of any application."

Understanding database internals helps you write efficient queries, choose the right data model, and optimize performance under load.

Overview

This section covers the two most critical databases for backend engineers:

MySQL

Redis

Advanced Topics

Quick Reference

MySQL Indexing

TypeDescriptionUse Case
B+ TreeBalanced tree, range queriesPrimary keys, most columns
PrimaryClustered index, data stored with indexPrimary key
SecondaryNon-clustered, stores primary key valueFrequent queries
CompositeMulti-columnMulti-condition WHERE

Transaction Isolation Levels

LevelDirty ReadNon-RepeatablePhantomPerformance
Read UncommittedFastest
Read Committed (RC)Fast
Repeatable Read (RR)⚠️*Medium
SerializableSlowest

*MySQL InnoDB prevents phantom reads with Gap Locks

Redis Data Structures

StructureUse CaseCommands
StringSimple cache, countersGET, SET, INCR
HashObject storageHGET, HSET, HGETALL
ListMessage queues, feedsLPUSH, RPOP, LRANGE
SetUnique items, tagsSADD, SMEMBERS, SINTER
Sorted SetLeaderboards, rankingsZADD, ZRANGE, ZRANK

Common Pitfalls

MySQL

  • ❌ Missing index on WHERE column → Full table scan
  • ❌ Function on column (WHERE YEAR(date) = 2024) → Index invalidation
  • ❌ Deep pagination (LIMIT 1000000, 10) → Scans millions of rows
  • ✅ Use EXPLAIN to analyze queries
  • ✅ Add indexes based on query patterns
  • ✅ Monitor slow query logs

Redis

  • ❌ No TTL set → Memory leak
  • ❌ Cache stampede → Database overload
  • ❌ Cache penetration → Repeated queries for non-existent data
  • ✅ Always set TTL for cache data
  • ✅ Use appropriate data structures (Hash for objects)
  • ✅ Monitor memory usage and hit rate

Learning Path

For interviews:

  1. MySQL Architecture → Indexes → Transactions → Locking
  2. Redis Data Structures → Caching Patterns → Persistence

For production:

  1. MySQL Optimization → Indexes → Transactions → Replication
  2. Redis Caching Patterns → Persistence → Cluster
Production Tips
  1. Always use connection pooling (HikariCP for Java)
  2. Add indexes based on query patterns, not guesses
  3. Monitor slow query logs regularly
  4. Set appropriate TTLs for Redis cache entries
  5. Use read replicas for read-heavy workloads