Senior & Lead Engineer
Interview Mastery Hub
100 real-world, scenario-based architecture & concurrency questions designed for 5–10+ years experienced developers. Free & open for every student and engineer.
100
Real-world Q&As
100%
Scenario-Based
5-10 Yrs
Senior / Lead Depth
5 Stacks
Core Technologies
Showing 20 questions for senior interview preparation
How does the Redis Redlock Algorithm provide Fault-Tolerant Distributed Locking across independent Redis nodes?
Why a Single Redis Master is Insufficient for Strict Distributed Locking
In a standard Master-Replica Redis setup, replication is asynchronous. If the master crashes immediately after granting a lock before replicating to replicas, the promoted replica can grant the exact same lock to a second client, causing catastrophic double-execution.
The Redlock Algorithm Workflow
- Client initializes $N$ independent Redis master nodes (e.g. $N=5$).
- Client gets current millisecond timestamp $T_1$.
- Client attempts to acquire the lock sequentially across all $N$ instances using identical key, random unique value, and TTL (e.g. 10s) with a short timeout (e.g. 50ms) per node.
- Client computes elapsed time $Elapsed = T_2 - T_1$.
- Lock Acquisition Success: If the client acquired the lock on majority of nodes (>= 3 out of 5) AND $Elapsed < TTL$, the lock is valid for $ValidityTime = TTL - Elapsed$.
- Failure & Rollback: If client failed to acquire on majority, it immediately sends an unlock script (
Lua EVAL) to all instances, even those where it thought acquisition failed.
Explain the 3 High-Scale Cache Failure Patterns: Cache Avalanche, Cache Breakdown (Dog-Piling), and Cache Penetration, with mitigations.
1. Cache Avalanche (Mass Simultaneous Expiration)
- Problem: Millions of keys expire at the same instant; massive traffic rushes to the DB.
- Solution: Add Random TTL Jitter:
$ttl = 3600 + rand(0, 600); // 60 to 70 minutes randomized expiration
2. Cache Breakdown / Dog-Piling (Hot Key Expiry)
- Problem: A single viral product key expires; 20,000 concurrent requests miss simultaneously.
- Solution: Distributed Mutex Lock or Background Pre-warming / Stale-While-Revalidate.
3. Cache Penetration (Non-Existent Keys Attack)
- Problem: Malicious requests query non-existent IDs (
id = -9999), bypassing cache and hitting DB on every request. - Solution:
- Cache Empty Objects / Nulls with short TTL (e.g. 60 seconds).
- Bloom Filter: Use Redis Bloom filter to determine in $O(1)$ whether an ID could possibly exist before querying database.
Compare Rate Limiting Algorithms: Token Bucket, Leaky Bucket, Fixed Window Counter, and Sliding Window Log/Counter.
Comparison Matrix
| Algorithm | Pros | Cons | Ideal For |
|---|---|---|---|
| Token Bucket | Allows bursts up to bucket capacity, smooth refills | Memory state tracking required | API Gateways (Stripe / GitHub style) |
| Leaky Bucket | Guarantees constant, smooth output rate | Discards bursts if queue full | Egress traffic shaping |
| Fixed Window | Low memory | Boundary spikes (2x burst at window edges) | Basic IP throttling |
| Sliding Window Counter | Prevents boundary spikes, memory efficient | Approximate calculation | High-scale tier limiting |
Sliding Window Lua Script in Redis
local key = KEYS[1]
local now = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local limit = tonumber(ARGV[3])
-- Remove timestamps older than current sliding window
redis.call('ZREMRANGEBYSCORE', key, '-inf', now - window)
local current_count = redis.call('ZCARD', key)
if current_count < limit then
redis.call('ZADD', key, now, now)
redis.call('EXPIRE', key, window)
return 1 -- Allowed
else
return 0 -- Blocked (HTTP 429)
end
How do you design a Real-Time Notification System delivering to 10M concurrent users with WebSockets and Redis Pub/Sub?
Architectural Blueprint
[Client Socket] ---> [Load Balancer / Nginx] ---> [WebSocket Gateway Cluster (Node/Go/Reverb)]
^
| Redis Pub/Sub Cluster / Kafka
|
[Notification Service]
Core Architecture Components:
- Stateful Connection Nodes: Gateway instances maintain persistent TCP socket connections and keep a local in-memory lookup map of
user_id -> [socket_fd_1, socket_fd_2]. - Channel Partitioning in Redis Pub/Sub: Broadcast messages are partitioned by tenant or topic channels (
broadcast:globalvsuser:12345). - Backpressure & Drop Policies: If a client's TCP socket buffer is saturated, drop non-critical status updates to prevent memory exhaustion on the gateway node.
How do you design an Idempotent Payment Processing Architecture with the Saga Pattern across Microservices?
Choreography vs Orchestration Saga
Because 2-Phase Commit (2PC) blocks distributed databases, distributed systems use Saga Pattern:
- A sequence of local transactions where each step publishes an event.
- If any step fails, the Saga executes Compensating Transactions in reverse order (e.g.
RefundPayment,RestoreInventory).
[Order Created] -> (Deduct Inventory) -> (Charge Stripe) -> [Invoice Failed]
|
v
[Trigger Compensating Action]
Refund Stripe
Restore Inventory
Mark Order Cancelled
Idempotency Keys:
Each step checks if the order_id has already been processed to safely allow retries without duplicate charges.
How does the Circuit Breaker Pattern prevent cascading failures in Microservice architectures?
The 3 States of Circuit Breaker
- Closed: Normal state. Requests pass through. Tracks failure rate.
- Open: If failure rate exceeds threshold (e.g. 50% over 10s), the circuit trips to OPEN. Requests fail immediately with a fallback response without calling the slow downstream service.
- Half-Open: After a cooldown period (e.g. 30s), a limited trial number of requests are allowed through. If they succeed, circuit resets to CLOSED; if they fail, it returns to OPEN.
How do you design a Global URL Shortener (Bitly-scale) handling 100M URLs and 10 Billion clicks/month?
Key Architecture Decisions
- Base62 Encoding:
[a-z, A-Z, 0-9]. A 7-character string allows $62^7 \approx 3.5 \text{ trillion}$ unique URLs. - Distributed ID Generator (Snowflake / Redis Atomic Counters): Generate unique 64-bit auto-incrementing integer -> Convert to Base62.
- Caching & CDN:
- Return HTTP 302 (Temporary Redirect) if click tracking analytics are required on every hit.
- Return HTTP 301 (Permanent Redirect) if latency is prioritized (browser caches redirect permanently).
- Cache hot 20% of URLs in Redis cluster (80/20 Pareto principle).
Explain the CAP Theorem and PACELC Theorem with real-world database trade-offs.
CAP Theorem: Consistency, Availability, Partition Tolerance
In the presence of a Network Partition ($P$), a distributed system must choose between:
- $CP$ (Consistency): Reject writes to maintain data accuracy (e.g. Redis Cluster, MongoDB, HBase).
- $AP$ (Availability): Accept writes on both sides of partition; resolve conflicts later (e.g. DynamoDB, Cassandra).
PACELC Theorem (Extending CAP for Normal Operations)
If Partition ($P$): Choose between Availability ($A$) or Consistency ($C$). Else ($E$): Choose between Latency ($L$) or Consistency ($C$).
How do you implement Async Worker Retries with Exponential Backoff and Dead Letter Queues (DLQ)?
Exponential Backoff with Jitter
$retryDelay = min(3600, pow(2, $attemptCount) * 10 + rand(0, 5));
- Attempt 1: 20s
- Attempt 2: 40s
- Attempt 3: 80s
- Max Retries Exceeded: Route the failed payload to a Dead Letter Queue (DLQ) for automated alerting and manual developer replay.
How do you design a High-Scale Distributed Counter for Millions of Video Views or Upvotes?
Sharded Counters / Write-Back Buffering
- Redis Atomic Increment:
Redis::incr("video:views:{$videoId}"); - Batch Flusher Daemon: Every 10 seconds, a background worker consumes the aggregated count from Redis via
INCRBYand writes a single consolidated SQL update:UPDATE videos SET views = views + 5000 WHERE id = 5;
Reduces database write operations by 99.9%.
How does Consistent Hashing minimize data movement during Cache Cluster node addition or removal?
Consistent Hashing Ring
- Maps both Cache Servers and Keys onto a 360-degree circular hash ring (e.g. 0 to $2^{32}-1$).
- A key is assigned to the nearest server located clockwise on the ring.
- When a server is added or removed, only $K/N$ keys need remapping, preventing total cache thrashing.
- Virtual Nodes: Assigns multiple virtual points per physical server to ensure even distribution across nodes.
Explain the Difference between Polling, Long-Polling, Server-Sent Events (SSE), and WebSockets.
Protocol Comparison
| Protocol | Direction | Overhead | Ideal Use Case |
|---|---|---|---|
| Short Polling | Client -> Server | High (repetitive HTTP headers) | Simple status checks |
| Long Polling | Client -> Server (held open) | Medium | Fallback when WebSockets blocked |
| Server-Sent Events (SSE) | Server -> Client Only | Very Low (Standard HTTP) | Live Feed, Stock Tickers, AI Streaming text |
| WebSockets | Full Duplex Bi-directional | Minimal (Single TCP handshake) | Multiplayer Games, Chat, Collaborative Editing |
How do you design a Resilient Multi-Region Disaster Recovery architecture (Active-Active vs Active-Passive)?
Active-Active vs Active-Passive
- Active-Passive (Warm Standby): Primary region handles 100% traffic. Secondary region replicates data. DNS failover (Route 53) flips traffic if primary fails. RTO: 2-5 mins.
- Active-Active (Multi-Region): Both US-East and EU-Central serve local traffic. Requires globally distributed conflict-free replicated data types (CRDTs) or distributed database like CockroachDB / Spanner.
How does Log Aggregation and Distributed Tracing work with OpenTelemetry, Jaeger, and ELK Stack?
Distributed Context Propagation
- The edge API gateway generates a unique
trace_idandspan_id. - Passed downstream across all HTTP/gRPC headers via W3C Trace Context (
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01). - Each microservice logs the
trace_idand exports span metrics to OpenTelemetry collector / Jaeger for visual timeline waterfalls.
How do you secure Microservices using Mutual TLS (mTLS) and OAuth2 Token Exchange in a Zero-Trust architecture?
Mutual TLS (mTLS) with Service Mesh (Istio / Linkerd)
- Both client and server authenticate each other using X.509 cryptographic certificates rotated automatically by the service mesh control plane.
- All internal pod-to-pod network traffic is encrypted with TLS 1.3.
- Combined with short-lived JWT scoped tokens to verify service-level authorization.
How do you design a High-Throughput Leaderboard System with Millions of Scores using Redis Sorted Sets (`ZSET`)?
Redis Sorted Sets ($O(\log N)$ Complexity)
# Add / update player score
ZADD leaderboard 4500 "user_9821"
# Get user current global rank (0-indexed)
ZREVRANK leaderboard "user_9821"
# Fetch top 10 players with scores
ZREVRANGE leaderboard 0 9 WITHSCORES
Internal SkipList data structure delivers sub-millisecond lookups across millions of elements.
How does Database Connection Starvation happen during traffic spikes and how do you configure Maximum Connection limits?
Connection Pooling with ProxySQL / PgBouncer
- Without connection pooling: $400 \text{ pods} \times 20 \text{ connections} = 8,000 \text{ open DB connections}$, overwhelming database thread management.
- With Multiplexing Proxy: 8,000 application client connections multiplex over 50 persistent, high-speed connections to MySQL, keeping DB CPU in the sweet spot.
Explain Write-Through vs. Write-Behind (Write-Back) vs. Cache-Aside caching strategies.
Caching Strategies
- Cache-Aside (Lazy Loading): Application reads cache; on miss, reads DB and writes to cache. Standard for most web apps.
- Write-Through: Application writes to cache, and cache synchronously writes to DB before returning. Consistency guaranteed, higher write latency.
- Write-Back (Write-Behind): Application writes to cache immediately; cache asynchronously flushes batch writes to DB in the background. High write performance, small risk of data loss if cache crashes before sync.
How do you design a Secure Content Delivery Network (CDN) Token Authentication system for private video streaming?
HMAC-Signed URLs with Expiration & IP Binding
- Backend generates an HMAC-signed URL containing:
path,expires_timestamp, and optionalclient_ip. - Cloudflare / CloudFront edge worker validates the signature in $O(1)$ at the edge node:
https://cdn.example.com/video/master.m3u8?token=a8f9c1...&exp=1772189400 - If token expired or tampered, edge rejects with HTTP 403 without touching origin servers.
How do you design a Scalable Full-Text Search Engine with Elasticsearch / Meilisearch synchronizing with relational databases?
Change Data Capture (CDC) via Debezium & Kafka
Instead of dual-writing in application code (which risks desynchronization if search fails):
- Database writes to transaction WAL (Binlog).
- Debezium CDC connector tails the Binlog and streams row change events to Kafka.
- An indexing consumer batch writes updates into Elasticsearch / Meilisearch. Guarantees 100% data consistency with sub-second synchronization latency.