Kai Chen
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 六角鼠年鐵人賽 Week 19 - Redis - 快取伺服器介紹 ==大家好,我是 "為了拿到金角獎盃而努力著" 的文毅青年 - Kai== ## 劇中佳句 3 idiots :::info Chase Excellence,Sucess will follow. ::: ## 主題 作為 MessageQueue (簡稱MQ) 的第一個主題,果然還是想先學學 Redis,這一款號稱輕量、極高效率聞名的快取伺服器,也是入門 MQ 世界的好標的。 但是... **Redis 並不是 MQ!** **Redis 並不是 MQ!** **Redis 並不是 MQ!** 很重要,強調三次! **Redis** 是一個 **分散式、支援持久化、使用 key-value 做資料處理的快取伺服器**,或者應該用最熱門的詞彙來說明更貼切 -- **NoSQL** 不懂 NoSQL 沒關係,等等下方會有一個簡單的介紹與比較。 總之,Redis 並非是專門做 MQ 的套件,但是它可以支援 MQ 的功能,也因為傳聞中它好學,Kai 才會當作 MQ 的第一個目標。 ## SQL & NoSQL ### SQL 一般常見的 MySQL, Oracle, SQL 都是傳統關聯式資料庫(RDBMS),顧名思義其資料架構為由資料庫包覆資料表,表與表之間生成關聯型態以此鍵結資料關係的架構。 在這之中,資料庫與資料表的定義都是預先設置的,這表示資料存儲須要事先格式化過,才能確保資料的一致,從而能夠統整出碩大且工整的數據表。 #### RDBMS 優點 - 結構化查詢語言 (就是 SQL) - ACID 一應具全 (原子性,一致性,隔離性,持久性) - 技術十分成熟,社群資源非常多 #### RDBMS 缺點 - 縱向發展,對於維持效能的代價過於龐大 > 雖然資料表可建立 index,但資料量越多還是會造成查詢效能上的低落 - 橫向發展的不如 NoSQL 方便 - 動態資料的存儲非常困難 ### NoSQL NoSQL (Not only SQL) 剛好相反,它不在乎資料內的物件型態是否一致,也不在乎資料與資料之間的關聯。你可以在一筆資料中增添五個物件,也可以指放入一個數值,這些都是被允許的事情。 與 RDBMS 相反的事實是,NoSQL 可以說更貼近真實資料的型態。 對於開發人員來說,作業上須要挑整重心在解析資料上,而減少部分存儲方面的工作。 #### NoSQL 優點 - 少量資源便可拓展資料中心,達到高度的橫向發展 - 支援動態資料,非常適合處理非結構化的巨量資料 #### NoSQL 缺點 - 缺乏標準化的處理方式,無法一套SQL打天下 - 僅能在 CAP 中三選二 (一致性,可用性,分區容忍度) - 缺乏效能測試和分析工具 - 技術過於新穎,社群資源難以找尋 然而,天秤的兩端總是平衡的,並沒有哪一種方式絕對好或壞,完全要端看業務的需求而定。 新創商業用 NoSQL 來做沒有問題。 金融保險就非得使用嚴謹的 RDBMS 才能符合規範。 以下是列出在 [guru99 所寫的表格](https://www.guru99.com/sql-vs-nosql.html) Kai 覺得該位大神整理的很棒,在這也把它翻譯成中文,讓大家更好閱讀,中間可能穿插了部分個人的領悟和解讀,若對於內容有所疑慮不妨可以直接查看原文。 ### SQL V.S. NoSQL | | SQL | NoSQL | | ---- | ---- | ---- | | **定義** |通常指 RDBMS,也稱傳統關聯式資料庫|非關聯式資料庫,或稱分布式資料庫| | **設計** |使用結構化查詢語言做 OLAP(線上分析處理)|各個不同的軟體皆是滿足特定需求而打造的,需要視情況做使用| | **使用語言** | SQL | 依照軟體不同 | | **類型** | 基於資料表 | 基於 Key-Value | | **架構** | 預定義架構 | 動態架構 | | **擴展能力** | 可**縱向**擴展 (花費為**指數線性成長**) | 可**橫向**擴展 (花費為**常數線性成長**) | | **常見軟體** | MySQL, Oracle, PostgresSQL, MS-SQL | MongoDB, Redis, , Neo4j, Cassandra, Hbase| | **適合對象** | 時常需要高度複雜性查詢的業務 | 不適合複雜查詢 (適合快速、多量、簡單的查詢結果)| | **變化** | 一種類型、多種變化 | 許多不同類型: Key-Value, 文檔, 圖型資料庫等 | | **發展歷程** | 1970年起,主要尋求處理平面文檔的存儲的方式 | 2000年後,主要尋求補足SQL的缺點與高拓展性 | | **開源程度** | 開源與鎖源的軟體都有 | 幾乎開源 | | **軟體一致性** | SQL 軟體大多數相似 | 依照不同軟體有不同資料規範存儲的要求 | | **適合的使用情景** | 需要解決 ACID 問題 | 需要解決資料可用性問題 | | **重點目標** | 資料正確性比速度更重要時 | 速度比資料正確性更重要時 | | **最佳選擇** | 當需要處理動態Query時 | 當需要根據變化進行拓展時 | | **硬體選擇** | 專用的特規硬體 (Oracle Exadata等) | 一般硬體 | | **網路選擇** | 高速網路 (Infiniband、Fabric Path等) | 一般網路 | | **儲存選擇** | 高速儲存硬體設備 (SAN、RAID等) | 一般儲存硬體設備 (一般HDDs、JBOD) | | **優勢** | 跨平台、安全、免費(MySQL等) | 容易使用、高性能、彈性 | | **著名使用於商業的公司** | Hootsuite, CircleCI, Gauges | Airbnb, Uber, Kickstarter | | **專業人員平均薪資(美國)** | **$84,328**美金/年 (約**253**萬) | **$72,174**美金/年 (約**216萬5千**) | ## Redis 講這麼多終於要進入正題... **Redis** 是由 **Salvatore Sanfilippo** 使用 C 語言開發的 NoSQL,基於當時對 MySQL 效能低落的無奈與傳統 RDBMS 的使用束縛,**Salvatore Sanfilippo** 花了一年時間親手打造的了 **Redis**。 > 工程師的夢想: 看不順眼的事物,就自己建一個 (? 在 2009 年釋出第一版後,Redis 並沒有進行鎖源,而是開源。 這讓想擴充功能的工程師可以協助參與開發工作,因此 Redis 獲得了程式界廣大的回響,一直到 2020 年 Redis 已經發布到了版本 6.0,達到了面相多種不同語言的環境都能提供完整功能與服務的境界。 *來自官方的圖片,可以看到 Redis 支援了許多不同類型的程式語言* ![](https://i.imgur.com/sxvyv5S.png) ## 優勢與劣勢 ### 高效能 所有存放進 Redis 的資料,都會被放置在 Redis 自己伺服器的主記憶體位置,這意味著資料並不需要進出磁碟,而 Redis 簡單的儲存結構也讓 CPU 能使用較少的資源取得同樣的結果。 這樣的結果就是,通常操作都可以在 **一毫秒** 內完成。 ### 資料結構 Redis 提供了多種常見的資料結構,如: String, Hash, List, Set, Sorted Set 等,最高可以保存單筆達 512 MB 的資料進入主記憶體中。 ### 多功能 Redis 除了作為 NoSQL 形式的 DB 外,另外還有 Pub/Sub, Transactions 等好用的功能,能夠做到 MessageQueue、串流等業務 ### 複寫 和 持久化 作為 NoSQL 一員的 Redis,也相同的使用主從式架構,橫向發展的方式讓其可以擴充許多從屬伺服器,增加讀取效能的同時也附帶將資料保持在記憶體空間中更久。 Redis 也支援非同步的復寫功能和時間點快照功能,兩者都可以幫助 Redis 發生問題時快速恢復資料 - **非同步復寫功能** - 在主伺服器發生問題,可以利用從屬伺服器將資料救回,避免憾事發生。 - **時間點快照功能** - 將資料複製到磁碟,建立 Append Only File (AOF),並將每一次的資料變更都寫入磁碟中(可以設定為每一秒作一次),直到下一次資料複製到磁碟蓋過去後,重新刷新資料變更的紀錄。 ### 支援多語言 最一開始的介紹就有提到了,Redis 發展至今目前支援共 **51** 種語言,幾乎可以說是所有軟韌體工程師都可以使用的一個軟體。 ## 資料結構 Redis 作為一個 **快取伺服器**,理所當然地會有它需要去架構和連線的方式,而它儲存資料的方式也是我們常見的 Key-Value 形式。 - **String** - **Hash** - **List** - **Set** - **Sorted Set** ## 功能 - **Pub/Sub (Publish/Subscribe)** - Redis 提供針對一個 Key 進行 Pub/Sub 設定,意即當該 Key 有新訊息進行 Pub 時,所有 Sub 的客戶端都會收到新的訊息。 - 常見於**即時聊天**、**社群平台**等應用。 - **Transactions** - Redis 是少數能夠提供類似於 SQL ACID 性質 Transactions 的軟體,並有提供 WATCH 命令得以監控執行期間資料是否被異動,若被異動,則整個 Transaction 會取消。 - 此外 Redis 並不支援 Roll Back 功能,這是因其面向產品而生的軟體,因此極其單純的僅是處理送入的指令,而這種錯誤通常在開發環節就 "應該" 要被修正,因此 Redis 並不提供任何過於複雜的機制,此包含了 Roll Back 功能。 ## 服務應用 - **快取** - 高效能的記憶體快取方式讓 Redis 減少I/O磁碟的延遲,搭配主從式可擴展架構,讓 Redis 在這項服務應用上幾乎是頂標的存在 - **工作階段管理** - 很多服務都有儲存暫時更新資料內容的功能,Redis 透過複寫和時間點快照的方式,可支援大量未確定的工作階段內容,從而達到線上訊息暫存的功能,常應用在許多遊戲、電商、社群平台上。 - 排行榜 - 許多遊戲或是電商活動都有推出類似的排行榜活動,靜態的排行榜絕對無法即時反應狀況,而 Redis 吞吐資料的速度非常之快,讓他可以達到近乎即時更新的速度,也讓動態排行榜這功能幾乎離不開使用 Redis。 - **流量管制、速率限制** - Redis 可當作測量的工具,讓網站或線上服務可以視流量狀況作自動化的調整,避免因突發性的爆量而導致 Shut Down 狀況發生。 - 也有部分功能是拿來作搶票機制,例如航空或傳播媒體的活動等。 - **媒體串流** - Redis 提供存放中繼資料的方式,讓媒體可以一次串流到數百萬行動裝置或電腦上 - **佇列** - Redis 資料結構提供 Atomic 操作和封鎖功能,可用於需要可靠的訊息代理程式或循環清單的各種應用程式中。 - **對談訊息** - Redis 提供 Pub/Sub 功能,讓其能夠支援高效能的聊天室、註解串流、社群媒體通訊等功能。 - **ML (Machine Learning)** - Redis 提供快速的記憶體內資料存放區讓您迅速建立、訓練和部署機器學習模型。 - **即時分析** - Redis 可與 Apache Kafka 和 Amazon Kinesis 等串流解決方案搭配使用做為記憶體內資料存放區,以低於一毫秒的延遲導入、處理和分析即時資料。 ## 結語 :::danger 打了好多,還是沒能介紹完整,而且中間還延伸不少話題... 哈 這篇篇幅不夠僅能做概略的介紹,下一篇文章 Kai 將會分享 Redis 的實務操作方式的學習心得 [六角鼠年鐵人賽 Week 20 - Spring Boot - Redis 安裝與設定配置介紹](/HfH8ETwLTJ-nzjuL03-Ivg) ::: 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Redis`,`w3HexSchool`

    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