4.3.4 Subscription Filter Policies

Use Subscription Filter Policies to Optimize Messaging

SNS Message Filtering

Scenario thi: Use Message Filter feature của SNS để enable subscribers to SNS topic receive only a subset of topic messages.

Ví dụ: Insurance quote application

  • Request for insurance → forwarded to backend systems
  • Sales system handles insurance policies
  • Analytics system processes all quote requests

Giải pháp: Use fanout message pattern using SNS and SQS to decouple website from backend systems.

{
  "eventType": ["order_placed"],
  "amount": [{"numeric": [">=", 100]}],
  "store": [{"prefix": "us-"}]
}

Lợi ích:

  • Filter at subscription level
  • Reduce unnecessary Lambda invocations
  • Reduce SQS messages processed
  • No extra cost for filtering
  • Event notifications get to right backend system

Filter Policy Operators

OperatorVí dụ
Exact match["order_placed"]
Prefix[{"prefix": "us-"}]
Numeric[{"numeric": [">=", 100]}]
Anything-but[{"anything-but": ["test"]}]
Exists[{"exists": true}]

Without vs With Filtering

Without: SNS → SQS → Lambda (processes ALL messages, discards 90%)
With:    SNS → Filter → SQS → Lambda (processes only relevant 10%)

Benefits:

  • ✅ Cost savings: Fewer Lambda invocations
  • ✅ Performance: Less unnecessary processing
  • ✅ Simplicity: No filter logic in consumer code
  • ✅ Right messages to right systems

EventBridge Content-Based Filtering

{
  "source": ["my-app"],
  "detail-type": ["OrderCreated"],
  "detail": {
    "amount": [{"numeric": [">", 100]}],
    "region": ["us-east-1", "eu-west-1"]
  }
}
  • Richer filtering than SNS
  • Pattern matching on event content
  • Multiple targets per rule

Real-world Example

Insurance Application:

Quote Request → SNS Topic
                  ├── Filter: {"type": ["new_policy"]} → SQS → Sales Lambda
                  └── Filter: {"type": ["quote"]} → SQS → Analytics Lambda

Exam Tip:

  • SNS filter policies = reduce unnecessary processing at subscription level
  • No extra cost for filtering
  • EventBridge = richer content-based filtering
  • Use fanout pattern (SNS + SQS) to decouple systems
  • Ensure event notifications get to right backend system