Memcached Slab Allocation

Memcached 會將預先內存,使用 Slab class 的方式來分類,每一個 Slab class 裡面,使用固定大小的 Chunk 來存取資料。Memcached 會預先跟作業系統申請,一定數量的 Page (實際的內存)來存放大小相同 Chunk 。當 Memcached client 去寫入一筆資料時,Memcached 會根據該筆資料的大小,去找適合的 Slab class 來去存放。另外每一個 Slab Class 會去跟作業系統申請 memory pages 來存放資料,而分配在該 Slab Class 的 memory pages 沒有使用,也不會主動歸還,而內存管理是依每一個 Slab Class來處理,所以當內存不足時,Memcached 跟作業系統申請額外的 memory pages來存放數據,即使其他 Slab class 有沒有使用的 memory pages,memcahed 仍然不會拿來使用。不過當您將參數 Parameter: slab_automove/slab_reassign 開啟時,就可以解決這個問題。

Jerry’s Notes
What’s next?
9 min readMar 22, 2022

--

Slab Allocation 運作方式

Memcached 會參考 Parameter: chunk_size 這個數值,來決定第一個 Slab Class 內每一個固定 Chunk 的大小,再參考 Parameter: chunk_size_growth_factor 這個數值,來決定下一個 Slab class 的大小。

■ 每個slab裡面包含若干個page,page的默認大小是1M,如果slab大小100M,就包含100個page。

■每個 slab class 由若干個大小相等的chunk組成。

■ 每個chunk中都儲存了一個item結構體、一對key和value。

■ chunk是數據的實際存放單位,每個slab裡面的chunk大小相同。

範例說明

當 slab class #1 = 96 Bytes 時,slab class #2 = 96 * 1.25 = 120 Bytes,以此類推。

slab class #1 = 96 Bytes
slab class #2 = 96 * 1.25 =
120 Bytes
….

  • Parameter: chunk_size
  • Parameter: chunk_size_growth_factor = 1.25

當 Memcached client 寫入 80 bytes 大小的數據時,就會寫到 slab class #1,若數據大小為 100 Bytes 時,就會寫 slab class #2。

key 80 bytes => put slab class #1
key 1~96 bytes => put slab class #1

key 100 Bytes => put slab class #2

> stats slabs

缺點:

由於分配的是特定長度的內存,因此無法有效利用,這個問題就是,由於分配的是特定長度的內存,因此無法有效利用分 配的內存。

優點:

內存分配機制簡單,有助於提升存取效率,減少在分配內存上的時間。

Slab model

The primary goal of the slabs subsystem in memcached was to eliminate memory fragmentation issues totally by using fixed-size memory chunks coming from a few predetermined size classes.

主要參數 Parameter

■ chunk_size: The minimum amount of space allocated for the smallest key. Default size is 48 bytes. key+value+flags 在 ElastiCache Memcached 中 Slab Class # 預設為 96 bytes。

■ chunk_size_growth_factor: The growth factor that controls the size of each successive chunk, i.e., chunk[n+1] = chunk[n] x multiplier. The default value is 1.25

slab_chunk_max: Specifies the maximum size of a slab. Setting smaller slab size uses memory more efficiently. Items larger than slab_chunk_max are split over multiple slabs. (Default: 524288 = 512KB, up to 1MB)

■ max_item_size: The size of the largest item that can be stored. Default value is 1 MB (1048576 bytes), currently we support the largest item is 128MB 這個設定是指單一鍵值(String)最大的大小值,預設為 1MB,而 memcached 支援最大為 128 MB,但因為 memcached 是內存型資料庫,並不建議存放大鍵值,而是建立將數據在 memcache Client 端做適當的拆分,以提高使用上的效率。

Q: How to check each chunk size in Memcached? (stats sizes)

Warning: In versions prior to 1.4.27 this command causes the cache server to lock while it iterates the items. 1.4.27 and greater are safe2. stats slabs

內存回收方式 — Expiration

LRU算法不是針對全局的,是針對slab class的。

當數據容量用完MemCached分配的內存後,就會基於LRU(Least Recently Used)算法清理失效的緩存數據(放入數據時可設置失效時間),或者清理最近最少使用的緩存數據,然後放入新的數據。

在LRU中,MemCached使用的是一種Lazy expiration。

Expiration策略,自己不會監控存入的key/vlue對是否過期,而是在獲取key值時查看記錄的時間戳,檢查key/value對空間是否過期,這樣可減輕服務器的負載。

Q: 如果特定 Slab Class 報錯,內存不足時,但其他 Slab Class 仍有可用 pages 時,該如何處理?

由於 Memcached 會預先分配多個大小不同的 Slab Class,而每一個 Slab Class 會去跟作業系統申請 memory pages 來存放資料,而分配在該 Slab Class 的 memory pages 沒有使用,也不會主動歸還,而內存管理是依每一個 Slab Class來處理,所以當內存不足時,Memcached 跟作業系統申請額外的 memory pages來存放數據,即使其他 Slab class 有沒有使用的 memory pages,memcahed 仍然不會拿來使用。不過當您將參數 Parameter: slab_automove/slab_reassign 開啟時,就可以解決這個問題。

slab_reassign

slab_reassign: Enable or disable slab reassignment. If this parameter is set to 1, you can use the “slabs reassign” command to manually reassign memory. (Default: 0)

Changes Take Effect: At launch

這個功能是一個手動操作的觸發的,命令如下:

$ echo ‘slabs reassign 3 5’ | nc localhost 11211;

■ Slab Automove — Auto move

slab_automove: Adjusts the slab automove algorithm: If this parameter is set to 0 (zero), the automove algorithm is disabled. If it is set to 1, ElastiCache takes a slow, conservative approach to automatically moving slabs. If it is set to 2, ElastiCache aggressively moves slabs whenever there is an eviction. (This mode is not recommended except for testing purposes.) (Default: 0)

Changes Take Effect: After restart

■當內存不足時會自動移動page,平時不會移動。自動調整 slab class 的大小。

--

--

Jerry’s Notes
What’s next?

An cloud support engineer focus on troubleshooting with customer reported issue ,and cloud solution architecture.