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 uses two workers.iii-queue is already in your project from iii project init. Add
iii-pubsub now; you publish your first event to it later in the chapter:
Make redirects fast with a queue
A queue holds work that is accepted now and run later: each message in theclicks queue is one
“insert a row recording that someone followed code X at time T.” Define the queue on the iii-queue
worker (already in your project from iii project init), but requiring a few updates to
queue_configs:
config.yaml
link::record_click in Chapter 3, where http::redirect triggers it directly.
Nothing about the function changes. You only change how it’s invoked. First import TriggerAction:
src/index.ts
action to the existing link::record_click call in http::redirect so the
iii-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
iii-queue and iii-pubsub worker. While iii-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
iii-queues iii::durable::publish and durable:subscriber.When you don’t need a publish and subscribe flow to be guaranteed then use iii-pubsubs publish
and subscribe.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 built-in publish function:
src/index.ts
Publish on link.updated
Add an update path so a link’s target can change, and announce it. First, the domain function: it updates the database row and publishes alink.updated event through durable pub/sub
(iii::durable::publish, served by iii-queue):
src/index.ts
Expose link updating via HTTP
Then the HTTP handler that validates input and calls the domain function:src/index.ts
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 iii-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 (iii-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.Create a new worker
Scaffold a Python worker the same way you scaffolded thelink worker in Chapter 1. That generates
an analytics/ worker with a main.py example, and a iii.worker.yaml manifest.
Subscribe to link.created events
Replace the examplemain.py with this one that subscribes to link.created events and keeps count
of every time that a new short link is created:
analytics/main.py
analytics/main.py
analytics/main.py
Configure the worker’s manifest
The generated manifest has no run scripts yet, so give it an install and start script:iii.worker.yaml
link worker never has to know it exists.
Add an analytics database to the database worker, alongside the primary one from Chapter 3:
config.yaml
config.yaml:
See it work
Create five links, follow one a few times, and change its target: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.