System Design Flashcards
(20 cards)
What are the key components of a system design interview?
Key components include gathering requirements, designing high-level architecture, choosing technologies, addressing scalability, and handling trade-offs. Example: For a URL shortener, define inputs (long URLs), outputs (short URLs), and scale (millions of users).
How do you design a scalable REST API?
Use a layered architecture: Load balancer → API servers (e.g., Node.js) → Database (e.g., PostgreSQL). Implement caching (Redis), rate limiting, and horizontal scaling. Example: Route requests via NGINX, cache responses, shard database.
What is the difference between horizontal and vertical scaling?
Horizontal scaling adds more servers (e.g., more API instances); vertical scaling increases a server’s resources (e.g., more CPU). Horizontal is preferred for distributed systems due to flexibility.
What is a load balancer, and how does it work?
A load balancer distributes traffic across servers to ensure availability. Example: NGINX routes requests to multiple Node.js instances using round-robin or least connections, improving throughput.
How do you design a database schema for a social media app?
Tables: Users (id, name), Posts (id, user_id, content), Comments (id, post_id, user_id). Use foreign keys for relationships, index user_id for fast queries. Normalize for consistency, denormalize for read-heavy loads.
What is the CAP theorem, and how does it affect system design?
CAP theorem states a distributed system can only guarantee two of: Consistency, Availability, Partition Tolerance. Example: Choose high availability (MongoDB) for social apps or consistency (MySQL) for banking.
How do you handle high traffic in a system?
Use load balancers, caching (Redis), CDNs for static assets, and database sharding. Example: Cache user profiles in Redis to reduce database load, scale API servers horizontally.
What is caching, and how is it implemented in a full-stack app?
Caching stores frequently accessed data for fast retrieval. Example: Use Redis to cache API responses in a Node.js backend, reducing database queries (O(1) lookup).
How do you design a rate limiter for an API?
Implement in-memory storage (Redis) to track requests per user/IP in a time window. Example: Allow 100 requests/hour per IP, reject excess with HTTP 429. Use sliding window for accuracy.
What is sharding, and when is it used?
Sharding splits a database into smaller, independent pieces (shards) based on a key (e.g., user_id). Used for large-scale data to improve performance. Example: Shard users by region.
How do you design a URL shortener service?
Components: API (POST /shorten, GET /:id), database (short_id, long_url), hash function (e.g., base62). Scale with sharding, caching redirects in Redis. Handle collisions with unique IDs.
What is a CDN, and how does it improve performance?
A CDN (Content Delivery Network) caches static content (e.g., images, CSS) on edge servers near users. Example: Cloudflare serves React app assets, reducing latency.
How do you ensure data consistency in a distributed system?
Use strong consistency (e.g., distributed locks, Paxos) for critical data or eventual consistency (e.g., DynamoDB) for high availability. Example: Bank transactions need strong consistency.
What is the difference between SQL and NoSQL in system design?
SQL (e.g., PostgreSQL) is structured, relational, best for complex queries; NoSQL (e.g., MongoDB) is flexible, scalable, ideal for unstructured data or high write loads.
How do you design a notification system?
Components: Queue (Kafka) for events, workers to process notifications, database for user preferences. Use WebSockets for real-time or email/SMS APIs. Scale with partitioning.
What is eventual consistency, and where is it used?
Eventual consistency means updates propagate over time, prioritizing availability. Used in NoSQL (e.g., Cassandra) for apps like social media where slight delays are acceptable.
How do you handle database migrations in a production system?
Use tools like Flyway or Liquibase for versioned migrations. Apply changes incrementally, test in staging, and use backward-compatible schemas to avoid downtime.
What is a microservices architecture, and what are its trade-offs?
Microservices split an app into small, independent services (e.g., user service, payment service). Pros: scalability, flexibility. Cons: complexity, inter-service communication overhead.
How do you design a chat application?
Components: WebSocket servers for real-time messaging, database (MongoDB) for message history, Redis for user presence. Scale with sharded message storage and load-balanced servers.
How do you optimize a system for low latency?
Use caching (Redis), CDNs, database indexing, and asynchronous processing (queues). Example: Cache API results, use in-memory DB for hot data, optimize queries with indexes.