Skip to main content

Concurrent Collections in C# — Building Thread-Safe Data Structures

Hello, .NET developers! 👋

When your application starts running tasks in parallel — multiple threads reading, writing, and updating data — one of the biggest challenges is data safety. Traditional collections like List<T> or Dictionary<TKey,TValue> are not thread-safe, meaning concurrent modifications can cause exceptions or corrupt data. To solve this, .NET introduced Concurrent Collections — a set of specialized thread-safe classes under System.Collections.Concurrent.

These include ConcurrentDictionary, ConcurrentBag, ConcurrentQueue, and ConcurrentStack. Each serves a different purpose in multi-threaded scenarios, balancing speed, safety, and structure.


ConcurrentDictionary — When You Need Key-Based Access

ConcurrentDictionary<TKey,TValue> is a thread-safe version of Dictionary<TKey,TValue>. It allows multiple threads to read and write simultaneously without locking the entire collection.

Real-Time Use Case

Imagine an API gateway caching user tokens. Each user ID maps to a token. Multiple threads (requests) might try to update or validate the same token concurrently — that’s a perfect case for ConcurrentDictionary.

Example

using System.Collections.Concurrent;

public class TokenCache
{
    private readonly ConcurrentDictionary<string, string> _userTokens = new();

    public void AddOrUpdate(string userId, string token)
    {
        _userTokens.AddOrUpdate(userId, token, (key, oldValue) => token);
    }

    public string GetToken(string userId)
    {
        _userTokens.TryGetValue(userId, out var token);
        return token ?? "Token not found";
    }
}

class Program
{
    static void Main()
    {
        var cache = new TokenCache();
        Parallel.For(0, 100, i =>
        {
            cache.AddOrUpdate($"user{i}", $"token{i}");
        });
        Console.WriteLine($"Total tokens stored: {cache.GetType().GetProperties().Length}");
    }
}

AddOrUpdate and TryGetValue handle concurrency gracefully, ensuring data consistency without explicit locks.


ConcurrentBag — When Order Doesn’t Matter

ConcurrentBag<T> is an unordered, thread-safe collection designed for scenarios where multiple threads add and remove items, but order is irrelevant. It’s efficient because each thread maintains a local storage area to reduce contention with others.

Real-Time Use Case

Imagine a background job scheduler pooling reusable objects — like reusable HttpClient instances, file handles, or task results. You don’t care who gets which one — you just want safe, fast access.

Example

using System.Collections.Concurrent;

public class WorkerPool
{
    private readonly ConcurrentBag<string> _completedJobs = new();

    public void ProcessJob(int jobId)
    {
        _completedJobs.Add($"Job {jobId} completed at {DateTime.Now}");
    }

    public void DisplayResults()
    {
        foreach (var job in _completedJobs)
            Console.WriteLine(job);
    }
}

class Program
{
    static void Main()
    {
        var pool = new WorkerPool();

        Parallel.For(0, 10, i => pool.ProcessJob(i));
        pool.DisplayResults();
    }
}

Since order doesn’t matter, ConcurrentBag optimizes for speed by reducing lock contention — perfect for logging or collection of independent results.


ConcurrentStack — When You Need Last-In, First-Out (LIFO)

ConcurrentStack<T> behaves like a traditional stack, but it’s safe for multiple threads to push or pop items simultaneously. It’s ideal when the latest data should be processed first.

Real-Time Use Case

Think of an image processing service where new tasks arrive faster than old ones are processed. You might prioritize the most recent images (LIFO order) to give users faster feedback.

Example

using System.Collections.Concurrent;

public class ImageProcessor
{
    private readonly ConcurrentStack<string> _images = new();

    public void AddImage(string fileName)
    {
        _images.Push(fileName);
    }

    public void ProcessImages()
    {
        while (_images.TryPop(out var file))
        {
            Console.WriteLine($"Processing {file}");
        }
    }
}

class Program
{
    static void Main()
    {
        var processor = new ImageProcessor();
        Parallel.For(1, 5, i => processor.AddImage($"image_{i}.png"));
        processor.ProcessImages();
    }
}

ConcurrentStack ensures the most recently added image is processed first without worrying about synchronization issues.


ConcurrentQueue — When You Need First-In, First-Out (FIFO)

ConcurrentQueue<T> guarantees that items are processed in the same order they were added, even under concurrent access. It’s the backbone of many message-queue or producer-consumer systems in .NET.

Real-Time Use Case

Consider a real-time chat application or task queue where messages must be delivered in sequence — no skipping or reordering allowed. ConcurrentQueue makes sure every producer thread adds messages safely, and consumer threads dequeue them correctly.

Example

using System.Collections.Concurrent;

public class MessageQueue
{
    private readonly ConcurrentQueue<string> _messages = new();

    public void EnqueueMessage(string message)
    {
        _messages.Enqueue(message);
    }

    public void ProcessMessages()
    {
        while (_messages.TryDequeue(out var msg))
        {
            Console.WriteLine($"Delivered: {msg}");
        }
    }
}

class Program
{
    static void Main()
    {
        var queue = new MessageQueue();

        Parallel.For(1, 6, i => queue.EnqueueMessage($"Message {i}"));
        queue.ProcessMessages();
    }
}

In high-throughput systems, ConcurrentQueue helps you maintain reliable message flow without race conditions.


Choosing the Right Concurrent Collection

Each concurrent collection has its own specialty:

ConcurrentDictionary → Key/value lookups and updates (like caches or session stores). ConcurrentBag → Unordered data collection (parallel results, logs, jobs). ConcurrentStack → LIFO structure (undo operations, latest-first processing). ConcurrentQueue → FIFO structure (message queues, background task pipelines).

All of them are lock-free (internally use fine-grained synchronization), optimized for multi-threaded workloads, and designed for modern parallelism using Task and Parallel.For.


Wrapping Up

Concurrent collections make multi-threaded programming approachable and reliable. Instead of locking entire collections manually, these classes handle synchronization under the hood, providing both speed and safety. Whether you’re caching tokens, processing images, queuing messages, or gathering results from background tasks — these structures let multiple threads cooperate without colliding.

Comments

Popular posts from this blog

Implementing and Integrating RabbitMQ in .NET Core Application: Shopping Cart and Order API

RabbitMQ is a robust message broker that enables communication between services in a decoupled, reliable manner. In this guide, we’ll implement RabbitMQ in a .NET Core application to connect two microservices: Shopping Cart API (Producer) and Order API (Consumer). 1. Prerequisites Install RabbitMQ locally or on a server. Default Management UI: http://localhost:15672 Default Credentials: guest/guest Install the RabbitMQ.Client package for .NET: dotnet add package RabbitMQ.Client 2. Architecture Overview Shopping Cart API (Producer): Sends a message when a user places an order. RabbitMQ : Acts as the broker to hold the message. Order API (Consumer): Receives the message and processes the order. 3. RabbitMQ Producer: Shopping Cart API Step 1: Install RabbitMQ.Client Ensure the RabbitMQ client library is installed: dotnet add package RabbitMQ.Client Step 2: Create the Producer Service Add a RabbitMQProducer class to send messages. RabbitMQProducer.cs : using RabbitMQ.Client; usin...

How Does My .NET Core Application Build Once and Run Everywhere?

One of the most powerful features of .NET Core is its cross-platform nature. Unlike the traditional .NET Framework, which was limited to Windows, .NET Core allows you to build your application once and run it on Windows , Linux , or macOS . This makes it an excellent choice for modern, scalable, and portable applications. In this blog, we’ll explore how .NET Core achieves this, the underlying architecture, and how you can leverage it to make your applications truly cross-platform. Key Features of .NET Core for Cross-Platform Development Platform Independence : .NET Core Runtime is available for multiple platforms (Windows, Linux, macOS). Applications can run seamlessly without platform-specific adjustments. Build Once, Run Anywhere : Compile your code once and deploy it on any OS with minimal effort. Self-Contained Deployment : .NET Core apps can include the runtime in the deployment package, making them independent of the host system's installed runtime. Standardized Libraries ...

Clean Architecture: What It Is and How It Differs from Microservices

In the tech world, buzzwords like   Clean Architecture   and   Microservices   often dominate discussions about building scalable, maintainable applications. But what exactly is Clean Architecture? How does it compare to Microservices? And most importantly, is it more efficient? Let’s break it all down, from understanding the core principles of Clean Architecture to comparing it with Microservices. By the end of this blog, you’ll know when to use each and why Clean Architecture might just be the silent hero your projects need. What is Clean Architecture? Clean Architecture  is a design paradigm introduced by Robert C. Martin (Uncle Bob) in his book  Clean Architecture: A Craftsman’s Guide to Software Structure and Design . It’s an evolution of layered architecture, focusing on organizing code in a way that makes it  flexible ,  testable , and  easy to maintain . Core Principles of Clean Architecture Dependency Inversion : High-level modules s...