Skip to main content

C# - Fine Tuning Your Processor/IO Load With SemaphoreSlim

My photo
Introduction
It is very important to predict the behavior of our system at the early stage of our development, Things like understanding the potential user loads, migration strategy, security routine and many more, these activity will consume system resources and without proper planning design, it might impact the performance/delivery of our system. In this article we will discuss on how to limit our solution tasks ( usually long running large processing task ) using SemaphoreSlim, SemaphoreSlim will limit a task from running concurrently based on a given number, with these limitation, we will somehow reduce the possibility that some tasks that consuming large resources will not bring a huge impact to other small processing tasks from doing its job.

The Problem Statement
Assuming we are using some sort of custom encryption to store our data at a data store, take an example a NoSQL database, we don't want the data that live in the data store to be in plain text ( due to company policy ). Somehow the data need to be encrypted with a Key, And These database consists of a very huge tables, and each table represent specific client, Each client will receive their own key so that they able to encrypt and decrypt the data for their business needs. At some point a certain company require a policy where a Key must be revoked every 6 months for security purpose. Which means all data in a specific table need to be tampered with a new key. We are trying to take advantage of SemaphoreSlim to perform the task to reduce the risk of system failure. We are allowed to perform only 5 key revoke concurrently

Consideration
This code base is proposed for the intention of specific problem domain that we might encounter throughout our development lifecycle, for production/release we might need to tweak it to meet the company policy, best practice and industry standards. The code snippet is free to use anywhere.

The SemaphoreSlim Implementation
From the code above, We instantiate a SemaphoreSlim class with a limit of 5 for each concurrent state. then we use .Wait() to block the thread to check if we the thread is open for long running tasks, if its open, it may then proceed with its task, Finally it will release it to allow other 'key revoke' task to run.

The Impact
Assuming that we have a lot of IO operation at our data store Machine, with this technique, we will reduce the impact of the Key revoking activity from distorting the IO operation of other business operations.

Conclusion
From The Article, we may conclude that at some situation, we might take advantage of SemaphoreSlim to limit our long running task from impacting the performance of other business operation tasks.

Published on : 29-Dec-2018
Ref no : DDN-WPUB-000213

About Author

My photo
Wan Mohd Adzha MCPD,MCSD,MCSE
I am passionate about technology and of course love Durians. Certified by Microsoft as MCP Since 2011. Blogging from Malaysia

Comments