Skip to content
Luca Ubiali Web Developer

Understanding Laravel's Context Capabilities - Using Context in Jobs

September 9th, 2024
Continuous Learning

Episode - https://laracasts.com/series/understanding-laravels-context-capabilities/episodes/3


When dispatching a job, the Context is included within the serialized version of the job class that’s sent to the queue.

As a result whatever context data that was available in controllers handling HTTP requests, is also available when running the handle method of the job and for all the code executed from there.

If the suggestion before was not to go wild with Context, that’s even more true now. Especially when using SQS as queue driver. SQS enforces a limit of 256kb for each message sent to the queue. It happened often to me to go over that limit even without Context. In these cases there are a few strategies like storing in cache or on S3 the full data needed by the job and pass a reference to it via parameter. This way the job can retrieve the data when it runs without bloating the message on the queue.

I was expecting to ear about this in the lesson or even see it in the Laravel documentation, but there’s none. It sort of makes sense as it’s a very specific SQS limitation, not a Laravel one. But using Context without a bit of knowledge on how it works internally, it’s very easy to get bitten in such scenarios.