Click Analytics for a URL Shortener Without Slowing Down Redirects
Design click analytics for a URL shortener: async event pipelines with Kafka, aggregation with stream processing, counters, unique visitors and storage.
Published November 27, 2026
Analytics turns a URL shortener from a utility into a product: marketers want clicks over time, countries, devices and referrers. The challenge is that every redirect becomes a write, multiplying load on the busiest path in the system. The rule is simple: never make the user wait for analytics. This article shows how to collect, process and serve click data asynchronously.
The concept
The redirect service should do as little as possible: respond with the redirect, then emit a small event to a buffer. That buffer is typically a log-based message broker such as Kafka, Kinesis or Pulsar, which can absorb huge write rates and replay data if downstream consumers fail. Local in-process batching before publishing reduces network overhead further.
Downstream, a stream processor (Flink, Kafka Streams, Spark, or a simple consumer group) enriches events (IP to country, user agent to device) and aggregates them into per-link, per-minute or per-hour counters stored in a database suited to time series or analytics, such as ClickHouse, Druid, BigQuery or even PostgreSQL with rollup tables. Raw events go to cheap object storage for reprocessing.
The analytics pipeline
A click event
| Field | Example | Notes |
|---|---|---|
| code | aZ3kP9q | Partition key so one link's events stay ordered |
| ts | 2026-11-27T10:15:03.120Z | Event time, not processing time |
| ip_hash | sha256(ip + daily salt) | Avoid storing raw IPs where possible |
| country | IN | Derived from IP via GeoIP at enrichment time |
| ua_family / device | Chrome / mobile | Parsed user agent |
| referrer_host | t.co | Host only, strip paths and query |
| is_bot | false | Filter link-preview crawlers |
Counting unique visitors cheaply
Exact unique counts per link per day require storing every visitor ID. HyperLogLog estimates cardinality with about 1% error using roughly 12 KB per counter, which is why Redis, ClickHouse and BigQuery all offer HLL functions.
# Redis: one HLL per link per day
PFADD uv:aZ3kP9q:2026-11-27 <ip_hash>
PFCOUNT uv:aZ3kP9q:2026-11-27Latency added to the redirect
Practical examples
Fire-and-forget event emission (Node.js + KafkaJS)
Batch in memory and flush every 100 ms or 500 events.
const buffer = []
function recordClick(evt) {
buffer.push({ key: evt.code, value: JSON.stringify(evt) })
if (buffer.length >= 500) flush()
}
async function flush() {
if (!buffer.length) return
const messages = buffer.splice(0)
await producer.send({ topic: 'clicks', messages }).catch(err => log.warn('click batch dropped', err))
}
setInterval(flush, 100)Per-minute rollup table (ClickHouse)
SummingMergeTree adds up counters as parts merge.
CREATE TABLE clicks_per_minute (
code String,
minute DateTime,
country LowCardinality(String),
clicks UInt64
) ENGINE = SummingMergeTree
ORDER BY (code, minute, country);
SELECT toStartOfHour(minute) AS hour, sum(clicks)
FROM clicks_per_minute WHERE code = 'aZ3kP9q' AND minute >= now() - INTERVAL 7 DAY
GROUP BY hour ORDER BY hour;Simple fallback: Redis counters
For small deployments, increment counters and flush to SQL periodically.
HINCRBY clicks:2026-11-27 aZ3kP9q 1
# cron every minute: HGETALL + upsert into daily_clicks, then DELCommon mistakes
- Updating a click counter column in the links table on every redirect, creating hot-row contention.
- Writing analytics synchronously and making redirects depend on analytics storage being up.
- Counting bots and link-preview fetches as human clicks.
- Storing raw IP addresses indefinitely without a privacy reason (GDPR and similar laws apply).
- Partitioning events randomly, which breaks per-link ordering and makes deduplication harder.
Key takeaways
- Respond with the redirect first; emit analytics events asynchronously.
- A log-based broker absorbs spikes and lets consumers replay.
- Aggregate into time-bucketed rollups for fast dashboards.
- Use HyperLogLog for unique visitors and filter bots.
FAQ
What if Kafka is down?
Buffer locally for a short time and then drop events with a metric. Losing some analytics is better than failing redirects.
Is analytics eventually consistent?
Yes. Dashboards typically lag by seconds to minutes, which users accept for click stats.
Can I use a managed product instead?
Yes. Many teams send events to a product analytics or data warehouse service. The key design rule, asynchronous emission, stays the same.