link::record_click directly, so the database write runs on
the redirect’s hot path and a slow write slows the redirect. In this chapter you move that work onto
a queue so redirects return immediately, then use pub/sub to broadcast link events to
independent subscribers: a Python analytics worker and a cache refresher, both decoupled from the
link worker.
Add the workers
This chapter usesqueue, pubsub, and a new Python analytics worker. Uncomment the Ch. 4 block
in worker-compose.yaml:
worker-compose.yaml
analytics database under the Ch. 3 block’s config_override:
worker-compose.yaml
Make redirects fast with a queue
A queue holds work that is accepted now and run later. Theclicks queue you uncommented above is
the one link::record_click uses.
Queue names are references to the queue and do not place any restrictions on what can be put into
a given queue.
link::record_click in Chapter 3, where http::redirect triggers it directly.
Nothing about that function needs to change to make it queuable. You only change how it’s triggered
with a TriggerAction.
First import TriggerAction into link/src/index.ts:
src/index.ts
action to the existing link::record_click call in http::redirect so the queue
worker enqueues it instead of running it inline:
src/index.ts
link::record_click
drains the queue in the background, with retries and a dead-letter queue if a write keeps failing.
Broadcast events with pub/sub
A queue delivers each message to one consumer. When several unrelated parts of the system need to react to the same event, use a publish subscribe design instead.We ship both a
queue and pubsub worker. While queue provides standard queueing
it also provides its own durable publish and subscribe.When you need a publish and subscribe flow to be guaranteed to succeed (or fail to a DLQ) then use
queues iii::durable::publish and durable:subscriber.When you don’t need a publish and subscribe flow to be guaranteed then use pubsubs publish and
subscribe.analytics worker that collects data from these events. We don’t have link updating
functionality yet, so we’ll add that and an HTTP endpoint for it too.
Publish on link.created
Publish an event whenever a link is created or its target changes. Insidelink::create, after the
database write and state::set, trigger the publish function:
src/index.ts
Publish on link.updated
Now add an update path to thelink worker so a link’s target can change, and announce it.
First, the domain function: it updates the database row and publishes a link.updated event through
durable pub/sub (iii::durable::publish, served by queue). Place it at the end of
link/src/index.ts:
src/index.ts
Expose link updating via HTTP
Then connectlink::update with an HTTP handler that validates input and calls the domain function:
src/index.ts
http::update to PUT /links/:code:
src/index.ts
Add reactive state: Keep the cache correct without coupling
link::update changes the database but not the state cache, so a query could serve stale data.
Rather than handle refreshing the state cache inside link::update, subscribe to the link.updated
event with a durable subscriber:
Durable vs. regular pub/sub.
link.updated uses durable pub/sub: iii::durable::publish with
a durable:subscriber trigger, both served by the queue worker. Consumers like this cache
refresher must receive every update. A dropped event would leave the cache pointing at a stale
URL. link.created stays on regular pub/sub (pubsub). Its only consumer is a best-effort daily
counter, so an occasional miss is harmless. Use durable pub/sub when a missed event would corrupt
state, and regular pub/sub for fire-and-forget fan-out.src/index.ts
Create an analytics worker in Python
Queues and events are useful within a single worker but also between workers. Thus far we’ve been writing all of our code in TypeScript. However workers are not restricted to specific languages or runtimes. So this time we’ll create an analytics worker in Python to count links.Adding a second worker
We already referenced the analytics worker inworker-compose.yaml, now let’s give it some code.
The analytics directory holds a Python worker, with an analytics/src/main.py entrypoint and an
iii.worker.yaml manifest of its own:
analytics/iii.worker.yaml
Subscribe to link.created events
Createanalytics/src/main.py with this code, which subscribes to link.created events and keeps
count of every time that a new short link is created:
analytics/src/main.py
analytics/src/main.py
analytics/src/main.py
link worker never has to know it exists.
That is the analytics entry you uncommented at the start of the chapter, alongside the primary
one from Chapter 3.
Restart Compose so it picks up the change:
See it work
Create five links, follow one a few times, and change its target:PUT /links/:code publishes link.updated durably and the subscriber refreshes
the cache, so link::resolve returns the new URL right away:
Conclusion
Redirects no longer wait on a database write: click rows ride a queue, drained in the background. Link events fan out over pub/sub to a Python analytics counter and a cache refresher, and thelink
worker does not know either of them exists. Next, in
Ch. 5: Stream live clicks, you broadcast every click in real time
from a dedicated click-streamer worker.