When it comes to communications for IoT there is a lot to consider. In this article we will cover a small piece of this topic, namely answering the question: Is UDP the right choice for IoT?

What is UDP?

UDP is one of the two most used formats for sending packets on the internet, the other being TCP.

TCP communications involve setting up a connection between the sender and receiver. You can think of a TCP connection as pair of pipes between the source and destination. One pipe for sending data and the other for receiving, TCP has lots of fancy features such as automatic error checking, packet reordering, and rate limiting to mention a few.

UDP on the other hand is much more basic. It is basically a fire-and-forget protocol. The sender fires a UDP packet at the destination address and hopes that the message is received. If the setup allows, the receiver can send back an acknowledgement to the sender in the same manner. This may seem very primitive, but it has a lot of useful applications. One of which involves IoT.

When is UDP the right choice for IoT?

There are 5 main reasons why UDP is the right choice for IoT projects.

1. Small amounts of data

Small amounts of data, such as sensor readings, are ideal to send using UDP. The overhead of setting up a TCP connection to send a small amount of data can quickly outweigh the data being sent. There is no setup however for UDP which makes data usage much more efficient.

2. Hardware is low power

Low-power hardware is typically hardware that needs to run on batteries for extended periods of time. In this case, efficiency is very important. Every few microamps of power consumption matters when you measure it over a 5 or 10-year period. Typically, low-power devices are in deep sleep most of the time and wake up to process an event or send data over the network. Every extra second spent in an active mode decreases the remaining battery.

This is where UDP starts to become important. TCP communications require setting up a TCP connection first. For a device that stays online all the time (high-power devices), this connection can be held open. However, low-power devices must power down which in turn drops any connection. So, for low-power devices to use TCP, they must negotiate a new TCP connection each time. TCP negotiation takes tens to hundreds of milliseconds and requires a few round trips between the device and the server. Modem on-time is one of the most expensive aspects of a low-power device in terms of battery consumption and is one of the first things which needs to be optimised to achieve consistent low-power behaviour.

UDP on the other hand does not require any connection setup. Once the modem is powered on and connected to the cellular network a UDP packet can be flung off into the network. Depending on the system setup, the device can either then wait for an acknowledgement from the server, or just go back to a low-power state and hope the packet was received. This will save tens to hundreds of milliseconds of each transaction, which is significant over the life of the device.

3. Connection Stability

Connection stability is another important factor for devices in areas with poor cellular coverage. With TCP, not only does the connection take effort to set up, but it also needs to be stable. If the connection drops partway through a transmission then the whole process of connecting needs to be set up again. In areas where cellular coverage is patchy, it is not guaranteed that a connection will stay open reliably even for a few seconds. This results in many attempts to connect and send data consuming both battery and cellular data.

If connection stability is likely to be an issue, then UDP might be the right choice, because it does not have any requirements for keeping a connection open.

4. NB-IoT

In many countries, there is a selection of cellular networks to choose from these days. LTE (3G & 4G), 5G, CAT-1, CAT-M1, and NB-IoT.

Of these, CAT-M1 and NB-IoT are the two most applicable networks for low-power devices to use. These networks have the longest reach, in terms of distance from a cell tower to the device and require less power on the device side. Some countries have implemented CAT-M1 networks, some have NB-IoT networks, and a few have both. NB-IoT has the greatest reach and the lowest power, but that comes with some trade-offs. One of those trade-offs is that it does not work well with TCP communications. In some situations, it can work fine but it was not designed to maintain the quality of connection that TCP requires.

So, if you find yourself needing to use the NB-IoT network UDP might be your best option.

5. Data Efficiency

One downside of a TCP connection is that it needs to be kept alive. The protocol dictates that if there has been no communication for a fixed period of time, then the connection should timeout and be dropped. For devices that communicate infrequently, this can be a problem. The way to get around this is to send “keep-alive” packets, typically every 5 minutes. These keep-alive packets are about 64 bytes in size, but over the course of a month account for about half a megabyte of data, which on a small IoT data plan can be a significant proportion of the data allowance simply lost in overhead keeping the connection alive.

Security can also come into play when it comes to data efficiency. TCP uses the TLS layer for encryption. TLS handshakes are needed for every connection and consume kilobytes of data for security certificate exchange. UDP has no standard way of doing security, but there are more efficient ways of encrypting data than TLS in an IoT setting.

When is UDP NOT the right choice for IoT?

While there are some compelling cases for using UDP for IoT communications, there are some reasons why this may not be the right choice.

1. Realtime communications

In a UDP-based low-power scenario, the devices spend most of their time asleep. As a consequence, they cannot receive messages because their modem is turned off. Also in a UDP-based situation, device communications always need to be initiated from the device. The server then has a small window to reply, before that window shuts and the device needs to send another packet for the window to open again.

If real-time bi-directional communications are a requirement, then TCP is the best option, because the connection is always open and able to receive data. In this situation, the requirement for low-power will need to be re-examined because low-power and real-time bi-directional communication are incompatible with each other.

2. MQTT

MQTT is a protocol that is commonly used for device communication by major cloud vendors. MQTT is great at what it does, but unfortunately only works with TCP. If it is a requirement to use MQTT then UDP-based communication is off the table, and so probably is very low power.

3. Development is harder

Most of the cloud providers and 3rd party tools assume a TCP connection is available. There are not so many libraries or tools available for UDP communications, and the ecosystem is not as mature. This makes development much harder.

4. High efficiency not needed

If the solution does not need to be highly efficient and can be mains powered. Then taking the extra effort to use UDP may not be worth the effort when off-the-shelf tools are more readily available.

5. Lots of data

Raw UDP communications are great for sending small amounts of data, which can fit into a single packet. If on the other hand, your application is data intensive such as streaming video from a camera, then TCP might be a better choice as the advanced features of the protocol are much better suited to high throughput applications.

Conclusion

So coming back to the original question “Is UDP the choice for IoT?” The answer is it depends.

If the application in question is low power and uses the cellular network to communicate (especially NB-IoT) the answer is almost certainly yes, UDP is the right answer.

On the other hand, if power and data efficiency are not so important then the options open up.

This is part 1 of a multi-part series on UDP for IoT communications.

Check out the rest of the series:

Part 1: Is UDP the right choice for IoT?

Part 2: Is it a good idea to use raw UDP packets for IoT Communications?

Part 3: CoAP — The best way to communicate over UDP for IoT applications