brooklin123
    • 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

      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.
      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
    • 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 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

    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.
    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
    # [The Engineering Wisdom Behind Redis’s Single-Threaded Design](https://community.aws/content/2tfQijlXleV5iM6hDkSGkMJlpcd/the-engineering-wisdom-behind-redis-s-single-threaded-design) 文章摘要整理 ## **redis 的設計** Redis 採用單執行緒設計主要基於效能與實現複雜度之間的權衡。其核心架構基於事件循環機制,透過系統級的I/O多路復用技術(如epoll/kqueue)實現大量連接的高效管理。 使用單執行緒模型具有幾項好處: 1. 可忽略了同步機制中的鎖競爭和上下文切換開銷,使得所有操作可以無阻塞地順序執行。 2. 提供了確定性行為。操作以可預測的序列執行,命令嚴格按照從輸入緩衝區處理的順序執行 3. 在記憶體存取層面,單執行緒模型確保了資料結構在操作處理期間保持在快取中,大幅提升了記憶體存取效率。 避免單執行緒的缺點: 無法直接利用多個核心進行命令處理,因此為避免過慢的命令處理導致阻塞,Redis 透過多種機制,如設定命令執行時間閾值、使用 Lua 腳本限制執行時長,以及將持久化等耗時操作轉移到後台執行緒處理。另外,利用水平擴展 ## **多執行緒的隱藏複雜性與局限** 改 Redis 成多執行緒後效能仍有所受限。 1. 平行化的根本限制 (Amdahl’s Law): Amdahl 定律說明了平行化所能帶來的最大加速比受到工作負載中序列部分的限制。即使我們能將工作負載中的一部分平行處理,但總會有無法平行化的部分,這部分將限制整體效能的提升。 對於類似 Redis 的工作負載,即使慷慨估計大約 30% 的操作可以有效平行化。縱使使用 32 個執行緒,理論上最多也只能達到 1.42 倍的加速,這遠遠達不到人們可能預期的線性擴展。 Redis 的操作特別難以有效平行化,原因包括:許多操作會以相互依賴的方式修改資料結構;大多數 Redis 命令在微秒內即可完成,留下很少的時間用於平行化開銷;命令經常修改全局狀態(例如 LRU 資訊或過期時間)。 2. 記憶體牆問題 (The Memory Wall Problem): 現代 CPU 的速度非常快,以至於記憶體存取經常成為資料庫效能的限制因素。這種「記憶體牆」問題意味著,即使增加了更多的執行緒,效能往往也無法提升,因為瓶頸不在於 CPU 處理能力,而在於記憶體頻寬和延遲,因此隨意增加更多執行緒反而可能使情況惡化。 多個執行緒會競爭有限的快取空間。雖然每個核心都有自己的 L1/L2 快取,但執行緒必須共享資料。所有核心共享 L3 快取,這也造成了對有限資源的競爭。緩存失效過程(cacheline ping-ponging),通過 CPU 的緩存一致性協定發生,此過程可能會給作增加數百個週期的延遲。而當工作集大小超過快取容量時,會需更常從主記憶體中獲取資料,這效能會急劇下降。 在 NUMA 系統中,記憶體存取時間取決於記憶體位置與處理器的相對位置。跨 NUMA 節點的記憶體存取可能比本地存取慢 2-3 倍。未能考慮 NUMA 拓撲的多執行緒應用程式在執行緒存取與不同 CPU 插槽相關的記憶體時,可能會遭遇嚴重的效能損失。 3. 複雜性稅 (The Complexity Tax): 多執行緒系統引入的複雜性會隨著執行緒數量和共享資源的增加而指數級增長。 當系統擁有 N 個鎖可能存在 N(N-1)/2 種潛在的死鎖情境。即使只有 5 個鎖,就有 10 種潛在的 deadlock 情境。為防止 deadlock,整個系統需小心地規劃鎖定順序,這變得越來越困難。 競爭條件發生在系統行為取決於事件相對時序的情況下,這將對開發與營運造成影響,因細微的平行錯誤可能會出現,且可能只在特定的條件下才顯現,導致問題難以重現和診斷。對於資料完整性至關重要的資料庫系統,人們難以對多執行緒程式碼的正確性有信心, ## **使用 Multi-Process 而非 Multi-Threading 的優勢** 1. 程序隔離每個 Redis 實例運行在自己的程序中,擁有獨立的記憶體空間,提供了真正的隔離。記憶體損壞被限制在單一程序內。崩潰的影響僅限於單一分片。 2. 線性擴展。透過分散資料到多個獨立的 Redis 程序,Redis Cluster 實現了近乎線性的跨核心和機器擴展。由於每個實例獨立運行,共享開銷極少。 3. 故障隔離。程序邊界提供了執行緒邊界無法提供的真正隔離。即使某些節點(程序)故障,集群也能繼續運行。這在生產環境中特別有價值。 4. NUMA 對齊:每個 Redis 程序可以被釘選 (pin) 到特定的 NUMA 節點上。這確保了記憶體分配盡可能保持本地,最大程度地減少了昂貴的跨插槽通訊和跨 NUMA 流量。 5. 簡化管理與開發:每個實例維持單執行緒模型,簡化的程式設計,這方法也與雲端原生設計完美契合

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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