forked from docs/doc-exports
Reviewed-by: Mützel, Andrea <andrea.muetzel@t-systems.com> Co-authored-by: Chen, Junjie <chenjunjie@huawei.com> Co-committed-by: Chen, Junjie <chenjunjie@huawei.com>
45 lines
6.3 KiB
HTML
45 lines
6.3 KiB
HTML
<a name="dcs-faq-0521005"></a><a name="dcs-faq-0521005"></a>
|
|
|
|
<h1 class="topictitle1">How Do I Avoid Big Keys and Hot Keys?</h1>
|
|
<div id="body0000001380026226"><ul id="dcs-faq-0521005__ul1627391114415"><li id="dcs-faq-0521005__li132743113411"><strong id="dcs-faq-0521005__b197041440153910">Keep the size of Strings within 10 KB</strong> and <strong id="dcs-faq-0521005__b113402035453910">the quantity</strong> of Hashes, Lists, Sets, or Zsets <strong id="dcs-faq-0521005__b159670428253910">within 5000</strong>.</li><li id="dcs-faq-0521005__li1427414112414">When naming keys, use the service name abbreviation as the prefix and do not use special characters such as spaces, line brakes, single or double quotation marks, and other escape characters.</li><li id="dcs-faq-0521005__li9274511114120">Do not rely too much on Redis transactions.</li><li id="dcs-faq-0521005__li0274161164112">The performance of short connections ("connect" in Redis terminology) is poor. Use clients with connection pools.</li><li id="dcs-faq-0521005__li127491184119">Do not enable data persistence if you use Redis just for caching and can tolerate data loss.</li><li id="dcs-faq-0521005__li13952142316363">For details about how to optimize big keys and hot keys, see the following table.</li></ul>
|
|
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dcs-faq-0521005__table51721132203610" frame="border" border="1" rules="all"><thead align="left"><tr id="dcs-faq-0521005__row8173532133619"><th align="left" class="cellrowborder" valign="top" width="22.89%" id="mcps1.3.2.1.3.1.1"><p id="dcs-faq-0521005__p35279463368">Category</p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="77.11%" id="mcps1.3.2.1.3.1.2"><p id="dcs-faq-0521005__p1652710469363">Method</p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="dcs-faq-0521005__row817319327363"><td class="cellrowborder" rowspan="2" valign="top" width="22.89%" headers="mcps1.3.2.1.3.1.1 "><p id="dcs-faq-0521005__p20263527163812">Big key</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="77.11%" headers="mcps1.3.2.1.3.1.2 "><p id="dcs-faq-0521005__p834719309371"><strong id="dcs-faq-0521005__b834703033715">Split big keys.</strong></p>
|
|
<p id="dcs-faq-0521005__p9347153013373">Scenarios:</p>
|
|
<ul id="dcs-faq-0521005__ul12347130153716"><li id="dcs-faq-0521005__li73471030183712"><strong id="dcs-faq-0521005__b1325751825017">If the big key is a String</strong>, you can split it into several key-value pairs and use <strong id="dcs-faq-0521005__b1925771813509">MGET</strong> or a pipeline consisting of multiple <strong id="dcs-faq-0521005__b17257121815015">GET</strong> operations to obtain the values. In this way, the pressure of a single operation can be split. For a cluster instance, key-value pairs can be automatically distributed to multiple shards, reducing the impact on a single shard.</li><li id="dcs-faq-0521005__li43471330143717"><strong id="dcs-faq-0521005__b7713182391514">If the big key contains multiple elements, and the elements must be operated together</strong>, the big key cannot be split. You can remove the big key from Redis and store it on other storage media instead. This scenario should be avoided by design.</li><li id="dcs-faq-0521005__li234703023715"><strong id="dcs-faq-0521005__b1534743016372">If the big key contains multiple elements, and only some elements need to be operated each time</strong>, separate the elements. Take a Hash key as an example. Each time you run the <strong id="dcs-faq-0521005__b52932189953910">HGET</strong> or <strong id="dcs-faq-0521005__b110362695053910">HSET</strong> command, the result of the hash value modulo <em id="dcs-faq-0521005__i196395322153910">N</em> (customized on the client) determines which key the field falls on. This algorithm is similar to that used for calculating slots in Redis Cluster.</li></ul>
|
|
</td>
|
|
</tr>
|
|
<tr id="dcs-faq-0521005__row1517316326368"><td class="cellrowborder" valign="top" headers="mcps1.3.2.1.3.1.1 "><p id="dcs-faq-0521005__p6347163013717"><strong id="dcs-faq-0521005__b5347130123717">Store big keys on other storage media.</strong></p>
|
|
<p id="dcs-faq-0521005__p19347113020373">If a big key cannot be split, it is not suitable to be stored in Redis. You can store it on other storage media, and delete the big key from Redis.</p>
|
|
<div class="caution" id="dcs-faq-0521005__note034833019375"><span class="cautiontitle"> CAUTION: </span><div class="cautionbody"><p id="dcs-faq-0521005__p17348130193710">Do not use the <strong id="dcs-faq-0521005__b66232654753910">DEL</strong> command to delete big keys. Otherwise, Redis may be blocked or even a master/standby switchover may occur.</p>
|
|
</div></div>
|
|
</td>
|
|
</tr>
|
|
<tr id="dcs-faq-0521005__row15174193215363"><td class="cellrowborder" rowspan="2" valign="top" width="22.89%" headers="mcps1.3.2.1.3.1.1 "><p id="dcs-faq-0521005__p166981631143815">Hot key</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="77.11%" headers="mcps1.3.2.1.3.1.2 "><p id="dcs-faq-0521005__p11227567386"><strong id="dcs-faq-0521005__b9227263381">Use the client cache or local cache.</strong></p>
|
|
<p id="dcs-faq-0521005__p1227562383">If you know what keys are frequently used, you can design a two-level cache architecture (client/local cache and remote Redis). Frequently used data is obtained from the local cache first. The local cache and remote cache are updated with data writes at the same time. In this way, the read pressure on frequently accessed data can be separated. This method is costly because it requires changes to the client architecture and code.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="dcs-faq-0521005__row1617483216361"><td class="cellrowborder" valign="top" headers="mcps1.3.2.1.3.1.1 "><p id="dcs-faq-0521005__p1222717623814"><strong id="dcs-faq-0521005__b11227186103816">Design a circuit breaker or degradation mechanism.</strong></p>
|
|
<p id="dcs-faq-0521005__p222756143820">Hot keys can easily result in cache breakdown. During peak hours, requests are passed through to the backend database, causing service avalanche. To ensure availability, the system must have a circuit breaker or degradation mechanism to limit the traffic and degrade services if breakdown occurs.</p>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dcs-faq-0805001.html">Big/Hot Key Analysis and Expired Key Scan</a></div>
|
|
</div>
|
|
</div>
|
|
|