Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 2.87 KB

File metadata and controls

72 lines (53 loc) · 2.87 KB

Latency, Concurrency, and Parallelism in System Design

Understanding latency, concurrency, and parallelism is critical for improving the user experience and ensuring systems scale efficiently. Here's a concise breakdown:

1. What are Async Processes?

  • Async programming allows tasks to run concurrently or in parallel without blocking other operations.
  • Concurrency and parallelism are the two key techniques for managing multiple tasks asynchronously.

2. Impact of Reduced Latency

  • Lower latency improves user experience, enabling quicker responses to user actions.
  • Reducing latency helps businesses react to demands instantly, improving system performance and scalability.

3. Concurrency Example

  • Concurrency: Multiple tasks progress at the same time, but not necessarily simultaneously.
  • Example: A server can handle multiple user requests by switching between tasks without waiting for each to complete.
import asyncio

async def task1():
    print("Task 1 started")
    await asyncio.sleep(1)
    print("Task 1 finished")

async def task2():
    print("Task 2 started")
    await asyncio.sleep(1)
    print("Task 2 finished")

# Running both tasks concurrently
async def main():
    await asyncio.gather(task1(), task2())

asyncio.run(main())

4. Parallelism Example

  • Parallelism: Tasks run simultaneously, using multiple processors or cores.
  • Example: Performing multiple calculations on different cores, reducing overall processing time.
from multiprocessing import Pool

def square(x):
    return x * x

# Running tasks in parallel
with Pool(2) as p:
    results = p.map(square, [1, 2, 3, 4])
print(results)  # Output: [1, 4, 9, 16]

5. Putting Them Together

  • Combining concurrency and parallelism: Use concurrency to manage I/O-bound tasks, and parallelism to process CPU-bound tasks.
  • Example: In a web server, use concurrency to handle multiple incoming requests and parallelism to process data-intensive tasks like image processing.

6. Drawbacks of Async Processes

  • Increased Complexity: Managing concurrency and parallelism introduces complexities, especially when dealing with shared resources.
  • Potential issues with deadlocks, race conditions, and state management.

7. Real-world Examples

  • Web Servers: Modern servers (e.g., Node.js) use concurrency to handle high numbers of simultaneous connections.
  • Data Processing: Systems like Hadoop use parallelism to process large datasets across multiple nodes.

Summary

  • Concurrency helps handle multiple tasks at once, but not necessarily simultaneously.
  • Parallelism runs tasks simultaneously, typically on multiple processors.
  • Both techniques reduce latency and improve performance, but introduce complexity in design and implementation.