Skip to content
intermediatePhase 48 · Distributed Systems

Synchronous vs Asynchronous

Compare blocking vs non-blocking communication patterns.

30m
0 problems
Topic Progress0%

When to Use Each

When to Use Synchronous vs Asynchronous

Decision Framework

Use Synchronous when:

1. Immediate response needed
   - User queries
   - Real-time lookups
   - CRUD operations

2. Simple request-response
   - REST APIs
   - Database queries
   - Function calls

3. Low latency critical
   - User-facing operations
   - Real-time dashboards

4. Strong consistency needed
   - Financial transactions
   - Inventory checks
Use Asynchronous when:

1. Long processing
   - Video encoding
   - Report generation
   - Batch operations

2. Fire-and-forget
   - Email sending
   - Log collection
   - Analytics events

3. Event broadcasting
   - Order notifications
   - Status updates
   - Cache invalidation

4. Buffering needed
   - Traffic spikes
   - Variable load
   - Peak handling

Comparison

Aspect Synchronous Asynchronous
Response Immediate Delayed
Coupling Tight Loose
Scalability Limited by latency Better
Complexity Simple Complex
Reliability Cascading failures Better fault tolerance
Throughput Limited Higher

Patterns

Communication Patterns

Synchronous Patterns

1. Request-Response:
   Client → Request → Server → Response
   Most common pattern

2. Request-Timeout:
   Client → Request → Server
   Client waits for timeout
   Returns error if no response

3. Circuit Breaker:
   Client → Circuit → Server
   Stops calling failing server

Asynchronous Patterns

1. Fire-and-Forget:
   Producer → Queue → Consumer
   No response expected

2. Request-Reply (Async):
   Client → Queue → Server
   Client ← Reply Queue ← Server
   Response comes later

3. Pub-Sub:
   Publisher → Topic → Subscriber 1
                   → Subscriber 2
                   → Subscriber 3

4. Event Sourcing:
   Event → Event Store → Projections
   All state derived from events

Hybrid Pattern

Sync for query, Async for command:

Query (sync):
GET /users/123 → Response immediately

Command (async):
POST /orders → Queue → Process later
             → Return 202 Accepted

CQRS Pattern

Command Query Responsibility Segregation:

Commands (write) → Event Bus → Projections
Queries (read) → Read Model

Separate read and write models

Pattern Selection

Need Pattern
User query Request-Response
Order processing Fire-and-Forget
Notifications Pub-Sub
Audit logging Event Sourcing
Mixed workloads CQRS

Tradeoffs

Tradeoffs Analysis

Tradeoff Matrix

Factor Sync Advantage Async Advantage
Latency Lower Higher
Throughput Lower Higher
Complexity Simpler More complex
Coupling Tighter (simpler) Looser (flexible)
Error handling Easier Harder
Consistency Stronger Eventual
Scalability Limited Better
Debugging Easier Harder

Latency Comparison

Synchronous:
Client → [10ms] → Server → [10ms] → Client
Total: 20ms

Asynchronous:
Client → [1ms] → Queue → Worker → [50ms] → DB
Total to user: 1ms (immediate return)
Total to complete: 52ms

Scalability

Synchronous:
- Each request holds connection
- 1000 concurrent = 1000 connections
- Limited by server capacity

Asynchronous:
- Requests queued
- Workers process at own pace
- Can handle 10000+ requests

Error Handling

Synchronous:
- Immediate error detection
- Simple try-catch
- Caller handles error

Asynchronous:
- Errors discovered later
- Need DLQ for failed messages
- Retry logic needed

Best Practices

  1. Default to async unless sync is needed
  2. Use sync for user-facing queries
  3. Use async for background processing
  4. Combine patterns (CQRS)
  5. Monitor both styles

Practice Problems

0/3solved
Design Synchronous vs Asynchronous System

Design a scalable Synchronous vs Asynchronous system. Cover high-level architecture, data model, and API design.

Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliability
Synchronous vs Asynchronous Scaling

How would you scale Synchronous vs Asynchronous to handle 10x the current load? Identify bottlenecks and solutions.

Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decomposition
Synchronous vs Asynchronous Failure Modes

Analyze potential failure modes for Synchronous vs Asynchronous and design mitigation strategies.

Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradation

Quiz

1. When should you use synchronous communication?

Question 1 options

2. What is the main advantage of asynchronous communication?

Question 2 options

3. What pattern is 'send email and don't wait'?

Question 3 options

4. What is CQRS?

Question 4 options

5. Why is async better for error handling?

Question 5 options

Flashcards

Question

When use synchronous?

Answer

When immediate response needed: user queries, CRUD, real-time lookups, strong consistency

Question

When use asynchronous?

Answer

Long processing, fire-and-forget, event broadcasting, traffic buffering

Question

What is CQRS?

Answer

Command Query Responsibility Segregation - separates read and write models for different optimization

Question

Sync vs async scalability?

Answer

Sync: limited by connections/latency. Async: better, workers process independently, queue buffers.

Question

Default recommendation?

Answer

Default to async unless sync is specifically needed for immediate response or strong consistency

Revision Notes

Key Takeaways

  • 1.Use sync for immediate response needs; async for background processing
  • 2.Async provides better scalability and fault tolerance
  • 3.CQRS separates read and write models
  • 4.Default to async unless sync is specifically required
  • 5.Combine patterns for different workloads

Interview Tips

  • Explain trade-offs clearly (latency, scalability, complexity)
  • Give examples for when to use each
  • Discuss CQRS and event sourcing
  • Mention hybrid approaches

Cheat Sheet

Cheat Sheet: Sync vs Async

Sync

  • Blocks until response
  • Immediate feedback
  • Simple error handling
  • Tight coupling
  • Lower scalability

Async

  • Non-blocking
  • Delayed response
  • DLQ for errors
  • Loose coupling
  • Better scalability

Patterns

  • Sync: Request-Response
  • Async: Fire-and-Forget, Pub-Sub
  • Hybrid: CQRS

Default

  • Async unless sync needed