TweakCode
Building Blocks lessons

Queues

Backpressure is the depth

Before you start

  • Queues

    Work moves off the request path into a queue.

    Open lesson

Why it matters

A normal author can have hundreds of followers, and each home timeline needs the new post within a few seconds. If the post request waits for every timeline update, its latency grows with follower count and bursts make authors wait for overloaded workers. The post API must return once the post is durable, regardless of follower count, while timeline lag stays within the freshness promise.

Example

Fan out a post

A normal user posts text. Followers’ home timelines should include it within a few seconds. The post API should return as soon as the post is durable.

Input
arrivals = [800, 1200, 1500, 900, 600, 400], capacity = 1000
Output
[0, 200, 700, 600, 200, 0]

Mental model

The idea

Keep one example in view

Fan out a post

arrivals = [800, 1200, 1500, 900, 600, 400], capacity = 1000→[0, 200, 700, 600, 200, 0]

Keep this exact scenario and its constraint in view while you build the model.

For a normal post, commit once and enqueue its post and author ids, then let workers write the followers’ timelines; measure the 800, 1,200, and 1,500-message burst against 1,000/s capacity so post latency never follows fanout depth.

After the post row commits, publish its id and author id. A worker expands that message into one timeline write per follower. A follower who opens home before the worker reaches them sees the previous page; the new post appears after the agreed few-second gap.

Track queue depth each second as prior depth plus arrivals minus the 1,000-message worker capacity. In the example, bursts raise depth to 700 and spare capacity drains it to zero. Rising lag triggers more workers or an alert; sustained overload needs admission control because a queue cannot erase excess work.

Visual anchor

Fan out a post

queue depth at the end of each second

0
200
700
0
0
0
012345

fanout messages arriving

012345
800
1200
1500
900
600
400
1,500 messages arrive and workers finish 1,000. The extra 500 wait in the queue: depth 700.

Pro

The rest of this is on Pro

The idea above is free. The walkthrough, the cost, and the practice are on Pro.