問題描述
我正在使用 redis 為我的 Web 應(yīng)用程序?qū)崿F(xiàn)社交流和通知系統(tǒng).我是 redis 新手,我對哈希及其效率有一些疑問.
I'm implementing a social stream and a notification system for my web application by using redis. I'm new to redis and I have some doubts about hashes and their efficiency.
我讀過這篇很棒的 Instagram 帖子我計劃實施他們類似的解決方案以減少存儲空間.
I've read this awesome Instagram post and I planned to implement their similar solution for minimal storage.
正如他們的博客中提到的,他們確實喜歡這個
As mentioned in their blog, they did like this
為了利用散列類型,我們將所有媒體 ID 分桶到 1000 個桶中(我們只取 ID,除以 1000 并丟棄余數(shù)).這決定了我們落入哪個鍵;接下來,在位于該鍵的散列中,媒體 ID 是散列內(nèi)的查找鍵,用戶 ID 是值.舉個例子,給定 Media ID 為 1155315,這意味著它落入桶 1155 (1155315/1000 = 1155):
To take advantage of the hash type, we bucket all our Media IDs into buckets of 1000 (we just take the ID, divide by 1000 and discard the remainder). That determines which key we fall into; next, within the hash that lives at that key, the Media ID is the lookup key within the hash, and the user ID is the value. An example, given a Media ID of 1155315, which means it falls into bucket 1155 (1155315 / 1000 = 1155):
HSET "mediabucket:1155" "1155315" "939"
HGET "mediabucket:1155" "1155315"
> "939"
因此,他們沒有將 1000 個單獨的鍵存儲在 一個具有數(shù)千個查找鍵的哈希中.我的疑問是為什么我們不能將查找鍵值增加到更大.
So Instead of having 1000 seperate keys they are storing it in one hash with thousand lookup keys. And my doubt is why can't we increase the lookup key values to even more larger.
例如: 媒體 ID 1155315 除以 10000 將落入 mediabucket:115
甚至更大.
他們?yōu)槭裁匆褂靡粋€具有 1000 個查找鍵的哈希桶.為什么他們不能有一個具有 100000 個查找鍵的哈希桶.這與效率有關(guān)嗎?
Why are they settling with one hash bucket with 1000 lookup keys. Why can't they have one hash bucket with 100000 lookup keys. Is that related to efficiency?
我需要您的建議,以便在我的 Web 應(yīng)用程序中實施有效的方法.
I need your suggestion for implementing the efficient method in my web application.
附:請!不要說stackoverflow不是用來提建議的,我不知道去哪里尋求幫助.
P.S. Please! don't say that stackoverflow is not for asking suggestions and I don't know where to find help.
謝謝!
推薦答案
是的,和效率有關(guān).
我們向總是樂于助人的 Redis 核心開發(fā)人員之一 Pieter Noordhuis 征求意見,他建議我們使用 Redis 哈希.Redis 中的哈希是可以非常有效地在內(nèi)存中編碼的字典;Redis 設(shè)置hash-zipmap-max-entries"配置哈??梢跃哂械淖畲髼l目數(shù),同時仍然可以有效編碼.我們發(fā)現(xiàn)這個設(shè)置最好在 1000 左右;更高,HSET 命令將導(dǎo)致明顯的 CPU 活動.更多詳細(xì)信息,您可以查看 zipmap 源文件.
We asked the always-helpful Pieter Noordhuis, one of Redis’ core developers, for input, and he suggested we use Redis hashes. Hashes in Redis are dictionaries that are can be encoded in memory very efficiently; the Redis setting ‘hash-zipmap-max-entries’ configures the maximum number of entries a hash can have while still being encoded efficiently. We found this setting was best around 1000; any higher and the HSET commands would cause noticeable CPU activity. For more details, you can check out the zipmap source file.
小散列以一種特殊的方式(zipmaps)進行編碼,即節(jié)省內(nèi)存,但操作 O(N) 而不是 O(1).因此,使用一個包含 100k 字段的 zipmap 而不是 100 個包含 1k 字段的 zipmap,您不會獲得內(nèi)存優(yōu)勢,但您的所有操作都會慢 100 倍.
Small hashes are encoded in a special way (zipmaps), that is memory efficient, but makes operations O(N) instead of O(1). So, with one zipmap with 100k fields instead of 100 zipmaps with 1k fields you gain no memory benefits, but all your operations get 100 times slower.
這篇關(guān)于redis - 使用哈希的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!