Improve network performance of apps with DiffServ
Recently I started working on the messaging app which works even when you don’t have internet connectivity to the server, using a direct IP address in Local Network (LAN). So even if the Internet is not available users can still communicate as long as they’re in the same network. While implementing this I was introduced to the networking concept Differentiated Services DiffServ. I would try to explain it here. This article is more useful for apps using socket connections, apps using HttpUrlConnection this won’t benefit much.
Differentiated Services
By definition : Differentiated services or DiffServ is a computer networking architecture that specifies a simple and scalable mechanism for classifying and managing network traffic and providing quality of service (QoS) on modern IP network.
If you’re as lazy as me you won’t be having any clue what exactly definition means, anyways Differentiated Services means :
Way to classify the outgoing packets on router/switches, using which packets can be prioritise one above other.
It’s like setting up different queues for different types of class as we see in airport check-in process VIP, Business, and economy class. Now the obvious question is what is the need of classifying the packets?
Need for Differentiated services :
Imagine you’re connected to wifi router having a bandwidth of 15 Mbps and it already has 10 other devices connected. Somebody is just surfing, someone is using Facebook, someone is streaming a YouTube video etc. At this point, you get the VoIP call on any app like Whatsapp, Skype, or Hangout. Without having Differentiated services here starts trouble as packets generated from your device would be treated as a normal priority and you would notice the delay in the communication. To overcome this problem we need to give some kind of instructions to the router that our packets fall into VIP category and send them before any of the other packet goes out. Since VoIP service uses UDP connection we will lose some packet in transit but if this loss is of greater than 250ms then we will be having a hard time having communication on call.
Where to configure?
Here goes a bit of theory first. Just recall your college days where you studied about the IP Packet.
In IP packet 8 bits are reserved for the type of Service or Differentiated services after version and the header. This field is used to indicate the type of the packet and gets treated accordingly on the hardware layer.
How to configure?
By setting DiffServe values we are assigning them a class. Before starting on the configuration we will go through how the packets are treated once they leave your device.
- At network layer packets a.k.a traffic gets classified by many different parameters such as source address, destination address, or traffic class.
- These classifiers may honor the DiffServ marking or they may ignore them completely. Because Internet Service Providers ISP wants to keep tight control over volume and type of traffic in a given class.
- And this is one of the reasons why some sites like Netflix, Youtube have very low response times.
Generally, most of the networks use the following commonly defined Per-Hop Behaviours (PHB)
- Default Forwarding (D PHB) : Any traffic which does not meet the requirements of any of the defined classes gets treated as default. DSCP value for default is 0B000000.
- Expedited Forwarding (EF PHB) : It is used to minimize the time packet spent in a queue. Therefore EF’s goal is to achieve low delay, low packet loss, and low jitter. EF traffic is strictly prioritized over all other traffic classes which makes it a perfect fit for Voice, Video, and other real-time services. DSCP for expedited forwarding is 0B101110.
- Assured Forwarding (AF PHB) : Assured forwarding allows the operator to provide assurance of delivery as long as the traffic does not exceed some subscribed rate. There are four classes and three drop precedence priorities High, Medium, Low. If congestion occurs within the class then packets with a High precedence rates are discarded first. Used in Youtube, Gaming etc. You can find out more on DSCP values here.
- Voice Admit : It is used in the Voice call setup procedure and it has the same characteristics as the EF with the only difference that it can be recognized by the network using Wireshark or similar tools. If you find this type of packet then you are pretty much sure that it is a Voice call packet. DSCP for voice admit is 0B101100.
Some Code :
In Java this snippet creates the DatagramSocket :
DatagramSocket socket = new DatagramSocket(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
socket.setReuseAddress(false);
socket.setSoTimeout(15000);
To assign the DSCP value you need to set value of the TrafficClass :
socket.setTrafficClass(0B101110)
This will set the TrafficClass to EF — Expedited Forwarding. As far as the programmer’s job is considered this is max we can do to prioritize our app’s traffic on the network layer. Now once the packet leaves the device we have no control over in which manner it will be processed, transmitted, and delivered.
Usage of DiffServ in the real world :
If the packets stay within your own network then you can apply and control QoS policies using these DiffServ markings — effectively giving some traffic higher priority than others, with sensible limits and safeguards.
Once you go into someone else’s network or onto the internet, most networks will reset or remove any Diffserver marking at the ingress point to the network.
This is because the network provider itself typically wants to decide which traffic, if any, to prioritize and will not be happy to leave it to clients of the network (unless you have an SLA or other agreement with the network provider to support some QoS scheme — some providers will support services which offer higher service then the typical best effort level, for example, BT in the UK offer a priority broadband service, although I am not sure how much take-up there is on these types of services).
Note that the service providers will often have QoS set up right to customers home or premises to provide services like VoIP, IPTV etc — these require better than best-effort generally.
Ref : https://stackoverflow.com/a/18859892/3746306
Conclusion :
If you’re developing an app-specific for communication within your organization’s network then you might consider applying Quality of Service policies on the network using DiffServ markings and see the real difference in the network performance of the app. Although setting up Quality of Service is Network Engineer’s job, you can anytime learn new things with this try.
Addendum :
- Overview of Differentiated Services for Quality of Service by Cisco
- Differentiated Services Design and Implementation by IIT Kharagpur India
You might find this story interesting as well.
Feel free to discuss, critique, and share.
My LinkedIn, Facebook. Let’s connect. :)