Limit Spotify upstream bandwidth with trickle02 Sep 2013
While debugging network related stuff on my router, I noticed that something in my network was using all the available DSL upstream bandwidth. The only active application at that time was Spotify and it wasn't even playing a song. After digging around in the interwebz for a time, I found quite a few reports about this behaviour. Spotify is using p2p techniques to distribute content. So, if somebody plays a song, Spotify tries to download it from another user (maybe you!) who has the song already sitting in his computers disk cache. While Spotify is saving money by using p2p, because it decreases bandwidth usage on their servers, it can lead to problems on your side. Spotify sometimes decides to use ALL of the available upstream, causing short lag spikes in games or SSH sessions. Or maybe your ISP limits traffic to a certain amount of data per month.
Since Spotify does not include an option to limit p2p upstream (Hey Spotify, I am a paying customer and I am pissed!) you have to help yourself and use the neat Linux tool "trickle" to limit the Spotify upstream maximum.
The first thing you need to do, is installing "trickle". It should be somewhere in your package repository, so in Debian/Ubuntu just type:
sudo apt-get install trickle
Trickle allows you to limit downstream and upstream of a userspace application. It does so by shaping SOCK_STREAM of connections established via the socket interface. It will not work on setuid and statically linked executables.
You can run Spotify with an upstream limit of 40KB/s (the Spotify path may differ slightly, depending on your Linux distribution):
trickle -s -d 0 -u 40 /usr/bin/spotify
The -s tells trickle to run in standalone mode (it also supports a daemon mode), -d 0 will allow unlimted downstream and -u 40 will enforce an upstream limit of 40KB/s.
Everytime you launch Spotify via trickle, you can enforce your specified limits. But maybe you want to start Spotify via your desktops volume indicator or by clicking on a Spotify link in your browser. To make your limits permanent you can use a little trick: /usr/bin/spotify is just a symlink to /opt/spotify/spotify-client/spotify. You can remove the symlink and replace it with a little wrapper shell script. You can use the following script to do it for you, save it and run it as root via sudo:
#!/bin/sh -e # remove spotify symlink [ -L /usr/bin/spotify ] && rm /usr/bin/spotify # create spotify wrapper script cat >/var/tmp/spotify << END #!/bin/sh exec trickle -s -d 0 -u 40 /opt/spotify/spotify-client/spotify $* END # move wrapper script to /usr/bin/spotify [ -L /usr/bin/spotify ] || mv /var/tmp/spotify /usr/bin/spotify # make wrapper script executable chmod +x /usr/bin/spotifyNow when Spotify is started via /usr/bin/spotify, trickle will be used to enforce bandwidth limits on the Spotify process. If you are interested in a detailed description on how the Spotify content distribution works and why it is using your upstream have a look at How Spotify Works.