<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ari (Hamid) Zare</title>
    <description>The latest articles on DEV Community by Ari (Hamid) Zare (@hamidzr).</description>
    <link>https://dev.to/hamidzr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4239501%2F9cbed801-d470-47d0-849d-b0f34ad3c605.jpg</url>
      <title>DEV Community: Ari (Hamid) Zare</title>
      <link>https://dev.to/hamidzr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hamidzr"/>
    <language>en</language>
    <item>
      <title>AirPods on a Pixel: "connected", but no audio</title>
      <dc:creator>Ari (Hamid) Zare</dc:creator>
      <pubDate>Wed, 11 Nov 2026 20:59:58 +0000</pubDate>
      <link>https://dev.to/hamidzr/airpods-on-a-pixel-connected-but-no-audio-dpi</link>
      <guid>https://dev.to/hamidzr/airpods-on-a-pixel-connected-but-no-audio-dpi</guid>
      <description>&lt;p&gt;The setup is a Pixel phone, a MacBook, and a pair of AirPods Pro. The rule we wanted is simple: when the AirPods come out of the case, they should go back to whichever device used them last. When that was the phone, they should play on the phone.&lt;/p&gt;

&lt;p&gt;What actually happened: take the AirPods out, and the Pixel shows them as connected. Press play, and the music comes out of the phone speaker. About 30 seconds later the "connected" label disappears and the AirPods end up on the Mac.&lt;/p&gt;

&lt;p&gt;Tapping the AirPods in Quick Settings fixed it every time. That turned out to be the most useful clue.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is supposed to happen
&lt;/h2&gt;

&lt;p&gt;Pairing guides say that after the first pairing, AirPods reconnect to an Android phone on their own when you open the case nearby, like any other Bluetooth earbuds (&lt;a href="https://tech.yahoo.com/audio/article/how-to-pair-airpods-to-an-android-phone-150749366.html" rel="noopener noreferrer"&gt;Yahoo Tech&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;There is a condition. AirPods go back to the device they were last connected to (&lt;a href="https://dev.to/toshihue/connecting-airpods-pro-using-automator-53gk"&gt;DEV Community&lt;/a&gt;). Apple's automatic switching moves them between Apple devices signed in to the same Apple Account, and an Android phone is not part of it (&lt;a href="https://support.apple.com/en-us/104988" rel="noopener noreferrer"&gt;Apple Support&lt;/a&gt;). On the Mac, the "Connect to This Mac" setting controls whether the Mac takes the AirPods while you are using another device. Setting it to "When Last Connected to This Mac" turns that off, but as the name says, the Mac still reconnects when it was the last device.&lt;/p&gt;

&lt;p&gt;So when the Mac had them last, a manual connect on the phone is expected. Our failures happened with the phone as the last device, which is the case the guides say should just work. We are not the first to hit it. A Pixel 5 owner on Android Central described AirPods Pro that used to reconnect when the case opened and then stopped, while a manual connect still worked. Their AirPods were also paired with a laptop and an iPad. Re-pairing did not help, and the thread ends without a fix (&lt;a href="https://forums.androidcentral.com/threads/airpod-pros-not-automatically-reconnecting.1033093/" rel="noopener noreferrer"&gt;Android Central&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Watching both sides at once
&lt;/h2&gt;

&lt;p&gt;A Bluetooth problem between two hosts is hard to read from either host alone, so we built one watcher script that merged five streams onto a single timeline:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Phone &lt;code&gt;logcat&lt;/code&gt; over wireless adb, filtered to link state (&lt;code&gt;aclStateChangeCallback&lt;/code&gt;), profile state for A2DP (media) and HFP (calls), active device changes, A2DP stream start, HCI disconnect reasons, and profile connect requests.&lt;/li&gt;
&lt;li&gt;A 5-second poll of &lt;code&gt;dumpsys audio&lt;/code&gt; and &lt;code&gt;dumpsys bluetooth_manager&lt;/code&gt; on the phone: which output device the music stream uses, which device the player's audio track is bound to, and the active A2DP device. Printed only on change.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;log stream&lt;/code&gt; on the Mac for &lt;code&gt;bluetoothd&lt;/code&gt; and &lt;code&gt;audioaccessoryd&lt;/code&gt;: Bluetooth power, connections from the AirPods, in-ear changes, and automatic switching requests.&lt;/li&gt;
&lt;li&gt;A 4-second poll on the Mac: Bluetooth power, whether the AirPods are connected, and the default audio input and output.&lt;/li&gt;
&lt;li&gt;The Mac's built-in microphone, recorded with ffmpeg and reduced to a per-second loudness level. If music was coming out of the phone speaker next to the laptop, the mic heard it. This was ground truth that did not depend on any "connected" label.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The phone clock ran about one second behind the Mac, so phone log times were shifted before merging. Without that, events on the two hosts appeared in the wrong order.&lt;/p&gt;

&lt;p&gt;Two small lessons from building it. First, copy the exact log strings from a raw dump before writing filters; our guessed patterns matched nothing. Second, in zsh &lt;code&gt;log&lt;/code&gt; is a shell builtin, so the macOS tool has to be called as &lt;code&gt;/usr/bin/log&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it was not
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Audio routing on the phone.&lt;/strong&gt; When the phone set up the connection itself (a tap in Settings or Quick Settings), everything worked: A2DP came up with AAC, the AirPods became the active device, the player moved to them, and the Mac let go within a second.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Mac stealing the AirPods.&lt;/strong&gt; This was our first suspicion, and it was wrong. Every connection the Mac received was incoming, started by the AirPods, and it only happened after the phone link had already ended. During some failures the Mac's Bluetooth was turned off entirely.&lt;/p&gt;

&lt;h2&gt;
  
  
  The root cause: a link with nothing on it
&lt;/h2&gt;

&lt;p&gt;Here is a failing case-out, with the Pixel as the last device:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;14:47:51&lt;/td&gt;
&lt;td&gt;AirPods page the Pixel. A base Bluetooth link (ACL) comes up.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14:47:51&lt;/td&gt;
&lt;td&gt;The AirPods run a service discovery query over the link.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;Nobody opens A2DP or HFP. Settings shows "connected". Audio stays local.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14:48:24&lt;/td&gt;
&lt;td&gt;About 33 s later the AirPods close the link (HCI 0x13, remote terminated).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14:48:33&lt;/td&gt;
&lt;td&gt;The AirPods connect to the Mac, which does open the audio profiles.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A Bluetooth link and a Bluetooth audio connection are two separate layers. The ACL is the base radio link. Media (A2DP) and calls (HFP) are profiles that run on top of it. When the AirPods page a host, they create the base link and then wait for the host to open the profiles. Android does not open profiles on a link the remote side created. So the phone was telling the truth: it was connected, at the link layer, with zero audio profiles.&lt;/p&gt;

&lt;p&gt;The AirPods wait about half a minute, give up, and fall back to the Mac, which handles this case the way they expect.&lt;/p&gt;

&lt;p&gt;This adds something the guides leave out. "They reconnect to the last device" describes what the AirPods try. Whether audio follows also depends on what the host does with the link. Here the AirPods did reach out to the phone, and the phone still showed "connected" with nothing playing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a tap always works
&lt;/h2&gt;

&lt;p&gt;When you tap the device in Quick Settings or Settings, those apps call &lt;code&gt;BluetoothAdapter.connectAllEnabledProfiles&lt;/code&gt;. That is a system API gated by the &lt;code&gt;BLUETOOTH_PRIVILEGED&lt;/code&gt; permission. It tells the stack to open every enabled profile whatever state the link is in, and the AirPods accept a profile connect from a paired host every time.&lt;/p&gt;

&lt;p&gt;We also checked whether the per-device toggles mattered, in particular contacts access. They do not. The AirPods have media and calls allowed, and they do not offer contact or message access at all, so that toggle has nothing to act on.&lt;/p&gt;

&lt;p&gt;Ordinary apps cannot get &lt;code&gt;BLUETOOTH_PRIVILEGED&lt;/code&gt;. Tasker requests it, but it is a signature-level permission, so it is never granted. There is also no adb command that connects a single device's profiles, so a pure adb fix was not an option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the automation app did not help either
&lt;/h2&gt;

&lt;p&gt;The phone already had a Tasker profile that ran Tasker's Bluetooth Connection, Connect action on screen unlock. Two things were wrong with it.&lt;/p&gt;

&lt;p&gt;The trigger was wrong. The AirPods give up in about 30 seconds, and you rarely unlock inside that window.&lt;/p&gt;

&lt;p&gt;The action did nothing in the broken state. We moved the trigger to "the AirPods' link came up", which fired within about 100 ms, and still nothing reached the Bluetooth stack. Tasker's Connect works when there is no link at all. Run by hand with the AirPods idle, it opened A2DP within about four seconds, and the stack added HFP a few seconds later. With a base link already up, though, it does nothing and reports success.&lt;/p&gt;

&lt;p&gt;Its log line made this harder to see. Every run logged &lt;code&gt;BluetoothAdapter: A2DP connected&lt;/code&gt;, which reads like a device state. It is only Tasker's connection to the system's A2DP service. Our first conclusion was that Tasker's Connect never worked, and that was wrong too. A manual test with the AirPods idle corrected it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The workaround: drop the link, then connect
&lt;/h2&gt;

&lt;p&gt;If Connect works only when there is no link, the fix is to remove the link first. Turning Bluetooth off and on does that. Tasker cannot toggle Bluetooth itself, because apps that target Android 13 or newer are not allowed to. The Tasker Settings helper app targets an old SDK and still holds &lt;code&gt;BLUETOOTH_ADMIN&lt;/code&gt;, so Tasker's Bluetooth on/off action goes through it.&lt;/p&gt;

&lt;p&gt;The final profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Profile: Headphones Link Connect
  Trigger: Bluetooth connected to the AirPods' address, not in the car

Task:
  wait 3 s
  %a2dp = BluetoothAdapter.getDefaultAdapter().getProfileConnectionState(2)
  if %a2dp is 1 (connecting) or 2 (connected)
    stop, nothing to fix
  else
    Bluetooth off (through Tasker Settings)
    wait 2 s
    Bluetooth on
    wait 1 s
    Bluetooth Connection: Connect to the AirPods
    wait 20 s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;getProfileConnectionState(2)&lt;/code&gt; asks whether any A2DP device is connected. Tasker can call it through its Java Function action, which tells a real media connection apart from a bare link. The 20-second wait at the end also works as a guard: while the task is still running, Tasker ignores a second trigger, so the reconnect it causes cannot start a loop.&lt;/p&gt;

&lt;p&gt;The first unassisted run, Mac clock:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;Event&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;16:02:09&lt;/td&gt;
&lt;td&gt;AirPods page the phone. Link only, no audio.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16:02:09&lt;/td&gt;
&lt;td&gt;Tasker profile fires.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16:02:12&lt;/td&gt;
&lt;td&gt;Bluetooth off. The bare link drops.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16:02:15&lt;/td&gt;
&lt;td&gt;Bluetooth on.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16:02:17&lt;/td&gt;
&lt;td&gt;Tasker Connect.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16:02:18&lt;/td&gt;
&lt;td&gt;A2DP connected. Media plays in the AirPods.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Media audio arrived about 9 seconds after the broken link appeared, well inside the AirPods' 33-second window.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it does not fix
&lt;/h2&gt;

&lt;p&gt;The workaround needs a trigger, and sometimes there is none. On several case-outs the AirPods paged nothing at all, neither the phone nor the Mac. The phone gets no signal, so no phone-side rule can react.&lt;/p&gt;

&lt;p&gt;We tried to predict when this happens and got it wrong twice. First, a clean last disconnect looked like it mattered; the next sample disproved it. Then time in the case looked like the answer, until the AirPods paged nothing after less than a minute in the case. With the data we have, whether the AirPods try the Pixel after case-out looks random.&lt;/p&gt;

&lt;p&gt;The usual advice for AirPods that stop reconnecting is to update the firmware, re-pair, or reset them. Firmware updates only come through an iPhone, iPad, or Mac (&lt;a href="https://support.apple.com/en-us/106340" rel="noopener noreferrer"&gt;Apple Support&lt;/a&gt;), and ours already ran the latest version, 9A348. We re-paired the AirPods with the Pixel partway through the session, and both failure modes showed up before and after. A full reset is the one step we have not tried.&lt;/p&gt;

&lt;p&gt;For that case the options are a phone-side trigger such as screen unlock, which pulls the AirPods away from the Mac on every unlock, or an app that watches the case's Bluetooth LE advertisements and connects when the lid opens.&lt;/p&gt;

&lt;p&gt;Other trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The restart drops every Bluetooth device for about three seconds, including a watch or a car.&lt;/li&gt;
&lt;li&gt;In the one live run, calls (HFP) had not come up by the time the user also tapped the AirPods, so we do not yet know if calls join on their own.&lt;/li&gt;
&lt;li&gt;It is one unassisted success so far. A few days of normal use will show how often each failure mode happens.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Debugging lessons
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"Connected" is not one state.&lt;/strong&gt; Check the profile state, not the label or the link.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify a tool's log line before trusting it.&lt;/strong&gt; "A2DP connected" described Tasker's service binding, not the headphones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A Bluetooth restart is not a reconnect.&lt;/strong&gt; In one early test it looked like one, because the AirPods were in use and reconnected after the unexpected link loss. With idle AirPods it did nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A paused task blocks its trigger.&lt;/strong&gt; We stepped through the task in Tasker's editor and left it paused. When the real trigger fired, Tasker refused to start a second instance and showed a "not running" toast. Press stop after stepping through a task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-check your assumptions every round.&lt;/strong&gt; With the AirPods moving between two hosts, it is easy to lose track of which one was last. We got that wrong once and only caught it in the logs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use an independent signal.&lt;/strong&gt; The microphone did not care what any UI said. It just heard whether the phone speaker was playing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://latentbyte.com/blog/airpods-pixel-connected-no-audio" rel="noopener noreferrer"&gt;latentbyte.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>bluetooth</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Cutting Kutt redirect latency with Cloudflare Workers and KV</title>
      <dc:creator>Ari (Hamid) Zare</dc:creator>
      <pubDate>Wed, 11 Nov 2026 16:38:12 +0000</pubDate>
      <link>https://dev.to/hamidzr/cutting-kutt-redirect-latency-with-cloudflare-workers-and-kv-2pgf</link>
      <guid>https://dev.to/hamidzr/cutting-kutt-redirect-latency-with-cloudflare-workers-and-kv-2pgf</guid>
      <description>&lt;p&gt;We run Kutt as a self-hosted link shortener. The application and its PostgreSQL and Redis dependencies live on a VPS in Europe. That setup is simple and reliable, but every redirect normally travels to the VPS before Kutt can return the destination.&lt;/p&gt;

&lt;p&gt;From a test machine near Cloudflare's LAX edge, that distance was visible:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;First hop&lt;/th&gt;
&lt;th&gt;Cloudflare edge&lt;/th&gt;
&lt;th&gt;Direct VPS&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Average TTFB&lt;/td&gt;
&lt;td&gt;~48 ms&lt;/td&gt;
&lt;td&gt;~184 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average total&lt;/td&gt;
&lt;td&gt;~48 ms&lt;/td&gt;
&lt;td&gt;~184 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TLS handshake&lt;/td&gt;
&lt;td&gt;~22 ms&lt;/td&gt;
&lt;td&gt;~127 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connect&lt;/td&gt;
&lt;td&gt;~3 ms&lt;/td&gt;
&lt;td&gt;~58 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The edge-backed short link returned its redirect about 3.8 times faster in this test. Both links ultimately pointed to Google.&lt;/p&gt;

&lt;p&gt;Following the complete redirect chain produced a smaller difference: roughly 268 ms through the edge and 279 ms through the VPS. Most of that journey happened after our shortener, including Google's own redirect to &lt;code&gt;www.google.com&lt;/code&gt;. Optimizing the first hop cannot remove latency owned by the destination site.&lt;/p&gt;

&lt;p&gt;These numbers are one regional comparison, not a global benchmark. The useful result is narrower: moving the short-link decision close to the visitor removed most of the network and TLS cost between Los Angeles and our European origin.&lt;/p&gt;

&lt;p&gt;European visitors may see a smaller gain because the origin already sits in Europe. The edge still helps with TLS termination at a nearby PoP and removes a long cross-Atlantic round trip for users far from the VPS, but it will not beat geography everywhere by the same margin.&lt;/p&gt;

&lt;h3&gt;
  
  
  How we measured it
&lt;/h3&gt;

&lt;p&gt;Both hostnames pointed at the same slug (&lt;code&gt;/testgoogle&lt;/code&gt;) and returned &lt;code&gt;302&lt;/code&gt; to &lt;code&gt;https://google.com&lt;/code&gt;. We used &lt;code&gt;curl&lt;/code&gt; from a machine near Cloudflare's LAX edge, ten runs per hostname, first hop only (&lt;code&gt;--max-redirs 0&lt;/code&gt;). That isolates the shortener from the destination site's own redirects. End-to-end timing used &lt;code&gt;-L&lt;/code&gt; and included Google's hop to &lt;code&gt;www.google.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Hostnames below are illustrative placeholders for the measured routing roles.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Hostname&lt;/th&gt;
&lt;th&gt;Path&lt;/th&gt;
&lt;th&gt;Role in test&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;edge.example.com&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cloudflare Worker + KV&lt;/td&gt;
&lt;td&gt;Edge canary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;example.internal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Direct DNS to VPS&lt;/td&gt;
&lt;td&gt;Same stock Kutt origin, no Worker&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  KV warm vs cold
&lt;/h3&gt;

&lt;p&gt;The ~48 ms result assumes a KV hit. On a cache miss, the Worker performs one synchronous origin lookup before it can return the &lt;code&gt;302&lt;/code&gt;. That first visitor still waits on origin RTT. Repeat clicks read the destination from KV at the edge and get the fast path. When benchmarking, warm the slug first or report cold and warm separately.&lt;/p&gt;

&lt;h2&gt;
  
  
  The constraint: preserve Kutt analytics
&lt;/h2&gt;

&lt;p&gt;Caching a &lt;code&gt;302&lt;/code&gt; at the edge is easy. Preserving Kutt's visit analytics is the interesting part.&lt;/p&gt;

&lt;p&gt;A normal Kutt request does two jobs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Look up the slug and return its destination.&lt;/li&gt;
&lt;li&gt;Queue a visit containing the browser, operating system or device, country, referrer, and click increment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If Cloudflare returns a cached redirect without contacting Kutt, the user gets a fast response but Kutt never records the visit.&lt;/p&gt;

&lt;p&gt;We split those jobs in a Cloudflare Worker:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the destination from Cloudflare KV.&lt;/li&gt;
&lt;li&gt;Return the &lt;code&gt;302&lt;/code&gt; immediately.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;waitUntil()&lt;/code&gt; to send a background GET to the stock Kutt origin.&lt;/li&gt;
&lt;li&gt;Forward the visitor's &lt;code&gt;User-Agent&lt;/code&gt;, &lt;code&gt;Referer&lt;/code&gt;, IP headers, and Cloudflare country header so Kutt records the same fields it normally derives.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The analytics write is best effort. If the origin is unavailable, the visitor can still use a cached short link, but that click may be absent from Kutt. We accepted that trade-off in exchange for redirect availability and lower latency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filling KV without counting twice
&lt;/h2&gt;

&lt;p&gt;A cache miss needs one synchronous origin lookup to discover the destination. A second origin request records the actual visit. Sending both as an ordinary visitor would double-count the first click.&lt;/p&gt;

&lt;p&gt;Kutt already ignores known bots. The Worker uses a Googlebot user agent for the lookup request, which makes stock Kutt skip its visit. It then sends the real visitor headers in the background analytics request. This produces one recorded visit rather than two, without modifying Kutt.&lt;/p&gt;

&lt;p&gt;Only absolute HTTP or HTTPS destinations enter KV. Relative Kutt control redirects such as &lt;code&gt;/404&lt;/code&gt; and &lt;code&gt;/banned&lt;/code&gt;, password pages, and other non-redirect responses stay on the origin path.&lt;/p&gt;

&lt;h2&gt;
  
  
  What still hits the origin
&lt;/h2&gt;

&lt;p&gt;The Worker is not a full reverse proxy for every path. These requests pass through to stock Kutt unchanged:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI and administration routes&lt;/li&gt;
&lt;li&gt;API and static assets&lt;/li&gt;
&lt;li&gt;Password-protected links, which return HTML rather than a &lt;code&gt;Location&lt;/code&gt; header&lt;/li&gt;
&lt;li&gt;Link info pages (&lt;code&gt;slug+&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Any non-&lt;code&gt;GET&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only a single path segment matching Kutt's slug pattern on &lt;code&gt;GET&lt;/code&gt; is eligible for KV. That keeps the edge layer narrow: cache the redirect decision, proxy everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Domain-scoped links required origin routing
&lt;/h2&gt;

&lt;p&gt;Kutt scopes links by hostname. A slug stored for &lt;code&gt;edge.example.com&lt;/code&gt; does not resolve when the origin receives &lt;code&gt;Host: example.internal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Cloudflare's &lt;code&gt;resolveOverride&lt;/code&gt; gave us the required separation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The request URL and &lt;code&gt;Host&lt;/code&gt; remain &lt;code&gt;edge.example.com&lt;/code&gt; for Kutt's lookup.&lt;/li&gt;
&lt;li&gt;DNS resolution uses &lt;code&gt;example.internal&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;That illustrative origin hostname represents the same Kutt backend.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The public hostname uses a proxied DNS record and a Worker Route. We initially tried a Worker Custom Domain, but subrequests to the same public hostname recursed through the Worker. A Worker Route over the proxied origin record was the correct mechanism for this topology.&lt;/p&gt;

&lt;p&gt;We also found a Cloudflare SSL mode trap. The zone was set to Flexible, so Cloudflare contacted the origin over HTTP. Traefik redirected HTTP back to HTTPS, producing a self-redirect loop. Switching the zone to Full SSL allowed the Worker subrequest to reach the HTTPS origin correctly.&lt;/p&gt;

&lt;p&gt;One failed test had already cached that self-redirect. We deleted the poisoned KV entry and added a guard that refuses to cache a redirect whose destination is the original request URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Smart cache refresh without another origin request
&lt;/h2&gt;

&lt;p&gt;A fixed TTL creates a stale-link window after someone edits or deletes a link in Kutt. We use a 15-minute KV TTL as a final bound, but the background analytics request gives us a better refresh signal on every click.&lt;/p&gt;

&lt;p&gt;The Worker now reconciles KV from the analytics response:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A changed external destination updates KV for the next click.&lt;/li&gt;
&lt;li&gt;A deleted, banned, password-protected, or not-found response removes the KV entry.&lt;/li&gt;
&lt;li&gt;A network error or origin &lt;code&gt;5xx&lt;/code&gt; keeps the last known destination.&lt;/li&gt;
&lt;li&gt;A link with no traffic still expires through the normal 15-minute TTL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This adds no extra origin request. The current click can still use the previous destination because its response is already on the way, but the following click sees the refreshed value.&lt;/p&gt;

&lt;p&gt;Keeping the last known target on transient failures is deliberate. Deleting it during an outage would turn a temporary analytics problem into a redirect outage.&lt;/p&gt;

&lt;h2&gt;
  
  
  KV is not the CDN response cache
&lt;/h2&gt;

&lt;p&gt;During testing, one report interpreted &lt;code&gt;cf-cache-status: EXPIRED&lt;/code&gt; as evidence that edge caching was not working. That header describes Cloudflare's HTTP response cache, not Workers KV.&lt;/p&gt;

&lt;p&gt;Our Worker-generated &lt;code&gt;302&lt;/code&gt; responses do not use the CDN response cache and currently return no &lt;code&gt;cf-cache-status&lt;/code&gt; header. The target is stored in KV instead. We verified the KV entry directly and repeated requests returned the stored destination from the Worker.&lt;/p&gt;

&lt;p&gt;That distinction matters when measuring the system: &lt;code&gt;cf-cache-status&lt;/code&gt; is not a KV hit indicator. Worker observability or a direct KV inspection is needed to establish whether the slug mapping is present.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying the cutover
&lt;/h2&gt;

&lt;p&gt;Before moving more hostnames, we checked that edge redirects did not regress Kutt behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit count increases by &lt;strong&gt;one&lt;/strong&gt;, not two, on the first click after a KV miss&lt;/li&gt;
&lt;li&gt;Browser, OS, country, and referrer in the dashboard match the client, not the Worker&lt;/li&gt;
&lt;li&gt;Country reflects the visitor, not a Cloudflare PoP default&lt;/li&gt;
&lt;li&gt;Password links still render Kutt's form; &lt;code&gt;slug+&lt;/code&gt; still shows link info&lt;/li&gt;
&lt;li&gt;Deleted or retargeted links stop working at the edge within the KV TTL, or sooner on the next click that reconciles KV&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These checks matter because the whole design depends on stock Kutt remaining unmodified. The Worker adapts to Kutt's existing bot filtering and visit queue, not the other way around.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;The canary hostname now follows this path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;visitor
  -&amp;gt; nearest Cloudflare edge
  -&amp;gt; Worker reads destination from KV
  -&amp;gt; immediate 302
  -&amp;gt; background Kutt visit and cache reconciliation
  -&amp;gt; PostgreSQL analytics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our LAX-to-Europe comparison, first-hop redirect latency fell from about 184 ms to 48 ms on warm KV hits. The existing Kutt deployment remained stock, its dashboard retained visit details, and edits now repair the edge mapping lazily on the next click.&lt;/p&gt;

&lt;p&gt;The approach does not make destination sites faster. Best-effort analytics can lose visits when the background ping fails. A link with no traffic after an edit can stay stale until the 15-minute KV TTL expires. During origin outages we keep serving the last known destination rather than failing open to a broken redirect. Those trade-offs are deliberate: the user-visible short link should stay fast and available, with Kutt remaining the source of truth for links and analytics.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on the &lt;a href="https://latentbyte.com/blog/kutt-cloudflare-edge-redirects" rel="noopener noreferrer"&gt;Latent Byte blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>architecture</category>
      <category>cloudflare</category>
    </item>
  </channel>
</rss>
