Edge Computing Architecture — Fog vs Edge, AWS Greengrass, Azure IoT Edge, ML at Edge
Edge computing processes data near its source — on a gateway, industrial PC, or capable microcontroller — instead of sending everything to the cloud. This tutorial covers the architecture patterns, cloud platforms, and ML deployment strategies that make edge computing work in production.
In this tutorial, you’ll learn the edge gateway pattern, how AWS Greengrass and Azure IoT Edge deploy cloud services locally, running ML inference on edge hardware, handling offline operation gracefully, and filtering data to reduce cloud costs. This architecture powers autonomous vehicles, smart factories, and oil rig monitoring where latency and bandwidth are critical.
A factory with 10,000 vibration sensors produces 864 million readings per day. Sending all data to the cloud costs $50,000/month in bandwidth. Edge gateways process 99% of data locally, triggering real-time alerts and sending only aggregated insights — cutting cloud costs by 90% while reducing alert latency from seconds to milliseconds.
Edge vs Fog vs Cloud
graph TD
subgraph "Cloud Layer"
Cloud[AWS / Azure] --> Storage[Data Lake]
Cloud --> Training[ML Training]
Cloud --> Dashboard[Global Dashboard]
end
subgraph "Fog Layer"
GW1[Edge Gateway] --> Cloud
GW2[Edge Gateway] --> Cloud
end
subgraph "Edge Layer"
S1[Sensor Array] --> GW1
S2[Sensor Array] --> GW2
S3[Camera] --> GW2
end
style Cloud fill:#3498db,color:#fff
style GW1 fill:#e67e22,color:#fff
style GW2 fill:#e67e22,color:#fff
style S1 fill:#27ae60,color:#fff
| Layer | Location | Latency | Compute | Example hardware |
|---|---|---|---|---|
| Edge | On device | <10ms | Very constrained | ESP32, Raspberry Pi Pico |
| Fog | Local network | <50ms | Moderate | NVIDIA Jetson, industrial PC |
| Cloud | Data center | >100ms | Unlimited | AWS, Azure, GCP |
AWS Greengrass — Lambda at the Edge
AWS Greengrass extends AWS Lambda, device shadows, and MQTT to edge devices. Lambda functions run locally and sync to AWS IoT Core when connectivity is available.
# greengrass_predictive_maintenance.py
import json
THRESHOLD_VIBRATION = 5.0
def lambda_handler(event, context):
reading = json.loads(event)
risk = reading['vibration'] / THRESHOLD_VIBRATION
if risk > 0.7:
return {'action': 'ALERT', 'severity': 'critical',
'message': 'Immediate shutdown required'}
elif risk > 0.4:
return {'action': 'WARNING', 'message': 'Schedule inspection'}
return {'action': 'OK', 'message': 'Normal operation'}Greengrass synchronises device shadows — the edge device’s state in the cloud — even when the device is offline. When connectivity returns, changes sync automatically.
Azure IoT Edge — Modules and Offline Operation
Azure IoT Edge deploys Docker modules to edge devices. Each module runs in a container, communicating via the IoT Edge hub.
# edge_vibration_filter.py — Azure IoT Edge module
import json
import iothub_client
class VibrationModule:
def __init__(self):
self.buffer = []
def process(self, message):
data = json.loads(message)
self.buffer.append(data['vibration'])
if len(self.buffer) >= 10:
avg = sum(self.buffer) / len(self.buffer)
self.buffer = []
return {'average_vibration': avg, 'samples': 10}
return None # buffer until we have enough dataOffline Data Flow
class OfflineCache:
def __init__(self):
self.queue = []
def store_offline(self, data):
self.queue.append(data)
if len(self.queue) > 10000:
self.queue.pop(0) # drop oldest if full
def sync_on_reconnect(self):
for data in self.queue:
self.send_to_cloud(data)
self.queue.clear()ML Inference at the Edge
Running ML models on edge hardware requires optimisation. Use quantised models (TensorFlow Lite) and hardware accelerators (GPU, TPU, NPU).
# tflite_edge_inference.py
import tflite_runtime.interpreter as tflite
import numpy as np
interpreter = tflite.Interpreter(model_path="model_quantized.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
def predict(sensor_data: np.ndarray) -> float:
interpreter.set_tensor(input_details[0]['index'],
sensor_data.astype(np.float32))
interpreter.invoke()
return interpreter.get_tensor(output_details[0]['index'])[0][0]
# Test with simulated sensor data
data = np.array([[0.5, 0.3, 0.8, 0.1]])
result = predict(data)
print(f"Edge inference result: {result:.3f}")Expected output:
Edge inference result: 0.872Common Mistakes
- Processing everything at the edge: Filter locally but send raw data periodically for model retraining and auditing.
- No offline fallback: Edge devices lose connectivity. Cache data locally and implement a sync-on-reconnect pattern.
- Running heavy ML on underpowered hardware: A quantised TFLite model runs on Raspberry Pi 4 at 10fps. The same model on ESP32 takes 30 seconds. Match model size to hardware.
- Ignoring model drift: Edge ML models become stale as equipment ages. Implement OTA model updates with A/B testing.
- Security blind spot: Edge devices are physically accessible. Encrypt local storage, use secure boot, and disable debug UART in production.
Practice Questions
What’s the difference between edge and fog computing? Edge processes on the device itself (microseconds latency). Fog processes on the local network gateway (milliseconds). Fog spans edge and cloud.
How does AWS Greengrass handle offline operation? Lambda functions run locally, device shadows cache state changes, and synchronisation resumes when connectivity returns.
What data should be filtered at the edge? Filter routine data (normal readings, heartbeats). Send alerts, anomalies, aggregated statistics, and periodic raw samples.
How does model quantisation help edge ML? Quantisation reduces model size from 32-bit floats to 8-bit integers, shrinking models 4x and accelerating inference by 2-3x on edge hardware.
What is the edge gateway pattern? A gateway device sits between sensors and the cloud, translating protocols, filtering data, running local logic, and buffering offline.
Challenge
Build an edge anomaly detector using z-score: collect 100 sensor readings, calculate running mean and standard deviation, flag values with |z| > 3, and send only flagged values to the cloud.
FAQ
Cross-References
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro