2012-10-08 41 views
15

Tôi đã đọc this article đề cập đến việc lưu trữ 1 triệu khóa trong redis sẽ sử dụng 17GB bộ nhớ. Tuy nhiên khi chuyển sang băm chunking chúng tại 1k mỗi (ví dụ: HSET "mediabucket:1155" "1155315" "939") cho phép họ lưu trữ 1M trong 5GB mà là một khoản tiết kiệm khá lớn.HSET và sử dụng bộ nhớ SET?

Tôi đọc redis memory-optimization nhưng tôi không hoàn toàn hiểu sự khác biệt. Nó nói rằng HGET không hoàn toàn là O (1) nhưng đủ gần và đề cập đến việc sử dụng CPU nhiều hơn khi sử dụng hsets. Tôi không hiểu tại sao sẽ có nhiều sử dụng CPU (chắc chắn thời gian giao dịch cho không gian. Nhưng làm thế nào/những gì?). Nó đề cập đến 'mã hóa' chứ không phải cách mã hóa nó.

Nó cũng chỉ đề cập đến chuỗi nhưng tôi không biết chuỗi chỉ có nghĩa là gì. Nó là lĩnh vực băm? Nó có nghĩa là trường băm? Tôi không thấy bất cứ điều gì về nó trong HSET. Chính xác những gì sẽ được mã hóa và tại sao mã hóa sẽ hiệu quả hơn sau đó sử dụng một SET?

Làm cách nào có thể HSET "mediabucket:1155" "1155315" "939" hiệu quả hơn sau đó SET "mediabucket:1155315" "939"? Có ít dữ liệu trong SET (1155315 một d 1155 được sử dụng thay vì sau đó 1155315). Cá nhân tôi sẽ cố gắng sử dụng các phím nhị phân tuy nhiên tôi không nghĩ rằng đó đã làm với lý do tại sao HSETs là hiệu quả hơn.

EDIT:

Chữ thập được đăng trên mailing list redis-db cũng như: https://groups.google.com/d/topic/redis-db/90K3UqciAx0/discussion

Trả lời

19

đối tượng băm nhỏ được mã hóa như ziplists tùy thuộc vào giá trị của băm-max-ziplist-mục và băm-Max- thông số ziplist-value. Đây là tuần tự hóa dữ liệu đơn giản.

Một ziplist được định nghĩa như sau (trích từ mã nguồn Redis):

/* The ziplist is a specially encoded dually linked list that is designed 
* to be very memory efficient. It stores both strings and integer values, 
* where integers are encoded as actual integers instead of a series of 
* characters. It allows push and pop operations on either side of the list 
* in O(1) time. However, because every operation requires a reallocation of 
* the memory used by the ziplist, the actual complexity is related to the 
* amount of memory used by the ziplist. 
* 
* ---------------------------------------------------------------------------- 
* 
* ZIPLIST OVERALL LAYOUT: 
* The general layout of the ziplist is as follows: 
* <zlbytes><zltail><zllen><entry><entry><zlend> 
* 
* <zlbytes> is an unsigned integer to hold the number of bytes that the 
* ziplist occupies. This value needs to be stored to be able to resize the 
* entire structure without the need to traverse it first. 
* 
* <zltail> is the offset to the last entry in the list. This allows a pop 
* operation on the far side of the list without the need for full traversal. 
* 
* <zllen> is the number of entries.When this value is larger than 2**16-2, 
* we need to traverse the entire list to know how many items it holds. 
* 
* <zlend> is a single byte special value, equal to 255, which indicates the 
* end of the list. 
* 
* ZIPLIST ENTRIES: 
* Every entry in the ziplist is prefixed by a header that contains two pieces 
* of information. First, the length of the previous entry is stored to be 
* able to traverse the list from back to front. Second, the encoding with an 
* optional string length of the entry itself is stored. 
* 
* The length of the previous entry is encoded in the following way: 
* If this length is smaller than 254 bytes, it will only consume a single 
* byte that takes the length as value. When the length is greater than or 
* equal to 254, it will consume 5 bytes. The first byte is set to 254 to 
* indicate a larger value is following. The remaining 4 bytes take the 
* length of the previous entry as value. 
* 
* The other header field of the entry itself depends on the contents of the 
* entry. When the entry is a string, the first 2 bits of this header will hold 
* the type of encoding used to store the length of the string, followed by the 
* actual length of the string. When the entry is an integer the first 2 bits 
* are both set to 1. The following 2 bits are used to specify what kind of 
* integer will be stored after this header. An overview of the different 
* types and encodings is as follows: 
* 
* |00pppppp| - 1 byte 
*  String value with length less than or equal to 63 bytes (6 bits). 
* |01pppppp|qqqqqqqq| - 2 bytes 
*  String value with length less than or equal to 16383 bytes (14 bits). 
* |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes 
*  String value with length greater than or equal to 16384 bytes. 
* |11000000| - 1 byte 
*  Integer encoded as int16_t (2 bytes). 
* |11010000| - 1 byte 
*  Integer encoded as int32_t (4 bytes). 
* |11100000| - 1 byte 
*  Integer encoded as int64_t (8 bytes). 
* |11110000| - 1 byte 
*  Integer encoded as 24 bit signed (3 bytes). 
* |11111110| - 1 byte 
*  Integer encoded as 8 bit signed (1 byte). 
* |1111xxxx| - (with xxxx between 0000 and 1101) immediate 4 bit integer. 
*  Unsigned integer from 0 to 12. The encoded value is actually from 
*  1 to 13 because 0000 and 1111 can not be used, so 1 should be 
*  subtracted from the encoded 4 bit value to obtain the right value. 
* |11111111| - End of ziplist. 
* 
* All the integers are represented in little endian byte order. 
*/ 

Mỗi mục từ đối tượng băm được biểu diễn dưới dạng một vài chìa khóa/giá trị trong ziplist (2 mục kế tiếp). Cả khóa và giá trị đều có thể được lưu trữ dưới dạng chuỗi đơn hoặc số nguyên. Định dạng này là nhỏ gọn hơn trong bộ nhớ vì nó tiết kiệm rất nhiều con trỏ (8 byte mỗi) được yêu cầu để thực hiện một cấu trúc dữ liệu động (như một hashtable thực).

Nhược điểm là các hoạt động HSET/HGET thực sự là O (N) khi được áp dụng trên ziplist. Đó là lý do tại sao ziplist phải được giữ nhỏ. Khi dữ liệu ziplist phù hợp với bộ nhớ cache CPU L1, các thuật toán tương ứng đủ nhanh mặc dù phức tạp tuyến tính của chúng.

Bạn có thể muốn tham khảo các link sau để biết thêm thông tin:

Redis 10x more memory usage than data

Redis Data Structure Space Requirements

Những câu trả lời tham khảo cấu trúc dữ liệu khác (như bộ, danh sách, hoặc bộ được sắp xếp), nhưng nó chính xác là cùng một khái niệm.

+3

Giải thích hay. –

Các vấn đề liên quan