How Node really works · Chapter 34 of 34

Interview: build it

By the end of this chapter you can build, with nothing but Node, the small pieces every backend leans on: a promise wrapper, a job queue with retries, a rate limiter, a line splitter and a graceful shutdown. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The interview
  • 6 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

The last round. Dev pushes the snippets aside: "Now build me the small pieces every Node backend has. No packages." Tapri needs each of them: the kitchen printer takes two receipts at a time, the UPI service fails now and then, OTP requests get spammed, the printer feed arrives as raw bytes, and every deploy stops a server that may be halfway through a payment.

You built simpler versions already: myPromisify in chapter 26, a worker pool in 24, retries in 18, a line splitter in 14 and a shutdown in 27. Each round here adds the detail that separates a pass from a strong pass, and that detail is what breaks in production.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: promisify, write your own promisify

    Write promisify(fn) without importing util.promisify. fn takes an error-first callback, (err, value), as its last argument. promisify returns a function that returns a promise: it calls fn with all of its own arguments plus a callback, and the promise rejects with err when there is one, and otherwise resolves with value.

    this is passed through: stored on an object and called as a method, fn sees that object as this. When fn throws straight away instead of calling back, calling the returned function must not throw: the promise rejects with that error.

  2. Task 2 · interview question

    Interview: createJobQueue, run jobs with a concurrency limit

    Write createJobQueue({ concurrency, retries }), with concurrency 1 and retries 0 when they are left out (and when no object is passed at all). It returns { add }. add(job) takes a function that returns a promise or a plain value, and returns a promise of the job's result.

    At most concurrency jobs run at once. The others wait in the order they were added, and the next one starts as soon as any running job finishes, whether it succeeded or failed, without waiting for the rest. A job that fails, by rejecting or by throwing straight away, rejects only its own promise and never stops the queue.

  3. Task 3 · interview question

    Interview: createJobQueue, retry a failing job in its slot

    With retries set, a job that fails is tried again straight away, up to retries more times, so retries 2 means up to 3 tries in all. It keeps its slot the whole time: with concurrency 1, the next job only starts once this one has succeeded or given up.

    When the last try fails, add's promise rejects with the error of that last try, not the first. A job that succeeds on a later try resolves with that try's result. With retries 0, one failure is final.

  4. Task 4 · interview question

    Interview: createRateLimiter, a token bucket on an injected clock

    Write createRateLimiter({ capacity, refillPerSecond, now }), with now Date.now when it is left out (it returns milliseconds). It returns { allow }. The bucket starts full with capacity tokens and refills continuously at refillPerSecond tokens per second, counted from when the limiter was made, never above capacity.

    allow() takes one token and returns true, or returns false when less than one whole token is left. Fractions are kept: at 2 tokens per second, two waits of 250 ms make one whole token. Use no timers: work out the refill from the time that has passed. Every limiter has its own bucket.

  5. Task 5 · interview question

    Interview: createLineSplitter, a Transform from bytes to lines

    Write createLineSplitter(). It returns a Transform stream. Buffers go in, in chunks of any size; lines come out as strings, one line per chunk (readableObjectMode), without the '\n' and without a '\r' at the end.

    A line can be cut between chunks, and so can a character of several bytes, such as the Hindi letters of राहुल. An empty line between two newlines comes out as ''. A last line with no newline after it still comes out when the input ends, but a newline at the very end does not add an extra empty line.

  6. Task 6 · interview question

    Interview: shutdown, stop the server gracefully

    Write shutdown(server, { timeoutMs }), with timeoutMs 10000 when it is left out (and when no object is passed). It returns a promise that never rejects. It stops accepting new connections straight away, so a new request fails, lets requests already in progress finish and get their answers, closes idle keep-alive connections, and resolves with 'clean' once the server has closed.

    If the server has still not closed after timeoutMs (a request that never ends), it cuts every connection and resolves with 'forced', not before timeoutMs has passed.

What you will learn

  • Throws and promises: where a throw becomes a rejection, and where it escapes.
  • Free slots, not fixed batches: when the next job should start.
  • Time as an argument: a clock you pass in, so tests do not wait.
  • Bytes and closing time: StringDecoder, and what server.close waits for.

More in The interview

Build "Interview: build it" yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.