Files
nexus/sreweekly/articles/66/10-slow-down-your-internet-with-tc.html
2026-09-12 17:23:01 +08:00

268 lines
14 KiB
HTML

<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<title>Slow down your internet with tc</title>
<meta name="author" content="Julia Evans">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="description" content="Slow down your internet with tc">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content='Slow down your internet with tc'>
<meta property="og:type" content="website" />
<meta property="og:url" content="https://jvns.ca/blog/2017/04/01/slow-down-your-internet-with-tc/" />
<meta property="og:site_name" content="Julia Evans" />
<link rel="canonical" href="https://jvns.ca/blog/2017/04/01/slow-down-your-internet-with-tc/">
<link href="/favicon.ico" rel="icon">
<link href="/stylesheets/screen.css" rel="preload" type="text/css" as="style">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/stylesheets/print.css" media="print" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="Julia Evans" type="application/atom+xml">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.css" integrity="sha384-vKruj+a13U8yHIkAyGgK1J3ArTLzrFGBbBc0tDp4ad/EyewESeXE/Iv67Aj8gKZ0" crossorigin="anonymous">
<script defer data-domain="jvns.ca" src="https://plausible.io/js/script.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.js" integrity="sha384-PwRUT/YqbnEjkZO0zZxNqcxACrXe+j766U2amXcgMg5457rve2Y7I6ZJSm2A0mS4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous" onload="renderMathInElement(document.body);"></script>
<script defer type="text/javascript">
window.heap=window.heap||[],heap.load=function(e,t){window.heap.appid=e,window.heap.config=t=t||{};var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.heapanalytics.com/js/heap-"+e+".js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a);for(var n=function(e){return function(){heap.push([e].concat(Array.prototype.slice.call(arguments,0)))}},p=["addEventProperties","addUserProperties","clearEventProperties","identify","resetIdentity","removeEventProperty","setEventProperties","track","unsetEventProperty"],o=0;o<p.length;o++)heap[p[o]]=n(p[o])};
heap.load("2242143965");
</script>
</head>
<body>
<div id="skiptocontent">
<a href="#main">Skip to main content</a>
</div>
<div id="wrap">
<header role="banner">
<hgroup>
<h1><a href="/">Julia Evans</a></h1>
</hgroup>
<ul class="header-links">
<li><a href="/about">About</a></li>
<li><a href="/talks">Talks</a></li>
<li><a href="/projects/">Projects</a></li>
<li><a rel="me" href="https://social.jvns.ca/@b0rk">Mastodon</a></li>
<li><a href="https://bsky.app/profile/b0rk.jvns.ca">Bluesky</a></li>
<li><a href="https://github.com/jvns">Github</a></li>
</ul>
</header>
<nav role="navigation" class="header-nav"><ul class="main-navigation">
<li><a href="/categories/favorite/">Favorites</a></li>
<li><a href="/til/">TIL</a></li>
<li><a href="https://wizardzines.com">Zines</a></li>
<li class="subscription" data-subscription="rss"><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div>
<article class="hentry" role="article">
<header>
<h1 class="entry-title">Slow down your internet with tc</h1>
<div class="post-tags">
</div>
<p class="meta sans">
<time class="date" datetime="2017-04-01T21:05:50" pubdate data-updated="true">
April 1, 2017
</time>
</p>
</header>
<main>
<p>Hello! This week I learned how to make my internet slower on purpose! My
awesome coworker Doug told me about tc, which is a tool for Linux that
stands for &ldquo;traffic control&rdquo;.</p>
<p>I&rsquo;d actually heard about this tool a while ago when I interviewed
someone awesome and she was telling me about her experience using tc in
her last job. But I hadn&rsquo;t thought about it since then, and I definitely
hadn&rsquo;t used it!</p>
<p>He pointed me to this blog post <a href="http://bencane.com/2012/07/16/tc-adding-simulated-network-latency-to-your-linux-server/">Adding simulated network latency to your Linux server</a>
which explains it really well.</p>
<h3 id="use-tc-to-make-your-personal-internet-slow" class="post-heading">
<a href="#use-tc-to-make-your-personal-internet-slow">
use tc to make your personal internet slow
</a>
</h3>
<p>I tried <code>tc</code> out on my laptop! The incantation in that blog post told
me how to add extra latency to every packet I send. So when any program
on my computer sends a network packet, it&rsquo;ll just get sent&hellip; a bit
late. Here&rsquo;s what that looks like:</p>
<pre><code>sudo tc qdisc add dev wlp3s0 root netem delay 500ms
# and turn it off with
sudo tc qdisc del dev wlp3s0 root netem
</code></pre>
<p><code>wlp3s0</code> is the network interface for my wireless card, and this command
adds a 500ms delay to every packet. Totally unsurprisingly, browsing the
internet got a lot slower. There was something surprising, though!</p>
<p>Last week I learned about this site that compares the speed of HTTP and
HTTPS for loading a whole bunch of small images:
<a href="http://www.httpvshttps.com/">http://www.httpvshttps.com/</a>.</p>
<p>Here&rsquo;s a table with how long it took with my normal internet and with
artifically-slowed down internet.</p>
<pre><code> http | https
normal internet | 3.5s | 0.7s
500ms delay | 33s | 1.1s
(with TLS connection already open)
500ms delay | 33s | 5s
(with new TLS connection)
</code></pre>
<p>That&rsquo;s weird, right? The HTTP version got 10x slower, but the https
version didn&rsquo;t slow down as much. To understand why, you have to
read this article <a href="https://www.troyhunt.com/i-wanna-go-fast-https-massive-speed-advantage/">I wanna go fast: HTTPS&rsquo; massive speed advantage </a>.</p>
<p>Basically HTTP/2 (the next version of HTTP) only
works when you&rsquo;re using HTTPS, and HTTP/2 lets you do a lot more
requests in parallel, so you&rsquo;re not slowed down as much.</p>
<p>There&rsquo;s something else weird in this table though. It says that with my
500ms delay, the HTTPS version of the page loaded in 1.1s. But to do HTTPS you have to do a
TLS handshake, and if your latency is 500ms you can definitely not
open the connection and send/receive data in 1.1s. When the HTTPS version was taking 1.1s it
was reusing a TLS connection that was already open. Still, when I
restarted my browser and opened the website from scratch it only took 5
seconds. That&rsquo;s really a lot faster than 33s! I might conceivably wait 5
seconds for a page to load but I feel like I&rsquo;d get too bored in 30 seconds.</p>
<p>So it&rsquo;s really neat to see that HTTPS and HTTP/2 are really possibly
making things faster for people with high latency!</p>
<h3 id="use-tc-to-make-your-server-internet-slow" class="post-heading">
<a href="#use-tc-to-make-your-server-internet-slow">
use tc to make your server internet slow
</a>
</h3>
<p>You can also use tc to make your server internet slow. I used tc to make
a DNS server in a test environment slow to see how a DNS client I was
interested in would behave, exactly.</p>
<p>It&rsquo;s important to note that &ndash; you probably don&rsquo;t want to add a
delay to every packet on a machine in production, you might have a bad
day. Unless your goal is really to break things (&ldquo;surprise, network
latency everyone! let&rsquo;s find out how your code deals with timeouts!&rdquo;) in
which case maybe it&rsquo;s a good idea.</p>
<p>One thing I learned by doing this &ndash; if you add a delay to every
packet on a machine you&rsquo;re SSHed into, your SSH connection is also
sending packets, so your SSH connection also gets slow. This was okay
though, it was still usable enough.</p>
<h3 id="that-s-all" class="post-heading">
<a href="#that-s-all">
that&rsquo;s all
</a>
</h3>
<p>I think there are more things you can do with tc but I don&rsquo;t know what
they are yet! I think it&rsquo;s cool that you can pretend to be on a machine
that is Really Far Away by just running one command on your laptop!</p>
</main>
<footer>
<style type="text/css">
#mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; display: inline;}
#mc_embed_signup {
display: inline;
}
#mc_embed_signup input.button {
background: #ff5e00;
display: inline;
color: white;
padding: 6px 12px;
}
</style>
<div class="sharing">
<style>
.form-inline {
display:flex; flex-flow: row wrap; justify-content: center;
}
.form-inline input, .form-inline span {
padding: 10px;
}
.form-inline input {
display:inline;
max-width:30%;
margin: 0 10px 0 0;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
}
button {
background-color: #f50;
box-shadow: none;
border: 0;
border-radius: 5px;
color: white;
padding: 5px 10px;
}
@media (max-width: 800px) {
.form-inline input {
margin: 10px 0;
max-width:100% !important;
}
.form-inline {
flex-direction: column;
align-items: stretch;
}
}
</style>
<div align="center">
<form class="form-inline" action="https://app.convertkit.com/forms/1052396/subscriptions" method="post" data-uid="8884355abb" data-format="inline" data-version="5">
<span> Want a weekly digest of this blog?</span>
<input name="email_address" type="text" placeholder="Email address" />
<button type="submit" data-element="submit">Subscribe</button>
</form>
</div>
</div>
<p class="meta">
<a class="basic-alignment left" href="https://jvns.ca/blog/2017/03/26/bash-quirks/" title="Previous Post: Bash scripting quirks &amp; safety tips">Bash scripting quirks &amp; safety tips</a>
<a class="basic-alignment right" href="https://jvns.ca/blog/2017/04/06/netdev-2-1/" title="Next Post: netdev conference, day 1">netdev conference, day 1</a>
</p>
</footer>
</article>
</div>
</div>
</div>
<nav role="navigation" class="footer-nav"> <a href="/">Archives</a>
</nav>
<footer role="contentinfo"><span class="credit">&copy; Julia Evans. </span>
<span>If you like this, you may like <a href="https://web.archive.org/web/20181228051203/http://www.uliaea.ca/">Ulia Ea</a> or, more seriously, this list of <a href="https://jvns.ca/blogroll">blogs I love</a> or some <a href="https://jvns.ca/bookshelf">books I've read</a>. <br>
<p class="rc-scout__text"><i class="rc-scout__logo"></i>
You might also like the <a class="rc-scout__link" href="https://www.recurse.com/scout/click?t=546ea46360584b522270b8c3e5d830f8">Recurse Center</a>, my very favorite programming community <a href="/categories/hackerschool/">(my posts about it)</a></p>
</span>
<style class="rc-scout__style" type="text/css">.rc-scout{display:block;padding:0;border:0;margin:0;}.rc-scout__text{display:block;padding:0;border:0;margin:0;height:100%;font-size:100%;}.rc-scout__logo{display:inline-block;padding:0;border:0;margin:0;width:0.85em;height:0.85em;background:no-repeat center url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2012%2015%22%3E%3Crect%20x%3D%220%22%20y%3D%220%22%20width%3D%2212%22%20height%3D%2210%22%20fill%3D%22%23000%22%3E%3C%2Frect%3E%3Crect%20x%3D%221%22%20y%3D%221%22%20width%3D%2210%22%20height%3D%228%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%222%22%20y%3D%222%22%20width%3D%228%22%20height%3D%226%22%20fill%3D%22%23000%22%3E%3C%2Frect%3E%3Crect%20x%3D%222%22%20y%3D%223%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%2361ae24%22%3E%3C%2Frect%3E%3Crect%20x%3D%224%22%20y%3D%223%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%2361ae24%22%3E%3C%2Frect%3E%3Crect%20x%3D%226%22%20y%3D%223%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%2361ae24%22%3E%3C%2Frect%3E%3Crect%20x%3D%223%22%20y%3D%225%22%20width%3D%222%22%20height%3D%221%22%20fill%3D%22%2361ae24%22%3E%3C%2Frect%3E%3Crect%20x%3D%226%22%20y%3D%225%22%20width%3D%222%22%20height%3D%221%22%20fill%3D%22%2361ae24%22%3E%3C%2Frect%3E%3Crect%20x%3D%224%22%20y%3D%229%22%20width%3D%224%22%20height%3D%223%22%20fill%3D%22%23000%22%3E%3C%2Frect%3E%3Crect%20x%3D%221%22%20y%3D%2211%22%20width%3D%2210%22%20height%3D%224%22%20fill%3D%22%23000%22%3E%3C%2Frect%3E%3Crect%20x%3D%220%22%20y%3D%2212%22%20width%3D%2212%22%20height%3D%223%22%20fill%3D%22%23000%22%3E%3C%2Frect%3E%3Crect%20x%3D%222%22%20y%3D%2213%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%223%22%20y%3D%2212%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%224%22%20y%3D%2213%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%225%22%20y%3D%2212%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%226%22%20y%3D%2213%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%227%22%20y%3D%2212%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%228%22%20y%3D%2213%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3Crect%20x%3D%229%22%20y%3D%2212%22%20width%3D%221%22%20height%3D%221%22%20fill%3D%22%23fff%22%3E%3C%2Frect%3E%3C%2Fsvg%3E');}.rc-scout__link:link,.rc-scout__link:visited{color:#61ae24;text-decoration:underline;}.rc-scout__link:hover,.rc-scout__link:active{color:#4e8b1d;}</style>
</footer>
<script type="text/rocketscript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
</div>
</body>
</html>