Daily Tech Briefing
AI 科技速览

每天 5 分钟内学习 AI。获取最新的人工智能新闻,理解其重要性,并学习如何将其应用于您的工作。

AI 快讯
Dev.to AI · 2026/8/4 16:47:15

Reducing Neon Compute Costs by Tuning the TV Dashboard Refresh Interval

AI 中文解读
核心亮点:通过把电视仪表盘的自动刷新时间从30秒延长到15分钟,作者成功省下大笔数据库计算费用,还解决了服务休眠失败的问题,改动只是一个小小的数值。 通俗解读:很多网站后台会定时去数据库“问”有没有新数据,以前作者设置的频率是每30秒就问一次,哪怕页面没什么变化,服务器也得一直待命,就像家里空调24小时开着一样耗电。后来他发现后台数据其实几分钟才变一次,干脆把“问”的间隔调成了5分钟,甚至15分钟。数据库不用一直工作,就能在没人看的时候彻底“睡觉”,醒来再干活,电费自然就降下来了。 实际影响:这件事对普通人来说可能感觉不到,但对创业公司或独立开发者来说很重要。云数据库按秒计费,刷新太勤意味着白花冤枉钱;刷新太慢又怕数据不准。作者这次调整既保住了数据更新速度,又没影响用户体验,还让系统更稳定。这也提醒我们,很多时候省钱不需要复杂优化,调低“没必要的勤奋”就能立竿见影。
<h1> Reducing Neon Compute Costs by Tuning the TV Dashboard Refresh Interval </h1> <p><strong>TL;DR:</strong><br><br> I increased the TV dashboard refresh interval from 30 s to 5 min, then to 15 min, saving Neon compute credits and preventing scale‑to‑zero issues. The change is a single constant in <code>src/features/tv/TVDashboard.tsx</code>, but the impact on cost and reliability is huge.</p> <h2> The Problem </h2> <p>The TV dashboard (<code>/tv</code>) was auto‑refreshing every 30 seconds. Neon, our serverless Postgres provider, charges per second of compute usage. With a 30 s poll, the database stayed hot for most of the day, consuming compute credits and causing the pool to stay awake. This prevented Neon from scaling to zero, which would otherwise save us money when the site was idle. Additionally, the data in the dashboard rarely changes more frequently than a few minutes, so the frequent polling was unnecessary.</p> <p>The symptoms were simple:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight console"><code><span class="gp">$</span><span class="w"> </span>npm run dev <span class="c">... </span><span class="go">⚠️ Neon compute headroom: 0.3s remaining </span></code></pre> </div> <p>And the cost dashboard in Vercel spiked during traffic peaks.</p> <h2> What I Tried First </h2> <p>Initially, I thought a quick fix would be to add a debounce or throttle to the polling logic in the dashboard component. I wrapped the <code>useEffect</code> that triggers <code>fetchDashboardData</code> with a <code>setTimeout</code> of 30 s:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="nf">useEffect</span><span class="p">(()</span> <span class="o">=></span> <span class="p">{</span> <span class="kd">const</span> <span class="nx">id</span> <span class="o">=</span> <span class="nf">setTimeout</span><span class="p">(</span><span class="nx">fetchDashboardData</span><span class="p">,</span> <span class="mi">30</span><span class="nx">_000</span><span class="p">);</span> <span class="k">return </span><span class="p">()</span> <span class="o">=></span> <span class="nf">clearTimeout</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span> <span class="p">},</span> <span class="p">[]);</span> </code></pre> </div> <p>However, this still caused a database hit every 30 s, so the compute usage didn’t change. I also considered moving the polling to the server side via Vercel Edge Functions, but that would add complexity and latency. The simplest path was to adjust the polling interval directly.</p> <h2> The Implementation </h2> <h3> 1. Identify the Refresh Constant </h3> <p>In <code>src/features/tv/TVDashboard.tsx</code>, the refresh interval is defined as a constant:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="c1">// src/features/tv/TVDashboard.tsx</span> <span class="kd">const</span> <span class="nx">REFRESH_MS</span> <span class="o">=</span> <span class="mi">30</span><span class="nx">_000</span><span class="p">;</span> <span class="c1">// 30 s — data only changes on manual sync, no need to poll faster</span> </code></pre> </div> <h3> 2. First Change – 5 min Refresh </h3> <p>I bumped this to 5 minutes to give Neon a chance to scale to zero during idle periods:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight tsx"><code><span class="c1">// src/features/tv/TVDashboard.tsx</span> <span class="kd">const</span> <span class="nx">REFRESH_MS</span> <span class="o">=</span> <span class="mi">300</span><span class="nx">_000</span><span class="p">;</span> <span class="c1">// 5 min — data only changes on manual sync, no need to poll faster</span> </code></pre> </div> <p><strong>Why 5 min?</strong> </p> <ul> <li>The TV data changes only when a new broadcast is scheduled or a user manually syncs.</li> <li>5 min
分享
阅读原文