Laravel won't do the first bullet point there - it doesn't explicitly enable long polling by setting the WaitTimeSeconds when polling for new jobs. The ReceiveMessage call doesn’t set WaitTimeSeconds, but the queue attribute ReceiveMessageWaitTimeSeconds is set to 0.The ReceiveMessage call sets WaitTimeSeconds to 0.Short polling occurs when the WaitTimeSeconds parameter of a ReceiveMessage request is set to 0 in one of two ways: There's a little note in the bottom of the docs linked above: However, Laravel doesn't support long-polling out of the box. It turns out that with long polling, we're likely to get jobs more quickly as it polls all of the SQS servers that may contain our jobs! Amazon SQS sends an empty response only if the polling wait time expires. Amazon SQS sends a response after it collects at least one available message, up to the maximum number of messages specified in the request. With long polling, the ReceiveMessage request queries all of the servers for messages. Amazon SQS sends the response right away, even if the query found no messages. With short polling, the ReceiveMessage request queries only a subset of the servers (based on a weighted random distribution) to find messages that are available to include in the response. Here's the relevant thing to know, from the SQS docs: This is related to how SQS is scaled within AWS. If you use the SQS queue driver, you may see that some jobs take a while to get processed - as if the queue worker can't find a new job. Laravel doesn't do any long polling, but there is something important to know here. While the HTTP request is open, SQS may send a job to the queue worker at any time. Long polling allows you to keep an HTTP request open for a certain amount of time. This means that when our queue workers poll SQS for a new job, it's making an HTTP request to the SQS API.īy default, this does "short polling" - if no job is available when the HTTP request is made, SQS immediately returns an empty response. If you don't set a delay, the Visibility Timeout will be zero, which makes the job visible in the SQS queue immediately - likely not what you want! Long and Short Polling This will give your job more time to process before being made visible for another queue worker to pick up. If you have a specific job that likely needs more time than the default Visibility Timeout, you can call this method early in your job (with a delay!!) to increase the Visibility Timeout. There is a "hack" you can use to extend a job's Visibility Timeout, however! You can call the release() method, making sure to set a delay.įor the SQS queue driver, this sets the visibility timeout. Laravel won't set a Visibility Timeout for you, so it's best to set a default value that's higher than it takes to process your jobs. You can even extend a visibility timeout on a job that already exists. Visibility Timeout can be set to a default value for each SQS queue, or it can be set for each individual job. So we need to be careful that our Visibility Timeout is set HIGHER than it will take to get a job completed. This means another queue worker may pick that job up! If our queue worker takes more than 10 seconds to process the job, SQS will make the job visible again. Let's say our Visibility Timeout is set for 10 seconds. Most of us are probably used to not having to think about this - when we get a job in our Laravel queue worker, no other queue worker will pick up that job. One unique part of SQS queues is the Visibility Timeout. Here's the little details about SQS that you should know! Visibility Timeout However, there are a few key differences you need to know about it! Each of the following details have bitten me before. SQS is one of the few very useful services that is extremely affordable. Most managed services in AWS are rather expensive. In general, I'm a fan of any service I don't have to manage myself. It's super cheap, super reliable, and can scale higher than most of us will ever need. When it comes to queues, the AWS SQS service is a great option.
0 Comments
Leave a Reply. |