Home > Uncategorized > Using #AWS #SQS in C# – Decouple database inserts

Using #AWS #SQS in C# – Decouple database inserts

Amazon SQS or Simple Queue Service is AWS’s answer to Microsoft’s MSMQ, for those who remember it. It’s a great way to decouple an application from database inserts.

So, for example, if your application logs activity to a database, and you want to seperate conerns, so that in the event of a database outage, you don’t loose your data.

Because typically, you can do only one of two things, in the case of a database outage in this case, you can ignore the data, and loose it. or you can keep it in memory, and hope that the application pool doesn’t recycle or server doesn’t reboot. Or you can store it in local persistant storage – but this is going to start becoming complicated to manage.

SQS offers a way that you can send your data to a queue, and some other process can read it back out, and store it in a database. If that process fails to write the data, it just leaves it in the queue, until such time as the database is back online.

So, without further ado, here is the code;

First install the Nuget package for AWS SQS:

Install-Package AWSSDK.SQS

Then create the SQS client as follows (I’ve omitted my IAM user and password)

public static AmazonSQSClient CreateClient()
{
var sqsConfig = new AmazonSQSConfig
{
RegionEndpoint = RegionEndpoint.EUWest1
};
const string iamUser = "xxxxx";
const string iamPass = "xxxxx";
var awsCredentials = new BasicAWSCredentials(iamUser, iamPass) ;
return new AmazonSQSClient(awsCredentials, sqsConfig);
}

Then, here I am writing some code to write a new message to the queue

const string strSqsUrl = "https://sqs.eu-west-1.amazonaws.com/005445879168/helloWorld";
          
var client = CreateClient();

var sendMessageRequest = new SendMessageRequest
{
 QueueUrl = strSqsUrl,
mMessageBody = "Hello World"
};

Console.WriteLine("Sending 'hello world' to queue");
var msg = client.SendMessageAsync(sendMessageRequest).Result;

Console.WriteLine("Sent with messageId:" + msg.MessageId);

Then, in the same function, I’ll read it back (here it would be a seperate process)

var latestMessages = client.ReceiveMessageAsync(new ReceiveMessageRequest(strSqsUrl)).Result;

onsole.WriteLine("Reading messages back from queue:");
foreach (var message in latestMessages.Messages)
{
 Console.WriteLine(message.Body);
}

Now, if you’re happy that everything has worked well, you can delete the message from the queue.

 Console.WriteLine("Deleting from queue");
client.DeleteMessageAsync(new DeleteMessageRequest(strSqsUrl, msg.MessageId));

And, that’s the basics of how to use SQS. .

You shouldn’t use SQS for long-term storage (>4 days), and you shouldn’t put large (>256K) messages in the queue. Although these defaults are configurable (max 14 days), But it works very well for what it’s designed for.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: