Basic Reliability and Security
Networks fail. Devices go offline. The cloud might be down. How do we handle that? And how do we keep things secure?
Networks Aren’t Perfect
Things that can go wrong:
- Wi-Fi drops - Router restarts, signal weakens
- Cloud is down - Service maintenance, outages
- Device loses power - Battery dies, power outage
- Network congestion - Too much traffic, slow connection
The system needs to handle these gracefully.
Simple Reliability Patterns
Retry with Backoff
If sending fails, try again. But don’t spam. Wait a bit longer each time.
def send_with_retry(message, max_attempts=3):
for attempt in range(max_attempts):
try:
send_to_cloud(message)
return True # Success!
except NetworkError:
if attempt < max_attempts - 1:
wait_time = 2 ** attempt # 1s, 2s, 4s
time.sleep(wait_time)
else:
return False # Failed after all retries
return False
Offline Buffer
Store messages locally when offline. Send them when back online.
# Local storage (simple list, or use a file/database)
message_buffer = []
def send_message(message):
try:
send_to_cloud(message)
except NetworkError:
# Store for later
message_buffer.append(message)
print(f"Stored message locally. Buffer size: {len(message_buffer)}")
def flush_buffer():
"""Send all buffered messages when back online"""
while message_buffer:
message = message_buffer.pop(0)
try:
send_to_cloud(message)
except NetworkError:
# Put it back and stop
message_buffer.insert(0, message)
break
Try It: Failure Simulation
See what happens when things fail:
Basic Security Ideas
API Keys / Tokens
The cloud needs to know who you are. Use an API key or token.
# Device code
API_KEY = "sk_abc123xyz"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(url, json=message, headers=headers)
Why it matters:
- Prevents unauthorized devices from sending data
- Cloud can track which device sent what
- Can revoke access if device is compromised
Token Reveal
TLS (Encrypted Connection)
Use HTTPS (not HTTP) or MQTT over TLS. This encrypts the connection.
What it does:
- Encrypts data in transit
- Prevents eavesdropping
- Ensures data integrity
In code:
# Use https:// not http://
url = "https://api.example.com/telemetry" # Encrypted
# url = "http://api.example.com/telemetry" # Not encrypted (don't use)
Validation
The cloud should validate incoming data:
def validate_message(message):
# Check required fields
if "device_id" not in message:
return False, "Missing device_id"
if "temperature_c" not in message:
return False, "Missing temperature_c"
# Check data types
if not isinstance(message["temperature_c"], (int, float)):
return False, "temperature_c must be a number"
# Check ranges
if message["temperature_c"] < -50 or message["temperature_c"] > 100:
return False, "temperature_c out of range"
return True, "OK"
Security Best Practices
Do:
- Use API keys/tokens
- Use HTTPS/TLS
- Validate all incoming data
- Keep keys secret (don’t commit to git)
- Rotate keys periodically
Don’t:
- Send data over unencrypted connections
- Expose raw endpoints without authentication
- Trust data from devices blindly
- Hardcode keys in your code
Reliability Best Practices
Do:
- Implement retries with backoff
- Buffer messages when offline
- Log errors for debugging
- Monitor device health
- Set timeouts on network calls
Don’t:
- Retry forever (set max attempts)
- Ignore errors silently
- Send duplicate messages
- Block on network calls
What’s Next?
You now understand the complete flow: sensor → device → network → cloud → dashboard. You also know how to handle failures and keep things secure. In the final page, we’ll test your knowledge and suggest next steps.