Chung-Yi
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # What is Amazon SQS(Simple Queue Service) > 什麼是SQS?它是一個message queue([訊息佇列](https://medium.com/@b98901052/%E4%BB%80%E9%BA%BC%E6%98%AFmessage-queue-%E5%84%AA%E9%BB%9E%E5%8F%8A%E4%BD%BF%E7%94%A8%E5%A0%B4%E6%99%AF-23d6a39cc4f2)),當producer產生資料時傳送資訊給consumer過程中,用來存放message的緩衝區。像是fb通知,像這種不是即時處理的訊息,就可以放在queue,然後讓其他服務處理。像有些情況會有多個worker需要處理,這時候queue就扮演著重要的角色,降低worker的耦合性,將過程變得比較簡單,也比較好維護。有時候服務量比較大時或是系統異常時會造成塞車的情況,所以放在queue可以避免遺失,等到系統回復時再慢慢處理。 ![](https://i.imgur.com/b3WqNQi.png) ![](https://i.imgur.com/QMvruxp.png) Standard Queue VS FIFO Queue --- | Standard Queue | FIFO Queue | | ------------------------------------------------------- |:---------- | | 1. 每秒訊息的傳輸量(TPS)幾乎是沒有限制 | 1. 高傳輸量,每秒訊息的傳輸量(TPS)是3000 | | 2. 每個訊息至少傳送一次,但有時候同一個訊息不只傳送一次 | 2. 訊息只處理一次,不會重複處理 | | 3. 盡可能維持訊息的順序,但一則 | 3. 按照 first in first out 傳送與接收 | SQS架構 --- SQS 的架構如下圖,其主要由三個部分所組成,分別為分散系統、server上的queue及message。SQS會將message複製到很多台queue server,但並不是每台都是儲存所有的message,pull時可能那台server 只有message A、C 或 B、C、D 等,所以自己的server 如果有許多台,就可能會拿到不同或重複的messages 。 ![](https://i.imgur.com/hE1M7HN.png) SQS訊息週期 --- SQS的訊息週期如下圖所示: ![](https://i.imgur.com/UDpyvuD.png) 1. 需要傳送訊息的producer將選取Amazon SQS佇列,並使用SendMessage將新訊息A傳送到佇列 2. consumer開始接收處理queue的訊息,並使用ReceiveMessage然後傳回此訊息,其中當訊息被接收後,仍然留在queue不會馬上消失,這是由於Amazon SQS是分散式系統,不保證consumer會實際接收到訊息(例如因為連線問題或consumer應用程式的問題而未接收到)。因此consumer必須在接收和處理訊息後,才可將訊息從佇列上刪除 3. 但是如果訊息持續留在queue,可能會造成其他consuner拿到同一筆message,為了避免此強況發生,Amazon SQS會設定visibility timeout,也就是說在visibility timeout這段時間內,其他consumer不能重複處理同一筆message,除非visibility timeout已過,否則不會由其他ReceiveMessage傳回 4. 如果consumer順利接收訊息,呼叫DeleteMessage從queue刪除message,這樣可以避免在visibility timeout過後,其他consumer處理同一筆message Long Polling VS Short Polling --- 其中consumer主要是透過兩種方式來接收訊息,分別為long polling及short polling。 * short polling: 如下圖所示,consumer可以很快的從queue server接收到message,但也因為時間太短,所以沒有辦法將每個queue的message都拿到,是基於weghted random的方法,找尋部分queue server裡的message並且傳回給consumer,缺點是太過於頻繁使用SQS,是成本偏高。 ![](https://i.imgur.com/tFRkUPV.png) * long polling: consumer把接收message的時間拉長,在這段時間內如果queue裡沒有其他message,則接收的request會暫時block,等到queue有message時才解除block,這樣可以避免過於頻繁使用SQS,減少使用成本。 程式碼實作 --- 步驟分別為create queue, list queue, get queue url, send message, receive message, delete message, delete queue ``` import ( "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" ) const ( CredPath = ".aws/credentials" CredProfile = "op" Queueurl = "https://sqs.us-west-2.amazonaws.com/478205036267/queue1s" ) ``` ``` sess := session.New(&aws.Config{ Region: aws.String("us-west-2"), Credentials: credentials.NewSharedCredentials(CredPath, CredProfile), }) svc := sqs.New(sess) ``` 開始建立一個queue,其中參數DelaySeconds是指,consumer在這段時間內是看不到任何傳到這個queue裡面的message,MessageRetentionPeriod是指訊息保留的時間,成功建立後可以拿到QueueUrl。 ``` result, err := svc.CreateQueue(&sqs.CreateQueueInput{ QueueName: aws.String("queue1"), Attributes: map[string]*string{ "DelaySeconds": aws.String("60"), "MessageRetentionPeriod": aws.String("86400"), // "FifoQuesue": aws.String("true"), }, }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", *result.QueueUrl) ``` ![](https://i.imgur.com/EUyk7EJ.png) 列出所有建立的queue,其中ListQueues所帶的參數如果是nil,代表列出所有的queue,最多能列出1000個。 ``` results, err := svc.ListQueues(nil) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success") for i, urls := range results.QueueUrls { if urls == nil { continue } fmt.Printf("%d: %s\n", i, *urls) } ``` ![](https://i.imgur.com/2ISipJ7.png) 但是如果要列出特定的queue,可以使用指定queue名稱字首的方式。 ``` queueName := "queue1.fi" results, err := svc.ListQueues(&sqs.ListQueuesInput{QueueNamePrefix: &queueName}) //svc.ListQueues(nil) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success") for i, urls := range results.QueueUrls { if urls == nil { continue } fmt.Printf("%d: %s\n", i, *urls) } ``` ![](https://i.imgur.com/LdfuHmA.png) 給定queue的名稱就能拿到QueueUrl ``` url, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: aws.String("queue1s"), }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", *url.QueueUrl) ``` ![](https://i.imgur.com/IXBZYcc.png) 開始傳送message到指定的queue,並且輸入要傳送的message,傳送成功便可拿到一組MD5及MessageId ``` send_message := &sqs.SendMessageInput{ MessageBody: aws.String("Hi, Andy"), QueueUrl: aws.String(Queueurl), DelaySeconds: aws.Int64(3), } Sendresp, err := svc.SendMessage(send_message) if err != nil { panic(err) } fmt.Printf("Send message \n%v \n\n", Sendresp) ``` ![](https://i.imgur.com/vuUF3RG.png) 成功傳送之後,就可以檢查是否可以接收到此message,參數MaxNumberOfMessages是指設定一次最多message數量, WaitTimeSeconds是設定一個long polling的時間。 ``` receive_message := &sqs.ReceiveMessageInput{ QueueUrl: aws.String(Queueurl), MaxNumberOfMessages: aws.Int64(3), //設定一次最多message數量 VisibilityTimeout: aws.Int64(10), //如果此message沒被刪除,必須等10秒後才能拿 WaitTimeSeconds: aws.Int64(20), //long polling 方式取,會建立一條長連線並且等在那邊,直到 SQS 收到新 message 回傳給這條連線才中斷 } Receivwresp, err := svc.ReceiveMessage(receive_message) if err != nil { panic(err) } fmt.Printf("Send message \n%v \n\n", Receivwresp) ``` ![](https://i.imgur.com/MiCB8Kp.png) 在接收完message之後,建議能夠將此messag刪除,避免其他consumer重複拿到。 ``` for _, message := range Receivwresp.Messages { delete_message := &sqs.DeleteMessageInput{ QueueUrl: aws.String(Queueurl), ReceiptHandle: message.ReceiptHandle, //用來提供刪除message的憑證 } _, err := svc.DeleteMessage(delete_message) if err != nil { panic(err) } fmt.Printf("Delete message \nMessage ID: %s has beed deleted.\n\n", *message.MessageId) } ``` ![](https://i.imgur.com/U7kBZVJ.png) 參考文獻 --- 1. https://docs.aws.amazon.com/zh_tw/AWSSimpleQueueService/latest/SQSDeveloperGuide/standard-queues.html#standard-queues-at-least-once-delivery 2. https://ithelp.ithome.com.tw/articles/10194654 3. https://ithelp.ithome.com.tw/articles/10219553 4. http://pragmaticnotes.com/2017/11/20/amazon-sqs-long-polling-versus-short-polling/

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully