Traffic Shaping in Software Engineering

Traffic Shaping in Software Engineering


4 min read

Hello everyone! In this article I'm going to be talking about traffic shaping in software engineering. We're going to be talking through what it is, how it's achieved and some of its famous algorithms. Let's start

What is Traffic Shaping?

Traffic shaping, in simple terms, is like managing the flow of cars on a road to prevent congestion or accidents. In the context of software and web servers, it's about managing the flow of data (like requests and responses) to ensure smooth and efficient operation.

Traffic shaping is often used to manage the flow of data between users and the server. For example, if too many users are trying to access a website at once, traffic shaping can prioritize certain requests, like those from paying customers or those accessing critical services, while slowing down less important requests. This helps ensure that the server can handle the load without crashing or slowing down for everyone.

What is Traffic Shaping? — Definition by Techslang

In general, it aims to achieve the following:

  • Preventing Network Congestion

    By controlling the flow of traffic, traffic shaping ensures that network resources are utilized efficiently and that congestion-induced packet loss is minimized.

  • Prioritizing Critical Traffic

    Traffic shaping allows network administrators to prioritize critical traffic over less important or non-critical traffic

  • Enhancing Quality of Service (QoS)

    Traffic shaping plays a crucial role in enhancing Quality of Service (QoS) for users and applications. By controlling factors such as latency, jitter, and packet loss, traffic shaping helps maintain a consistent and predictable user experience

  • Optimizing Resource Allocation

    By allocating network resources judiciously, traffic shaping helps optimize resource utilization and maximize the overall performance of the network. By dynamically adjusting bandwidth allocation based on traffic patterns and user requirements

Traffic Shaping Algorithms

Two of the most well known algorithms of traffic shaping (used in rate limiting too) are:

  • Token Bucket Algorithm

  • Leaky Bucket Algorithm

  • Weighted Fair Queueing (WFQ)

  • Hierarchical Token Bucket (HTB)

Many more algorithms exist, we'll dive into these for now..

Token Bucket Algorithm

The token bucket algorithm is a widely used traffic shaping algorithm that regulates the rate at which packets are transmitted based on available tokens in a token bucket.

Each packet sent consumes a certain number of tokens from the bucket. If there are no tokens available, packets are queued or dropped. Tokens are replenished at a fixed rate, representing the maximum allowable transmission rate.

This algorithm allows bursts of traffic to be transmitted at higher rates as long as there are sufficient tokens available in the bucket

What is Token Bucket and Leaky Bucket algorithms | Hans Blog

Leaky Bucket Algorithm

A way to mitigate the bursts of traffic problem that the token bucket has is the leaky bucket algorithm.

The leaky bucket algorithm controls the rate of packet transmission by continuously leaking packets from a bucket at a fixed rate.

Incoming packets are buffered in the bucket until they can be transmitted. If the bucket is full, excess packets are discarded

This algorithm helps smooth out bursts of traffic by limiting the rate at which packets are released from the bucket.

A representation of the leaky bucket algorithm (Vidyasagar et al, 2013) |  Download Scientific Diagram

Weighted Fair Queueing (WFQ)

Now comes the algorithms that deal with prioritizing requests over other requests.

WFQ is a scheduling algorithm used in routers and network switches to prioritize traffic based on assigned weights. It divides available bandwidth among different traffic flows proportionally to their weights, ensuring that each flow receives its fair share of bandwidth.

WFQ can help prevent individual flows from monopolizing bandwidth and ensures that low-priority flows do not starve for resources in the presence of high-priority traffic.

Weighted Fair Queuing Technique. | Download Scientific Diagram

Hierarchical Token Bucket (HTB)

HTB is a more advanced version of the token bucket algorithm that allows for hierarchical shaping and prioritization of traffic.

It enables administrators to define multiple token buckets with different rates and burst sizes, allowing for more granular control over traffic shaping policies.

HTB organizes traffic into a hierarchical structure of token buckets, where each bucket represents a different level of prioritization or classification. This hierarchical approach enables fine-grained control over traffic shaping policies, allowing for prioritization based on various criteria such as source/destination IP addresses, ports, protocols, or application types.


Traffic shaping is a critical aspect of network management, enabling organizations to optimize network performance, manage bandwidth usage, and ensure reliable and responsive service delivery. By implementing effective traffic shaping strategies, organizations can enhance user experience, mitigate security risks, and improve overall network efficiency.

We've discussed some of the famous traffic shaping algorithms and there's definitely more out there. I thought I'd document myself learning about traffic shaping as well as share what I found. That's it for this one see you in the next article!

Did you find this article valuable?

Support Amr Elhewy by becoming a sponsor. Any amount is appreciated!