#concurrency
36 snippets tagged with #concurrency
Node.js In-Memory Task Queue
A simple in-memory task queue with concurrency control, retries, and priority support.
Best for: Rate-limited API call processing
Promise Concurrency Patterns
Master Promise.all, allSettled, race, any — parallel execution with error handling and timeouts.
Best for: Parallel API calls with error handling
Semaphore for Concurrency Limiting
Control concurrent async operations with a semaphore pattern: rate limiting, connection pooling, and batch processing.
Best for: API rate limiting
Promise Pool for Batch Processing
Process large arrays with controlled concurrency using a promise pool pattern with progress tracking.
Best for: Bulk API requests
Promise Queue with Concurrency Limit
A queue that processes async tasks with a configurable concurrency limit to prevent resource exhaustion.
Best for: Rate-limited API calls
Optimistic Locking with Version Column
Prevent lost updates in concurrent environments using a version column for optimistic concurrency control.
Best for: Multi-user editing
SQL Transaction Isolation Levels
Understand and use transaction isolation levels to control concurrency and data consistency.
Best for: Preventing race conditions in financial transactions
asyncio.gather Concurrent Tasks
Run multiple async operations concurrently with asyncio.gather and proper error handling.
Best for: Parallel API calls
Python Concurrent Futures for Parallel Work
Run tasks in parallel using ThreadPoolExecutor and ProcessPoolExecutor with error handling.
Best for: Parallel HTTP requests for web scraping
Run Async Tasks Concurrently with gather
Execute multiple async operations concurrently using asyncio.gather.
Best for: Parallel API calls
Asyncio Semaphore and Timeout Patterns
Control concurrency with asyncio semaphores, timeouts, and task groups for robust async code.
Best for: rate limiting API calls
Async ETL Pipeline with asyncio
Run concurrent data fetches and transformations using asyncio.gather for high-throughput pipelines.
Best for: concurrent API ingestion
CompletableFuture — Async Programming
Chain async operations with CompletableFuture: thenApply, thenCompose, allOf, and exception handling.
Best for: Parallel API calls to multiple services
Virtual Threads — Lightweight Concurrency
Use Java 21 virtual threads for massive concurrency without thread pool tuning or reactive frameworks.
Best for: High-concurrency web servers handling thousands of requests
ExecutorService — Thread Pool Management
Create and manage thread pools with ExecutorService: fixed, cached, scheduled, and custom pools.
Best for: Managing concurrent task execution
Concurrent Collections — Thread-Safe Maps
Use ConcurrentHashMap, CopyOnWriteArrayList, and BlockingQueue for thread-safe data structures.
Best for: Thread-safe caching in multi-threaded applications
Rate Limiter — Token Bucket Algorithm
Implement a thread-safe rate limiter using the token bucket algorithm for API throttling.
Best for: API rate limiting per user or IP
Connection Pool Implementation
Build a generic connection pool with size limits, idle timeout, health checks, and metrics.
Best for: Database connection pooling
ThreadLocal and Scoped Values
Use ThreadLocal for per-thread data and ScopedValue (Java 21+) for structured concurrency contexts.
Best for: Request-scoped context in web servers
BlockingQueue — Producer Consumer Pattern
Implement producer-consumer with BlockingQueue: bounded buffers, multiple producers, and graceful shutdown.
Best for: Task queue processing systems
Structured Concurrency (Java 21+)
Use StructuredTaskScope for parallel subtasks with automatic cancellation and error propagation.
Best for: Parallel API aggregation with auto-cancellation
Semaphore — Concurrency Control
Control concurrent access with Semaphore: rate limiting, resource pools, and bounded parallelism.
Best for: Limiting concurrent API calls to external services
Fork/Join Framework — Divide and Conquer
Parallelize recursive tasks with ForkJoinPool: RecursiveTask, RecursiveAction, and work-stealing.
Best for: CPU-intensive divide-and-conquer algorithms
CompletableFuture Combinators — allOf, anyOf, compose
Compose async operations with CompletableFuture: allOf, anyOf, thenCombine, handle, and timeout.
Best for: Aggregating results from multiple microservices
Virtual Threads — Practical Patterns
Use Java 21 virtual threads for high-concurrency I/O: per-request threads, structured scopes, and limits.
Best for: High-concurrency I/O-bound servers
Coroutines — launch, async, and Structured Concurrency
Write concurrent code with Kotlin coroutines: launch, async/await, structured concurrency, and dispatchers.
Best for: Parallel API calls in backend services
Channels — Producer-Consumer with Coroutines
Communicate between coroutines with Channels: produce, actor pattern, fan-out/fan-in, and select.
Best for: Producer-consumer patterns in coroutines
Structured Concurrency Patterns
Master coroutine structured concurrency: coroutineScope, async/await, fan-out/fan-in, and parallel map.
Best for: Parallel I/O operations with concurrency limits
Coroutine Channels Producer Consumer
Use Kotlin channels for coroutine communication: buffered, conflated, fan-out, and fan-in patterns.
Best for: Coroutine-based producer-consumer patterns
Coroutine Mutex and Synchronization
Synchronize shared mutable state in coroutines with Mutex, atomic operations, and thread confinement.
Best for: Thread-safe shared state in coroutines
Coroutine Select Expression
Use select expression to await the first result from multiple suspending operations or channels.
Best for: Racing multiple async operations
Futures and Async Programming
Use Scala Futures for async operations: map, flatMap, recover, sequence, and race patterns.
Best for: Asynchronous API calls
Parallel Processing with Collections
Process data in parallel using parallel collections, Future.traverse, and batched execution.
Best for: Speeding up CPU-bound operations
Akka Actor System Basics
Build concurrent applications with Akka actors: message passing, behavior switching, and supervision.
Best for: Concurrent message-passing systems
Future Async Patterns
Work with Scala Futures: composition, error handling, timeout, retry, and parallel execution.
Best for: Asynchronous API calls
ZIO Fibers and Concurrency
Use ZIO fibers for lightweight concurrency: fork, join, race, parallel operations, and supervision.
Best for: Lightweight concurrent task execution