Ultimate Guide to Edge Computing & Enterprise IoT –  ClearBlade Best Practices

January 14, 2017

Welcome to the ClearBlade IoT platform. This document contains a number of helpful hints for developers creating and building IoT solutions. As a start, please check out the docs page where many concepts and tasks are explored in greater depth.

Code

Basics

  1. Code is designed for high scale / high performance real-time execution. As you author a service — consider its behavior at scale being called 1000’s of times a second
  2. Leverage Code like a serverless architecture
  3. Author services that are designed to run quickly and be responsive — They are not for batch work
  4. Ensure that all execution paths result in a resp.success or resp.error call
  5. When starting a service — the most efficient way to instantiate the ClearBlade object is using the ClearBlade.init(request:req) This call does not require any interaction with the backend but instead simply configures the ClearBlade library to use the existing valid auth token
  6. Add descriptions that identify under what conditions this service is known to be called

Performance

  1. Be careful when pulling back large datasets. Since the dataset has to be parsed by the code service, it is sometimes more efficient to make 10 calls of 1Mb payloads rather than 1 call of 10Mb
  2. Calls to third party software can fail; handle those slow and failed response gracefully
  3. Be careful of too much logging. If your service is running at production scale, don’t needlessly log. Turn off your logs

Reuse

  1. Identify common calls early and make reusable libraries. This will minimize both development and maintenance time.
  2. Keep libraries focused. Each library will be executed when the microservice runs, so make sure you only are bringing code you need into scope

Debugging

  1. Debug services at runtime by dynamically turning logging on and off for the service as needed
  2. Check your failed service log to see which service didn’t execute correctly. You can always fix your code and then rerun those service calls to recover state.

Messaging

Basics

  1. JSON has become a very friendly structure for IoT. Passing strings of JSON is an easy way to quickly parse and work with your data.
  2. Topic patterns are a powerful way to structure and optimize communication. Use them to your advantage http://tinkerman.cat/mqtt-topic-naming-convention/
  3. Messaging can be treated as a topic or as a queue. Identify the pattern that is right for your system and workload

Data

Basics

  1. When integrating with third party software — avoid duplicate table writes. When using ClearBlade attempt to pick a single location for data structures rather than reproducing them across different databases. At scale 3 writes to different connections can be a significant workload
  2. The default timestamp pattern is 1970–01–01T00:00:00Z

Connections

  1. ClearBlade generally behaves as expected when tables have primary keys. Logic to handle third party databases without PK’s can result in unexpected results
  2. Not all databases are optimized for certain activities (IE — Cassandra is slow for reads) Make sure you are using your other databases in ways that are optimal

Triggers

  1. Avoid looping triggers. Trigger execution can result in activities which in turn cause more triggers to fire. Be careful to not create an infinite loop of triggers that will consume your resources
  2. Leverage the req object to identify state. The req object brings key information about the reasons for and under what authority the trigger was fired. Use this information to optimize your trigger logic and cut down on total execution time.
  3. In systems that have a high volume number of messages, don’t trigger on each message but instead use a repeating timer that pulls the message history (queue) and then clears it. This will allow you to optimize your CPU and interactions.

Timers

  1. Use dynamic timers to make sure certain events have happened. If you want to make sure a device communicates every 30 minutes, then create a dynamic timer. Each time you see the device move, reset the dynamic timer in your service. Unlike normal timers, recurring timers only run when they need to
  2. Timers recurring in under ~1 sec duration can become un-ordered to the horizontal asynchronous behavior in the platform.

Users

  1. When doing third party integrations, store tokens and keys for those service in the user record. This will let you secure access to those tokens by default for the user.
  2. When integrating with a third party registry, Create an anonymous service that will check the external registry and then upon success perform the auth in the ClearBlade service. This will let you grant ClearBlade tokens based on the third party system
  3. Verify your email addresses
  4. Downcase your email addresses at creation. The specification for email is nonstandardized. Protect your users from creating duplicate users by enforcing all lower case characters.
  5. Rapidly build 2-factor auth with the use of anonymous services

Devices

  1. Consider a portal for defining a common device experience. This portal will allow you to standardize what devices and schemas get submitting rights into your system.
  2. Device can have completely unique schemas.
  3. Provisioning devices into the table without enabling them. This will let you create the device record but not allow it to interact until the device is officially registered in the field.

Scalability

  1. When attempting to perform large amounts of analytics, consider raking and grooming your data at the edge first. The edge will let you process sensor data before it goes to the internet and incurs cloud costs.
  2. Consider the right data storage for your goals. Some cloud services like Splunk have a hard time keeping up with ClearBlade mqtt. If the end service cannot handle the data volume, try a different strategy that does not cause blocking behavior in your services.

Scalability

  1. Ensure adapters handle token expiration and reauth
  2. Maintain a thread for each unique device that is connected via the adapter for making sure it’s using its correct credential
  3. Consider automating the provisioning process as part of your adapter spec so that new devices can quickly provision in an abstraction fashion

Schedule A 30 Minute Demo