Back to HLD

SYSTEM DESIGN QUICK REFERENCE

beingsde.in — Built for Staff+ Engineering Interviews

1. Database Selection Matrix

Data TypeDatabaseWhy
Transactions / LedgerPostgreSQLACID, WAL, JOINs
User ProfilesMongoDBFlexible JSON schema
Logs / MetricsCassandraHigh write throughput, LSM
Real-time AnalyticsApache DruidColumnar, sub-second aggs
Search / CatalogElasticsearchInverted index, fuzzy search
Sessions / CacheRedisIn-memory, sub-ms reads

2. CAP Theorem

  • C — Consistency: Every read returns the most recent write or an error.
  • A — Availability: Every request receives a (possibly stale) response.
  • P — Partition Tolerance: System continues operating despite network splits.
  • → Distributed systems must choose CA, CP, or AP during a network partition.
CP Systems
PostgreSQL, Spanner, HBase
AP Systems
DynamoDB, Cassandra, CouchDB
CA Systems
Single-node RDBMS (no partition)

3. Scaling Decision Rules

  • Vertical first — Add CPU/RAM until hardware limits or cost spikes.
  • Horizontal — When write volume exceeds single-node capacity.
  • Read replicas — When reads >> writes (e.g., 90/10 ratio).
  • Caching — For repeated, slow queries with <1% write rate.
  • Sharding — When storage or write throughput hits single-DB ceiling.
  • CDN — For static assets; eliminates origin server load entirely.

4. Sharding vs Partitioning vs Indexing

StrategyScopeGoal
IndexingSingle tableFaster reads
PartitioningSingle DB instanceManage table layout
ShardingMultiple DB nodesScale write throughput

5. Auth Mechanisms

MethodStateRevocableUse Case
Sessions (Redis)Stateful✓ YesWeb apps, high security
JWTStateless✗ HardAPIs, microservices
OAuth 2.0Delegated✓ YesSSO, 3rd-party access

💡 Pattern: API Gateway validates JWT/session, then passes X-User-ID headers downstream.

6. Consistent Hashing

  • • Hash ring spans 0 → 2³² − 1. Servers and keys map onto the ring.
  • • Key routes clockwise to nearest server on the ring.
  • • Adding/removing a node only moves 1/N keys (vs. 100% in modulo hashing).
  • Virtual nodes (100–200 per server) prevent hotspots and ensure even distribution.
  • Used in: Cassandra, Dynamo, Memcached, Discord gateway.

7. Rate Limiting Algorithms

AlgorithmBurst?Memory
Token Bucket✓ YesLow
Sliding Window Log✗ NoHigh (stores timestamps)
Sliding Window Counter~ PartialLow (approximation)

💡 Use Lua scripts in Redis for atomic rate-limit evaluation across distributed gateways.

8. WebSocket Scaling

  • • Increase Linux file descriptor limit (nofile) to ~1M.
  • • Use Layer 4 load balancing (HAProxy) with sticky sessions.
  • • Use Redis Pub/Sub backplane for cross-server message routing.
  • • Pattern: Client → L4 LB → WS Server ↔ Redis Pub/Sub → WS Server → Client

9. Kafka Key Rules

  • • Same partition key → same partition → strict ordering within partition only.
  • • Max consumers in a group = number of partitions.
  • At-least-once delivery requires idempotent consumers.
  • • Consumer group lag = key metric for scaling consumer count.

10. Snowflake ID Layout (64-bit)

1b Sign|41b Timestamp (ms)|5b Datacenter|5b Worker|12b Sequence
  • • Time-sortable, no central coordinator needed.
  • • Up to 4,096 unique IDs per millisecond per machine.
beingsde.in — System Design Cheat Sheet — Use alongside full topic guides at beingsde.in/topics