Final challenge: put the slow work in the background
After this: Work out response times, queue lengths, retries and repeats for a new service.
This challenge has five steps, and each step is scored on its own. You will work out how long a user waits with a slow job behind a queue, follow a burst tick by tick, time the retries of a job that never works, predict what an idempotent handler prints when every job comes twice, and choose how many workers a queue needs. The code is shown in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Answer now
Answer now
A user asks for a PDF of their year's orders. Saving the request takes 40 ms, putting a job on the queue takes 5 ms, and building the PDF takes 3,000 ms; the PDF is emailed when it is ready. With a queue, how long does the user wait for the page that says "Your report is on its way"?
The burst
Jobs arrive 30, 30, 0, 0 in four ticks. Two workers take 10 each per tick, 20 in all. New jobs join first, then the workers take theirs. How many jobs are waiting after each tick?
Retries
A different queue waits 2, 4 and then 8 ticks before retries, with at most 3 retries. A job with a broken address fails every time. Its first attempt is at tick 0. At which tick does it go to the dead-letter queue?
Twice each
Each of two jobs is delivered twice. What does this print?
done = {}
def handle(job_id, amount): if job_id in done: print("skip", job_id) return 0 print("charge", job_id, amount) done[job_id] = amount return amount
total = 0total += handle("a-1", 15)total += handle("a-2", 30)total += handle("a-1", 15)total += handle("a-2", 30)print("total", total)import java.util.HashMap;import java.util.Map;
public class Main { static Map<String, Integer> done = new HashMap<>();
static int handle(String jobId, int amount) { if (done.containsKey(jobId)) { System.out.println("skip " + jobId); return 0; } System.out.println("charge " + jobId + " " + amount); done.put(jobId, amount); return amount; }
public static void main(String[] args) { int total = 0; total += handle("a-1", 15); total += handle("a-2", 30); total += handle("a-1", 15); total += handle("a-2", 30); System.out.println("total " + total); }}How many workers
On a normal day, order jobs arrive at 25 a tick, all day long. Each worker handles 10 a tick. What is the smallest number of workers that keeps the queue from growing all day?
- Work out a queued response time, follow a burst tick by tick, time the retries of a job that never works, predict an idempotent handler, and choose how many workers a queue needs.