and resizing ◦ We need to upload image ◦ We need to resize it to several different dimensions • Should we handle it in the http handler? ◦ Increases response time ◦ We should focus on more important tasks (ie. authentication) ◦ Let’s defer image resizing to background
Store the image data in database / data store ◦ Run scheduled cron jobs ◦ Scaling could be hard between multiple servers ◦ Overlapping cron jobs could lead to race conditions • Message Queues and Workers ◦ A queue stores the image data ◦ Multiple workers (across different servers) can consume data one by one ◦ Uses pub-sub pattern
Sends a message to a defined queue with file location and other parameters • Message Broker ◦ Routes the message to the consumer aka background worker • Background worker ◦ Receives the message ◦ Takes the image file, resizes according to parameters ◦ Puts it back
• Give celery a message broker (say: redis) • Have celery workers always running • Write the tasks as Python functions / classes • Wrap the function in a decorator • In your web end point, call the function (slightly differently) Celery does what needs to be done behind the scene