使用 SNS 实现扇出模式
学习使用 Amazon SNS 实现扇出消息模式,将一个事件并行发送给多个相互独立的 Lambda 消费者。
使用 SNS 实现扇出模式 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Fan-Out?
Fan-out means a single event is delivered to many subscribers at once, each processing it independently and in parallel.
Why Not Call Each Directly?
Calling each downstream function from your producer tightly couples them. Adding a consumer means changing the producer. Fan-out decouples this.
SNS as the Hub
Amazon SNS is a pub/sub topic. Publishers send a message once; SNS delivers a copy to every subscriber, including multiple Lambdas.
Publishing a Message
The producer publishes to the topic ARN. It does not know or care who is subscribed.
import boto3, json
sns = boto3.client('sns')
sns.publish(
TopicArn='arn:aws:sns:us-east-1:123:orders',
Message=json.dumps({'orderId': 42})
)The SNS Event Shape
Each subscribed Lambda receives the message wrapped in an SNS records envelope.
{
"Records": [{
"Sns": {
"Message": "{\"orderId\": 42}",
"TopicArn": "arn:aws:sns:us-east-1:123:orders"
}
}]
}A Consumer Handler
Each consumer parses the message from the record and does its own job: one emails a receipt, another updates inventory.
import json
def handler(event, context):
for record in event['Records']:
msg = json.loads(record['Sns']['Message'])
print('Processing order', msg['orderId'])Fan-Out plus SQS
For reliability, subscribe SQS queues to the topic and let Lambda poll the queues. This buffers spikes and gives each consumer its own retry and DLQ.
Message Filtering
SNS filter policies let a subscriber receive only messages matching attributes, so not every consumer processes every event.
{
"eventType": ["order.created"]
}SNS vs EventBridge
SNS is simple, high-throughput pub/sub. EventBridge adds richer routing, schemas, and many SaaS sources. Choose SNS for raw fan-out, EventBridge for complex routing.
Ordering Caveat
Standard SNS topics do not guarantee order or exactly-once delivery. Use SNS FIFO topics when ordering and deduplication matter.
Idempotent Consumers
Because a message may be delivered more than once, each consumer should be idempotent so duplicates do not cause double effects.
Quick Check
Test your fan-out knowledge.
Recap
You learned the fan-out pattern: publish once to SNS, deliver to many parallel consumers, add SQS for durability, filter messages, and keep consumers idempotent.
用 AI 导师学习 Serverless AWS Lambda Development — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「使用 SNS 实现扇出模式」课时是免费的吗?
是的 — 「使用 SNS 实现扇出模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「使用 SNS 实现扇出模式」这节课中我会学到什么?
学习使用 Amazon SNS 实现扇出消息模式,将一个事件并行发送给多个相互独立的 Lambda 消费者。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless AWS Lambda Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 SNS 实现扇出模式」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?
能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。