HLD · 10 min read

Tricky HLD Patterns for interviews

Commonly missed high-level design patterns for interviews.

On this page
  1. 1. Write-Behind (Write-Back) Cache
  2. Overview
  3. Real-World Example
  4. Advantages
  5. Drawbacks
  6. Best Use Cases
  7. 2. Thundering Herd Problem
  8. Overview
  9. Example Scenario
  10. Common Causes
  11. Solutions
  12. Real-World Example
  13. Advantages of Solving It Properly
  14. 3. Distributed Transactions & Two-Phase Commit (2PC)
  15. Overview
  16. How Two-Phase Commit Works
  17. Example
  18. Advantages
  19. Drawbacks
  20. Modern Alternatives
  21. 4. Materialized View Pattern
  22. Overview
  23. Workflow
  24. Advantages
  25. Drawbacks
  26. Common Refresh Strategies
  27. Best Use Cases
  28. 5. Bulk Processing Pattern
  29. Overview
  30. Real-World Examples
  31. Advantages
  32. Drawbacks
  33. Best Use Cases
  34. 6. Read Replica Architecture
  35. Overview
  36. Architecture
  37. Benefits
  38. Challenges
  39. Real-World Example
  40. Best Use Cases
  41. 7. Global Rate Limiting
  42. Overview
  43. Why Local Rate Limiting Fails
  44. Common Algorithms
  45. Real-World Examples
  46. Advantages
  47. Drawbacks
  48. Summary

7 Important Backend & Distributed System Design Patterns

Modern backend systems are no longer built as single monolithic applications running on one machine. Today's applications serve millions of users, process enormous amounts of data, and must remain reliable even during failures, traffic spikes, and network instability. To achieve this level of scalability and resilience, engineers rely on proven architectural and distributed system patterns. These patterns are used daily in companies like Netflix, Amazon, Uber, Google, and Meta.

This article explores seven essential backend and distributed system patterns:

  1. Write-Behind (Write-Back) Cache
  2. Thundering Herd Problem
  3. Distributed Transactions & Two-Phase Commit (2PC)
  4. Materialized View Pattern
  5. Bulk Processing Pattern
  6. Read Replica Architecture
  7. Global Rate Limiting

1. Write-Behind (Write-Back) Cache

Overview

The Write-Behind Cache pattern improves write performance by temporarily storing writes in a fast cache layer before asynchronously persisting them to the database.

So, instead of every write operation directly hitting the database, the application writes data into a cache such as Redis or Memcached. A background worker later flushes those changes to the database in batches.

This reduces database write pressure and improves latency.

Workflow:

  1. Client sends a write request.
  2. Data is written into the cache immediately.
  3. User receives a success response quickly.
  4. Background workers periodically persist data into the database.
mermaid
flowchart LR
    Client -->|"1. write"| Cache[(Cache)]
    Cache -->|"2. OK, immediately"| Client
    Cache -.->|"3. flush in batches, later"| Worker[Background worker]
    Worker --> DB[(Database)]

Databases are often the bottleneck in high-write systems. Writing every request synchronously can increase latency and overload the database.

Write-behind caching solves this by:

  • Reducing database calls
  • Batching writes together
  • Improving response times
  • Handling traffic bursts more efficiently

(For how it compares with cache-aside and write-through, see cache writing strategies in the Distributed Cache design.)

Real-World Example

Social media platforms use this technique for metrics such as:

  • Likes
  • View counts
  • Post impressions
  • Analytics counters

For example, a platform like Instagram has no need to update the database synchronously every time someone views a story. Instead, counters can be accumulated in memory and flushed periodically.

Advantages

  • Extremely fast write performance
  • Reduced database load
  • Better throughput
  • Efficient batching

Drawbacks

  • Risk of data loss if the cache crashes before persistence (Redis's AOF persistence narrows this window, but does not close it completely)
  • Eventual consistency instead of immediate consistency
  • Requires retry and recovery mechanisms

Best Use Cases

  • Analytics systems
  • Counters and metrics
  • Logging systems
  • High-frequency event ingestion

2. Thundering Herd Problem

Overview

The Thundering Herd Problem occurs when many clients simultaneously request the same resource after a cache miss or service recovery. Instead of one request rebuilding the cache, thousands of requests hit the backend at the same time, overwhelming the database or service. When it is caused by a cache entry expiring, it is also called a cache stampede.

Example Scenario

Imagine an e-commerce application where product details are cached for 30 minutes.

When the cache expires:

  • Thousands of users request the same product
  • All requests bypass the cache
  • Every request hits the database
  • Database CPU spikes dramatically
  • System latency increases

This sudden flood of requests is called a "thundering herd."

Common Causes

  • Cache expiration
  • Service restart
  • Traffic spikes
  • Sudden reconnect storms

Solutions

  1. Request Coalescing: Allow only one request to rebuild the cache while others wait.
  2. Distributed Locks: Use Redis locks or ZooKeeper locks to ensure only one process performs expensive recomputation.
  3. Randomized Cache Expiry: Add a little random jitter to each TTL so cache entries don't all expire simultaneously.
  4. Stale-While-Revalidate: Serve stale data temporarily while asynchronously refreshing the cache.
  5. Rate Limiting: Protect downstream systems from overload.
text
Without coalescing:  1,000 requests miss → 1,000 database queries
With coalescing:     1,000 requests miss → 1 query rebuilds the cache,
                                            999 wait and read the new value

(The Redis guide's caching patterns show a lock-based version of this in code.)

Real-World Example

During flash sales on Amazon or Flipkart, cache invalidation can cause massive bursts of database traffic if not carefully handled.

Advantages of Solving It Properly

  • Improved system stability
  • Reduced database overload
  • Better user experience
  • Higher availability during traffic spikes

3. Distributed Transactions & Two-Phase Commit (2PC)

Overview

In distributed systems, a single business transaction may involve multiple services and databases.

For example: Order Service + Payment Service + Inventory Service.

If one service succeeds and another fails, the system may become inconsistent.

Distributed transactions solve this problem by ensuring all participating services either:

  • Commit successfully together
  • Roll back together

One of the most well-known approaches is the Two-Phase Commit (2PC) protocol.

How Two-Phase Commit Works

Phase 1: Prepare Phase

The coordinator asks all participants whether they are ready to commit.

Each participant:

  • Validates the transaction
  • Locks required resources
  • Records its intended changes in a durable log, so it can still commit after a crash
  • Responds with YES or NO

Phase 2: Commit Phase

If all participants respond YES:

  • Coordinator sends COMMIT
  • Every participant permanently applies changes

If any participant responds NO:

  • Coordinator sends ROLLBACK
  • All changes are reverted

Example

Consider a banking transfer:

  • Debit money from Account A
  • Credit money into Account B

Both operations must succeed together.

If debit succeeds but credit fails, money disappears from the system, which is an unacceptable state.

2PC guarantees atomicity across distributed services.

Advantages

  • Strong consistency
  • Atomic transactions across services
  • Reliable state management

Drawbacks

  • High latency
  • Blocking behavior: if the coordinator crashes after participants vote YES, they are stuck holding locks until it recovers (why this happens)
  • Reduced scalability
  • Coordinator becomes a bottleneck
  • Difficult recovery scenarios

Modern Alternatives

Because 2PC can be expensive and slow, many modern systems prefer:

Real-World Usage: Traditional banking systems and financial platforms commonly use distributed transaction mechanisms because consistency is critical.

4. Materialized View Pattern

Overview

A Materialized View is a precomputed and stored query result used to optimize expensive read operations. Instead of executing complex joins and aggregations repeatedly, the system calculates results beforehand and stores them. This significantly improves read performance.

Complex analytical queries can become very expensive in large-scale systems.

For example:

  • Dashboard analytics
  • Sales reports
  • User statistics
  • Recommendation summaries

Running those queries in real time can overload the database.

Workflow

  1. Raw data is stored normally.
  2. Background jobs aggregate and process data.
  3. Precomputed results are stored separately.
  4. Applications query the materialized view directly.

Some databases support this directly. In PostgreSQL, for example:

sql
CREATE MATERIALIZED VIEW daily_sales AS
  SELECT order_date, SUM(amount) AS revenue
  FROM orders
  GROUP BY order_date;

-- re-run the query and store fresh results
REFRESH MATERIALIZED VIEW daily_sales;

In microservices, the same idea is built from events instead, with one service keeping a materialized view of data owned by others.

Real-World Example: YouTube analytics dashboards do not calculate total views, watch time, and engagement metrics in real time for every page load. Instead, metrics are periodically aggregated and stored.

Advantages

  • Extremely fast reads
  • Reduced query complexity
  • Lower database CPU usage
  • Better dashboard performance

Drawbacks

  • Data may become stale
  • Requires synchronization logic
  • Additional storage cost
  • Complexity in refresh mechanisms

Common Refresh Strategies

StrategyHow it works
Scheduled RefreshRefresh every few minutes or hours
Incremental RefreshOnly update changed data
Event-Driven RefreshUpdate materialized views whenever events occur

Best Use Cases

  • Analytics dashboards
  • Reporting systems
  • Recommendation engines
  • Data warehousing

5. Bulk Processing Pattern

Overview

Bulk processing combines multiple small operations into larger batches. Instead of processing requests individually, the system groups them together to improve efficiency. This pattern is heavily used in distributed systems because network calls, disk operations, and database writes are expensive.

Instead of inserting 10,000 rows one-by-one, the system:

  • Groups rows into batches
  • Inserts them together
  • Reduces network overhead
  • Improves throughput dramatically
sql
-- One round trip and one transaction for many rows,
-- instead of one of each per row
INSERT INTO page_views (user_id, page, viewed_at) VALUES
  (1, '/home',    '2026-05-19 10:00:01'),
  (2, '/pricing', '2026-05-19 10:00:01'),
  (3, '/home',    '2026-05-19 10:00:02');

Every database operation has overhead:

  • Connection handling
  • Disk synchronization
  • Transaction setup
  • Network latency

Batching reduces these repeated costs.

Real-World Examples

  • Email Systems: Bulk email services send messages in batches instead of individually.
  • Kafka Consumers: Kafka consumers process messages in batches to maximize throughput.
  • Payment Systems: Banks process settlement transactions in groups.

Advantages

  • Better throughput
  • Lower infrastructure cost
  • Reduced network calls
  • Improved CPU and disk utilization

Drawbacks

  • Increased latency for individual items
  • Partial batch failure complexity
  • Harder retry handling
  • Requires batch sizing optimization

Best Use Cases

  • ETL pipelines
  • Data ingestion systems
  • Notification services
  • Logging systems
  • Analytics pipelines

6. Read Replica Architecture

Overview

As applications grow, database read traffic often becomes much larger than write traffic. A single database server may struggle to handle:

  • User queries
  • Search operations
  • Dashboard reads
  • Analytics requests

Read replicas solve this problem by creating multiple copies of the primary database.

Architecture

Primary databaseRead replicas
HandlesINSERT, UPDATE, DELETESELECT queries, reporting, search operations, analytics reads

Data is usually replicated asynchronously from the primary database to the replicas. (This is the single-leader replication model from the Distributed Systems series.)

text
Writes ──► Primary ──async replication──► Replica 1
                                     ├──► Replica 2
                                     └──► Replica 3
Reads  ──► spread across the replicas

Benefits

  • Improved Scalability: Read traffic is distributed across multiple servers.
  • Reduced Primary Load: The primary database focuses on writes.
  • Better Availability: If one replica fails, others continue serving traffic.
  • Faster Query Performance: Heavy read queries no longer compete with write operations.

Challenges

  • Replication Lag: Replicas may not immediately reflect the latest writes.
  • Eventual Consistency: Users may temporarily see stale data. A common fix is to route a user's own reads to the primary for a short time after they write.
  • Failover Complexity: Promoting replicas during outages can be complicated.

Real-World Example

Social media platforms often use read replicas for:

  • Feed generation
  • Profile viewing
  • Search functionality
  • Recommendation systems

Meanwhile, writes still go to the primary database.

Best Use Cases

  • Read-heavy applications
  • Analytics systems
  • Content platforms
  • Large-scale APIs

7. Global Rate Limiting

Overview

Rate limiting controls how many requests a user or client can make within a specific time window. Without rate limiting, systems become vulnerable to:

  • Abuse
  • DDoS attacks
  • Traffic spikes
  • Resource exhaustion

Global rate limiting becomes especially important in distributed architectures where traffic is served by multiple application servers.

Why Local Rate Limiting Fails

If each server independently tracks request counts:

  • Limits become inconsistent
  • Users can bypass restrictions by hitting different servers

For example, with a limit of 100 requests per minute and 5 servers each counting on their own, a user spread across all 5 could actually make 500.

Global rate limiting solves this using centralized storage such as Redis. The check and the increment must happen as one atomic step (for example with INCR, or a Lua script for more complex algorithms), otherwise two servers can read the same count at the same moment and both let a request through.

Common Algorithms

AlgorithmHow it works
Token BucketTokens refill at a fixed rate. Requests consume tokens
Leaky BucketRequests are processed at a constant rate
Sliding WindowTracks requests within a moving time window
Fixed Window CounterSimpler but less accurate, since a burst at the end of one window plus the start of the next can let through up to twice the limit

(The Rate Limiter design walks through all four in detail.)

Real-World Examples

  • API Gateways: Platforms like Stripe and Twitter limit API requests.
  • Login Systems: Prevent brute-force attacks.
  • Payment Systems: Protect expensive financial operations.
  • AI Platforms: Control usage costs and prevent overload.

Advantages

  • Protects infrastructure
  • Improves reliability
  • Prevents abuse
  • Enables fair resource allocation
  • Helps control operational costs

Drawbacks

  • Additional infrastructure complexity
  • Incorrect limits can hurt user experience
  • Distributed synchronization challenges

Common tools used for rate limiting:

  • Redis
  • NGINX
  • Envoy Proxy
  • API Gateways
  • Cloudflare

Summary

These seven patterns represent some of the most practical and widely used techniques in modern backend engineering.

Each pattern solves a specific category of problems:

PatternWhat it gives you
Write-Behind CacheFaster writes
Thundering Herd protectionStability during traffic spikes
Distributed TransactionsStrong consistency
Materialized ViewsFaster reads
Bulk ProcessingHigher throughput
Read ReplicasRead scalability
Global Rate LimitingSystem protection

Once you understand these patterns deeply, you begin to think more like a systems architect.