Episode 9 — System Design / 9.8 — Communication and Data Layer

9.8 — Quick Revision (Cheat Sheet)

How to use this material (instructions)

  1. Use this for quick review before interviews, not as a primary learning resource.
  2. If any item feels unfamiliar, go back to the corresponding subtopic file.
  3. Print this or keep it open on a second screen during mock interviews.

1. Protocol Comparison

ProtocolTransportConnectionDirectionBest For
HTTP/1.1TCPPersistent (keep-alive)Request-ResponseStandard web, REST APIs
HTTP/2TCPMultiplexed streamsRequest-ResponseModern web (multiple resources)
HTTP/3QUIC (UDP)Multiplexed, 0-RTTRequest-ResponseLow-latency web
WebSocketTCPPersistent, full-duplexBidirectionalChat, live updates, gaming
SSEHTTPPersistent, one-wayServer -> ClientNotifications, live feeds
gRPCHTTP/2Multiplexed4 patterns (unary, streaming)Microservice-to-microservice

2. HTTP Methods

MethodActionIdempotentSafeBody
GETReadYesYesNo
POSTCreateNoNoYes
PUTReplaceYesNoYes
PATCHUpdateNo*NoYes
DELETERemoveYesNoNo

3. Must-Know Status Codes

CodeMeaningWhen
200OKSuccessful GET/PUT/PATCH
201CreatedSuccessful POST
204No ContentSuccessful DELETE
301Moved PermanentlyURL changed
304Not ModifiedCache still valid
400Bad RequestValidation error
401UnauthorizedNo auth provided
403ForbiddenAuth OK, no permission
404Not FoundResource missing
409ConflictDuplicate, state conflict
429Too Many RequestsRate limited
500Internal Server ErrorServer bug
502Bad GatewayUpstream failure
503Service UnavailableOverloaded

4. REST vs GraphQL

FactorRESTGraphQL
EndpointsManyOne (/graphql)
Data shapeServer decidesClient decides
Over-fetchingCommonEliminated
Under-fetchingCommonEliminated
CachingEasy (HTTP)Hard (all POST)
Best forSimple CRUD, public APIsComplex queries, mobile

5. Pagination

TypeJump to Page N?Consistent Under Writes?Deep Offset Perf
OffsetYesNoDegrades
CursorNoYesConstant

6. SQL vs NoSQL

FactorSQLNoSQL
SchemaFixed, predefinedFlexible, dynamic
RelationshipsJoins (strong)Embedded/denormalized
TransactionsMulti-table ACIDUsually single-document
ScalingVertical (+ read replicas)Horizontal (sharding)
QueryAd-hoc SQLLimited to access patterns
Best forFinancial, relational dataFlexible schemas, massive scale

7. ACID Properties

PropertyMeaningOne-Liner
AtomicityAll or nothingTransaction fully commits or fully rolls back
ConsistencyValid state to valid stateConstraints always enforced
IsolationConcurrent txns do not interfereOne transaction cannot see another's uncommitted changes
DurabilitySurvives crashesOnce committed, data is on disk

8. CAP Theorem

  Network partitions WILL happen -> Choose CP or AP

  CP: Consistency + Partition Tolerance (refuse stale reads)
      Examples: MongoDB (default), HBase, Zookeeper
      Use: Banking, inventory, leader election

  AP: Availability + Partition Tolerance (serve stale data)
      Examples: Cassandra, DynamoDB (default), CouchDB
      Use: Social feeds, shopping carts, analytics

Quorum formula: R + W > N = strong consistency


9. Database Selection Guide

NeedDatabaseType
General purpose, complex queriesPostgreSQLSQL
Flexible schema, rapid developmentMongoDBDocument
Sub-ms caching, sessionsRedisKey-Value
Serverless, predictable perfDynamoDBKey-Value/Doc
Massive writes, time-seriesCassandraColumn-Family
Relationship traversalNeo4jGraph

10. NoSQL Types

TypeModelExampleUse Case
DocumentJSON documentsMongoDB, CouchDBContent, catalogs, profiles
Key-Valuekey -> valueRedis, DynamoDBCache, sessions, config
Column-FamilyRow key -> column familiesCassandra, HBaseIoT, time-series, logs
GraphNodes + edgesNeo4j, NeptuneSocial networks, fraud, recommendations

11. Database Scaling Ladder

  Step 1: Optimize queries + indexes (free performance)
       |
  Step 2: Add read replicas (handle read-heavy load)
       |
  Step 3: Add caching layer - Redis (reduce DB load)
       |
  Step 4: Vertical scaling (bigger machine)
       |
  Step 5: Sharding (horizontal partitioning)

12. Sharding Strategies

StrategyDistributionRange QueriesReshardingBest For
RangeUneven (hotspots)Single shardMediumNaturally ordered data
HashEvenAll shardsExpensive (use consistent hashing)Most cases
GeographicBy regionWithin regionMediumMulti-region, compliance

Shard key must have: High cardinality, even distribution, matches query patterns, immutable.


13. Replication

  Primary (writes) --async--> Replica 1 (reads)
                   --async--> Replica 2 (reads)
                   --sync---> Replica 3 (reads, failover candidate)
TypeWrite SpeedData Loss RiskUse
AsyncFastSome (lag)Most apps
SyncSlowerNoneCritical data
Semi-syncBalancedMinimalProduction default

14. Consistency Models

  Strong -----> Sequential -----> Causal -----> Session -----> Eventual
  (slowest,     (total order)    (cause before  (read-your-   (fastest,
   safest)                        effect)        writes)        least safe)
Data TypeConsistency Needed
Bank balanceStrong
Inventory (low stock)Strong
Social media likesEventual
User profile (own view)Session (read-your-writes)
AnalyticsEventual
Payment processingStrong

15. Distributed Transactions

PatternBlocking?ConsistencyBest For
2PCYes (holds locks)StrongWithin single DB system
Saga (Choreography)NoEventualSimple 2-4 step flows
Saga (Orchestration)NoEventualComplex 5+ step flows
Outbox PatternNoEventualReliable event publishing

16. Saga Compensation

  Forward: Step 1 -> Step 2 -> Step 3 -> Step 4
  
  If Step 3 fails:
  Compensate: Undo Step 2 -> Undo Step 1
  
  Each step needs a reversing "compensating transaction"

17. Connection Pooling

  Formula: connections = (cores * 2) + spindles
  Typical: 10-20 per app server (not 100+)
  Tools: PgBouncer (PostgreSQL), ProxySQL (MySQL), HikariCP (Java)

18. Indexing Quick Reference

IndexBest ForNot For
B-Tree (default)=, <, >, BETWEEN, ORDER BYFull-text, spatial
HashExact = onlyRange queries
GINFull-text, JSONB, arraysSimple equality
BRINLarge, naturally ordered tablesRandom access

Composite index rule: Leftmost prefix. Index on (A, B, C) supports WHERE A=?, WHERE A=? AND B=?, but NOT WHERE B=? alone.


19. Latency Numbers

OperationTime
L1 cache0.5 ns
L2 cache7 ns
RAM100 ns
SSD read16 us
HDD read2 ms
Same datacenter RTT0.5 ms
Same region RTT1-5 ms
Cross-continent RTT80-150 ms

20. CDN Basics

  User -> CDN Edge (nearby)
            |
            +-- HIT: return cached content (fast)
            +-- MISS: fetch from origin, cache it, return

Cache: static assets (long TTL), HTML (short TTL), API responses (sometimes), user data (rarely).


21. Rate Limiting Algorithms

AlgorithmBehaviorBursts Allowed?
Fixed WindowCount per time windowYes (at boundary)
Sliding WindowCount in rolling windowSmoothed
Token BucketTokens added at fixed rateYes (up to bucket size)
Leaky BucketProcess at fixed rateNo (smooths everything)

Headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After Status code: 429 Too Many Requests


22. CRDTs (One-Liner)

Data structures that replicas can update independently and always merge without conflicts. Used for: counters (G-Counter), sets (OR-Set), registers (LWW-Register). Used by: Figma, Riak, Redis Enterprise.


23. Interview Decision Framework

When asked "which database/protocol/consistency model would you use?":

  1. State the requirements (read/write ratio, latency, consistency, scale)
  2. Name your choice with a specific database/technology
  3. Justify with tradeoffs ("I chose X because... the tradeoff is...")
  4. Mention what you would NOT use and why

"I would use PostgreSQL for the order data because we need ACID transactions for payment integrity. The tradeoff is that horizontal scaling is harder than with DynamoDB, but at our expected volume of 10K writes/sec, a vertically scaled PostgreSQL with read replicas is sufficient."


Use this sheet for final review. For deeper understanding, revisit the subtopic files: 9.8.a | 9.8.b | 9.8.c | 9.8.d | 9.8.e