Skip to content

Direct connections and gateways

A device can store its data in SQL Server in two ways: connect to the server itself, as tdslite does, or send the data to a service in between (a REST API, an MQTT broker with a subscriber, a cloud IoT hub) that writes it to the database. Both are reasonable. Which one fits depends mostly on who can reach the device, who can reach the server, and how many devices there are.

What a direct connection gives you

  • Fewer parts. No service to write, host, update and monitor; the device and the database are the whole system.
  • The full SQL language. Queries, transactions, stored procedures, and reading data back, not only the calls an API happens to offer.
  • Lower latency and less code on the device. One protocol, one connection, no JSON to build or parse.
  • Server tools work as they are. Logins, permissions, auditing, sys.dm_exec_sessions: SQL Server sees each device as a client.

What it costs

  • The device holds a database login. Anyone with the device can read the password from its flash. The login can do whatever its permissions allow, from anywhere that can reach the server, until you change the password. With a gateway, the device holds a token that only works on the gateway, and that you can revoke on its own.
  • The server must be reachable from the device. On the same network that's a given. Over the internet, it means opening SQL Server's port, which exposes it to password guessing and to any flaw in the server. Don't do that for an on-premises server; use a VPN, or a gateway. Azure SQL is made to be reached over the internet, behind its firewall rules.
  • Without TLS, the traffic can be read. TDS sends the password obfuscated, not encrypted, and the data in plain text. On a network you don't control, use TLS, which needs a board that can do it and a server that accepts TDS 8.0 strict connections.
  • The firmware knows the schema. An INSERT in the firmware has to change when the table does. A stored procedure moves that SQL to the server, so you can change the table without updating the devices.
  • Each device holds a connection. SQL Server handles hundreds of connections without trouble. For thousands of devices, or devices that connect and disconnect all the time, a gateway that collects the data and writes it in batches is easier on the server.

When a direct connection fits

A direct connection is a good fit when the devices and the server share a network that you control: a lab, a workshop, a factory floor, a building, a home. It's also a good fit for prototypes and small fleets, where running a separate service would be most of the work.

A gateway is the better choice when devices are spread over networks you don't control, when there are many of them, when a device could be stolen and its credentials must be revoked on their own, or when other systems need the data too.

If you connect directly

  • Give the devices a login of their own, with only the permissions they need: INSERT on one table, or EXECUTE on one stored procedure. See Log sensor readings and Call a stored procedure.
  • Use TLS whenever the network isn't fully yours.
  • Don't expose an on-premises SQL Server's port to the internet.
  • Plan to change the password: keep it in one place in the firmware, or in a configuration the device reads at startup, so a new one is easy to deploy.