Use getQueueUrl API to generate sqsUrl using sqsName and awsAccountId or simply hardcode the sqsUrl string. What is the recommendation?

0

There are two ways to get the sqsUrl:

A. We can simply hardcode the static url string in the code.

Pros:

a. No API call, hence less latency.

b. Simple to write and less line of code. Will have to define just the sqsUrl for each sqs in Configuration.

c. Many services in Amazon use this only, probably to save latency.

Cons:

a. If AWS changes the url structure in future, then the code will break. And we have to go change the url in code. But slim chance of happening this. They will make sure of compatibility.

b. Kind of hardcoding, not a good coding practice. (My opinion)

B. Other way is to programatically create the sqsUrl using getQueueUrl api, which accepts sqsName and awsAccountId (optional, only required if sqs’s account is different than caller’s account.)

Pros:

a. If we want to send the message to some random team’s sqs, but they didn’t provide the sqsUrl. They just provided the name of sqs and account id. In that case, generating url programatically can help.

b. If AWS changes the url structure in future, the code will require no changes, and things will work as it is.

c.There must be some reason why SQS provided getQueueUrl api, to avoid directly using url/arn. (Yet to find any documentation where AWS recommends using this. )

Cons:

a. Extra dependency on an api getQueueUrl, extra latency of ~200 ms. But should not be a concern when this much latency doesn’t matter. For ex: Offline systems, Postprocessing scenario, etc.

b. Less lines of codes.

1 Answer
0

If you expect the SQS URL to remain constant and do not anticipate needing to frequently change the queue or account, the simpler approach of hardcoding the URL may be suitable. This approach indeed reduces latency and the amount of code you need to maintain. The risk, as you pointed out, is that if Amazon were to change the URL structure in the future, your code might break. However, it's worth noting that such changes are typically rare and are usually accompanied by lengthy deprecation periods, during which both the old and new formats would continue to work. On the other hand, using the getQueueUrl API to dynamically generate the SQS URL provides more flexibility. This method would be beneficial if you foresee needing to interact with multiple queues or accounts, or if the queue name might change. Furthermore, this approach is more robust to potential future changes in AWS's URL structure.

profile picture
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions