8

A Linux box sends measurements to AWS-RDBMS. A python script opens and closes the connection only long enough to upload data to the database (sessions is closed immediately after update). The alternative is that the box opens up an indefinite session to the database and update the RDBMS: not sure what problems would be with this if the internet connection failed and unsure of the degree of the connection's 'persistence' when faced with unstable internet connection. At scale, there could be hundreds of measurement boxes uploading data to the RDBMS.

What is the best practice with regard to python IoT session connection duration? Is it best practice to close the session after transmitting the data? Maybe define an idle time that starts after data is transmitted: if the idle time reaches more than a predefined amount of time then close the channel. Bonus for any explanation of the why behind the best practice.

Perhaps this question is dependent on the platform? i.e. RDBMS vs AWS Greengrass?

gatorback
  • 381
  • 1
  • 5

2 Answers2

2

The variables which IoT bring to the party here are, at a minimum, scale and energy. You might also consider security/resilience as being subject to different constraints compared with how this question might have been answered 5 years ago. The 'root' question on SO is already 7 years old and not hugely popular.

The frequency of transactions and the cost of setting up an authenticated connection is probably the primary driver, both in terms of data bandwidth and energy costs.

Is there an cost to leaving the connection open? How much more effort will you need to manage a 'mostly persistent' connection compared to the more trivial 'open-use-close'? Which approach will result in resource contention fastest (assuming your product is planned to scale massively)?

Are you likely to migrate your code/protocol further to the edge of the network (or migrate the cloud functionality closer to the edge) in the future? These potential migrations might change your constraints, or you might prefer to defer the potential software investment.

Sean Houlihane
  • 10,524
  • 2
  • 26
  • 62
1

Although you are using the session for IOT, this is *not an IOT question, just a general sockets question, and, as such, there are many, many, many answers on Google and on Stack Overflow.

Basically, there is no "correct" answer, you just need to consider your situation.

Generally speaking, it is easier to implement "close after every transaction", as you don't need as much "ooops, my currently open session just closed unexpectedly" code. That is the approach that I always take, and have seen most often used in industry.

However, if you need to send frequent packets (size is not important, just the frequency), then the overhead of closing & reopening after every packet can lead to performance issues.

You pays your money, you takes your pick :-)

Mawg
  • 3,147
  • 1
  • 13
  • 35