SQS and Pub/Sub Queue Routing Strategies
In serverless geospatial pipelines, message routing dictates throughput, cost, and fault tolerance. When ingesting spatial payloads—ranging from Shapefile archives to GeoTIFF metadata and vector feature collections—SQS and Pub/Sub Queue Routing Strategies become the control plane for workload distribution. Effective routing ensures that coordinate reference system (CRS) transformations, topology validation, and raster tiling are dispatched to appropriately sized compute environments without creating bottlenecks or orphaned jobs. This guide details production-tested patterns for routing geospatial jobs across AWS and GCP, extending the architectural foundations established in Event-Driven Geospatial Processing Patterns.
Prerequisites
Before implementing routing logic, ensure your environment meets the following baseline requirements:
- Cloud Infrastructure: Active AWS and/or GCP projects with IAM/Service Account permissions to create queues, topics, subscriptions, and configure routing policies.
- Python Runtime: Python 3.9+ with
boto3(≥1.28.0) andgoogle-cloud-pubsub(≥2.19.0) installed. Use virtual environments to isolate dependencies. - Message Schema Standardization: A consistent JSON envelope for geospatial payloads containing mandatory fields:
source_uri,crs,processing_mode, andpriority. - Monitoring Stack: CloudWatch or Cloud Logging configured for queue metrics (approximate age, visible count, DLQ depth, and consumer lag).
- Network Configuration: VPC endpoints or Private Service Connect enabled if routing sensitive spatial data through public endpoints violates compliance requirements.
Core Routing Architectures for Geospatial Workloads
Geospatial workloads exhibit distinct routing requirements compared to generic event streams. Spatial data is inherently heterogeneous, often requiring different compute profiles for vector topology checks versus raster resampling. The following strategies address these characteristics.
Content-Based Routing via Message Attributes
Both SQS and Pub/Sub support attribute-driven filtering. By tagging messages with metadata such as crs=EPSG:4326, format=GeoJSON, or operation=mosaic, you can route payloads to specialized consumers. This avoids monolithic workers that must dynamically detect and adapt to spatial formats, reducing cold-start latency and memory overhead.
When publishing, attach these attributes directly to the message envelope rather than parsing the payload body. This enables brokers to filter messages before they reach consumers, saving compute cycles and reducing network chatter. AWS SQS supports up to 10 message attributes per message, while GCP Pub/Sub allows arbitrary key-value pairs that are evaluated at subscription time.
Priority Routing for Time-Sensitive Tiles
Satellite ingestion pipelines often require near-real-time processing for disaster response tiles while deferring historical archive jobs. Implementing priority queues with separate visibility timeouts ensures high-priority geometries are processed first without starving background batch jobs.
A reliable approach uses dedicated queues per priority tier (e.g., geo-priority-high, geo-priority-normal, geo-priority-low). Producers evaluate the priority field in the incoming envelope and dispatch to the corresponding queue. Consumers poll high-priority queues with shorter visibility timeouts (e.g., 30 seconds) and lower-priority queues with extended timeouts (e.g., 5 minutes). This architecture aligns naturally with Batch vs Stream Geospatial Processing paradigms, allowing teams to decouple latency-sensitive streaming jobs from resource-intensive batch transformations.
Geo-Partitioned Routing (Spatial Sharding)
Large-scale raster processing benefits from partitioning by tile index or geographic bounding box. Routing messages to region-specific queues (e.g., us-west-tiles, eu-central-tiles) aligns compute placement with data locality, minimizing cross-region egress costs and improving cache hit rates for intermediate outputs.
This pattern is typically triggered at the ingestion layer. When storage events fire, a lightweight router evaluates the spatial extent of the incoming dataset and forwards the message to the appropriate regional queue. For file-based ingestion, this integrates seamlessly with S3 and GCS Event Triggers for Shapefiles, where bucket notifications are intercepted by a routing Lambda or Cloud Function before entering the main processing pipeline.
Platform-Specific Implementation Patterns
AWS SQS: Attribute-Driven Dispatch
AWS SQS does not natively route messages to multiple queues based on attributes. Instead, routing logic must be implemented in the producer or an intermediary service like AWS Lambda or EventBridge. The following Python dispatcher demonstrates a production-ready routing function with explicit error handling and type safety:
import boto3
import json
import logging
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
sqs = boto3.client("sqs")
QUEUE_MAP = {
"vector": "arn:aws:sqs:us-east-1:123456789012:geo-vector-queue",
"raster": "arn:aws:sqs:us-east-1:123456789012:geo-raster-queue",
"metadata": "arn:aws:sqs:us-east-1:123456789012:geo-metadata-queue"
}
def route_geospatial_message(payload: dict) -> dict:
processing_mode = payload.get("processing_mode", "vector").lower()
target_queue = QUEUE_MAP.get(processing_mode, QUEUE_MAP["vector"])
message_attrs = {
"crs": {"DataType": "String", "StringValue": payload.get("crs", "EPSG:4326")},
"priority": {"DataType": "String", "StringValue": payload.get("priority", "normal")},
"source_uri": {"DataType": "String", "StringValue": payload["source_uri"]}
}
try:
response = sqs.send_message(
QueueUrl=target_queue,
MessageBody=json.dumps(payload),
MessageAttributes=message_attrs,
DelaySeconds=0
)
return {"status": "success", "message_id": response["MessageId"], "queue": target_queue}
except ClientError as e:
logger.error(f"SQS routing failed for {payload['source_uri']}: {e}")
raise
For advanced filtering, pair SQS with Amazon EventBridge. EventBridge can evaluate JSON path expressions and route to multiple SQS queues simultaneously, eliminating custom dispatcher code. Refer to the official SQS Message Attributes documentation for attribute limits and serialization rules.
GCP Pub/Sub: Subscription Filtering
GCP Pub/Sub natively supports subscription-level filtering, allowing a single topic to fan out to multiple consumers without intermediary routing logic. Each subscription defines a filter expression that evaluates message attributes before delivery.
from google.cloud import pubsub_v1
from google.api_core.exceptions import GoogleAPIError
publisher = pubsub_v1.PublisherClient()
TOPIC_PATH = publisher.topic_path("my-project", "geospatial-ingest")
def publish_with_attributes(payload: dict) -> str:
data = json.dumps(payload).encode("utf-8")
attributes = {
"crs": payload.get("crs", "EPSG:4326"),
"processing_mode": payload.get("processing_mode", "vector"),
"priority": payload.get("priority", "normal")
}
try:
future = publisher.publish(TOPIC_PATH, data=data, **attributes)
return future.result()
except GoogleAPIError as e:
raise RuntimeError(f"Pub/Sub publish failed: {e}")
Once published, configure subscriptions with filter syntax such as attributes.processing_mode = "raster" or attributes.priority = "high". GCP evaluates these filters at the broker level, ensuring consumers only receive relevant payloads. See Pub/Sub Subscription Filters for syntax specifications and performance implications.
Fault Tolerance and Operational Resilience
Routing strategies must account for partial failures, malformed payloads, and downstream consumer crashes. Geospatial transformations are particularly prone to failures due to corrupted geometries, unsupported projections, or out-of-memory conditions during large raster operations.
Implement exponential backoff with jitter for consumer retries. Both SQS and Pub/Sub support configurable retry policies, but they should be paired with strict visibility timeouts to prevent duplicate processing. When a message exceeds its retry threshold, route it to a Dead Letter Queue (DLQ) for forensic analysis. Proper DLQ configuration is critical for maintaining pipeline integrity, as detailed in Implementing Dead Letter Queues for Failed Vector Jobs.
Additionally, enforce idempotency at the consumer level. Include a job_id or message_hash in the payload and track processed IDs in a fast key-value store (e.g., DynamoDB, Firestore, or Redis). This prevents duplicate CRS transformations or topology checks if network partitions cause message redelivery.
Production Best Practices
- Validate Before Routing: Run lightweight JSON schema validation (e.g.,
pydanticorjsonschema) at the producer stage. Reject malformed payloads before they enter the queue to avoid consumer-side crashes and DLQ pollution. - Cap Payload Size: SQS and Pub/Sub have strict message size limits (256 KB and 256 KB respectively, though Pub/Sub allows up to 1 MB with configuration). Store large spatial files in object storage and pass only metadata and URIs in the message envelope.
- Monitor Consumer Lag: Track the delta between message publish time and consumer acknowledgment. Sustained lag indicates undersized compute or inefficient routing logic. Set alerts at 70% of your SLA threshold.
- Use FIFO for Strict Ordering: When processing sequential spatial edits (e.g., cadastral boundary updates), use SQS FIFO queues or Pub/Sub with ordering keys. Note that FIFO introduces throughput limits and requires careful deduplication ID management.
- Decouple Routing from Business Logic: Keep routing functions stateless and minimal. Delegate heavy transformations to dedicated workers. This simplifies testing, scaling, and debugging.
Conclusion
Effective SQS and Pub/Sub Queue Routing Strategies transform chaotic geospatial ingestion into predictable, scalable workflows. By leveraging content-based attributes, priority tiers, and spatial sharding, teams can align compute resources with data characteristics while maintaining strict SLAs. Pair these routing patterns with robust error handling, schema validation, and comprehensive monitoring to build pipelines that gracefully handle the inherent complexity of spatial data. As your geospatial footprint expands, revisit routing thresholds and consumer allocations quarterly to ensure cost efficiency and processing velocity remain optimized.