{"id":90317,"date":"2025-09-01T14:00:25","date_gmt":"2025-09-01T07:00:25","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=90317"},"modified":"2025-09-01T14:21:54","modified_gmt":"2025-09-01T07:21:54","slug":"tong-hop-redis-command","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/","title":{"rendered":"T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#Tong_quan_ve_Redis\" >T\u1ed5ng quan v\u1ec1 Redis<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#Redis_command_la_gi\" >Redis command l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#Tong_hop_Redis_commands\" >T\u1ed5ng h\u1ee3p Redis commands<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#6_vi_du_thuc_te_dung_Redis_command_hieu_qua\" >6 v\u00ed d\u1ee5 th\u1ef1c t\u1ebf d\u00f9ng Redis command hi\u1ec7u qu\u1ea3<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#Cau_hoi_thuong_gap_ve_Redis_command\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 Redis command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><strong><em>Redis l\u00e0 m\u1ed9t h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u in-memory n\u1ed5i b\u1eadt, kh\u00f4ng ch\u1ec9 nh\u1edd hi\u1ec7u n\u0103ng truy xu\u1ea5t c\u1ef1c nhanh m\u00e0 c\u00f2n b\u1edfi kh\u1ea3 n\u0103ng x\u1eed l\u00fd linh ho\u1ea1t v\u1edbi nhi\u1ec1u c\u1ea5u tr\u00fac d\u1eef li\u1ec7u. \u0110\u1ec3 khai th\u00e1c t\u1ed1i \u0111a s\u1ee9c m\u1ea1nh c\u1ee7a Redis, ta c\u1ea7n hi\u1ec3u v\u00e0 s\u1eed d\u1ee5ng th\u00e0nh th\u1ea1o c\u00e1c Redis command t\u1eeb nh\u1eefng l\u1ec7nh c\u01a1 b\u1ea3n \u0111\u1ebfn c\u00e1c l\u1ec7nh n\u00e2ng cao.<\/em><\/strong><\/p>\n\n\n\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u r\u00f5:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1c c\u00e1ch th\u1ef1c thi Redis command<\/li>\n\n\n\n<li>C\u00e1ch s\u1eed d\u1ee5ng t\u1eebng l\u1ec7nh Redis (c\u00f3 v\u00ed d\u1ee5 minh h\u1ecda)<\/li>\n\n\n\n<li>M\u1ed9t s\u1ed1 k\u1ecbch b\u1ea3n d\u00f9ng l\u1ec7nh Redis torng th\u1ef1c t\u1ebf<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-quan-v\u1ec1-redis\"><span class=\"ez-toc-section\" id=\"Tong_quan_ve_Redis\"><\/span><strong>T\u1ed5ng quan v\u1ec1 Redis<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Redis (Remote Dictionary Server) l\u00e0 m\u1ed9t h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u key-value d\u1ea1ng in-memory, m\u00e3 ngu\u1ed3n m\u1edf, n\u1ed5i ti\u1ebfng v\u1edbi t\u1ed1c \u0111\u1ed9 truy xu\u1ea5t c\u1ef1c nhanh. Redis th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 l\u00e0m cache, qu\u1ea3n l\u00fd session, x\u1eed l\u00fd h\u00e0ng \u0111\u1ee3i, l\u01b0u tr\u1eef d\u1eef li\u1ec7u t\u1ea1m th\u1eddi, truy\u1ec1n v\u00e0 nh\u1eadn th\u00f4ng \u0111i\u1ec7p gi\u1eefa c\u00e1c th\u00e0nh ph\u1ea7n trong h\u1ec7 th\u1ed1ng (pub\/sub messaging), x\u00e2y d\u1ef1ng b\u1ea3ng x\u1ebfp h\u1ea1ng (leaderboards), ph\u00e2n t\u00edch d\u1eef li\u1ec7u theo th\u1eddi gian th\u1ef1c (real-time analytics) v\u00e0 h\u1ed7 tr\u1ee3 c\u00e1c h\u1ec7 th\u1ed1ng y\u00eau c\u1ea7u ph\u1ea3n h\u1ed3i nhanh ch\u00f3ng.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>\u0110\u1ecdc th\u00eam: <strong><a href=\"https:\/\/itviec.com\/blog\/redis-la-gi\/\" target=\"_blank\" rel=\"noreferrer noopener\">Redis l\u00e0 g\u00ec: T\u1ed5ng h\u1ee3p t\u00ednh n\u0103ng h\u1eefu \u00edch nh\u1ea5t c\u1ee7a Redis<\/a><\/strong><\/em><\/p>\n<\/blockquote>\n\n\n\n<p>Kh\u00f4ng gi\u1ed1ng c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 (nh\u01b0 MySQL), Redis l\u01b0u tr\u1eef d\u1eef li\u1ec7u trong b\u1ed9 nh\u1edb RAM, gi\u00fap c\u00e1c thao t\u00e1c \u0111\u1ecdc\/ghi di\u1ec5n ra g\u1ea7n nh\u01b0 t\u1ee9c th\u1eddi. Tuy nhi\u00ean, Redis c\u0169ng h\u1ed7 tr\u1ee3 c\u01a1 ch\u1ebf l\u01b0u tr\u1eef b\u1ec1n v\u1eefng (persistence) \u0111\u1ec3 ghi d\u1eef li\u1ec7u xu\u1ed1ng \u0111\u0129a c\u1ee9ng \u0111\u1ec3 tr\u00e1nh m\u1ea5t m\u00e1t trong tr\u01b0\u1eddng h\u1ee3p h\u1ec7 th\u1ed1ng g\u1eb7p s\u1ef1 c\u1ed1. Hai ph\u01b0\u01a1ng ph\u00e1p ph\u1ed5 bi\u1ebfn l\u00e0:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RDB (Redis Database snapshot) ch\u1ee5p nhanh to\u00e0n b\u1ed9 d\u1eef li\u1ec7u \u0111\u1ecbnh k\u1ef3<\/li>\n\n\n\n<li>AOF (Append Only File) ghi l\u1ea1i t\u1eebng thao t\u00e1c ghi d\u1eef li\u1ec7u \u0111\u1ec3 c\u00f3 th\u1ec3 ph\u1ee5c h\u1ed3i ch\u00ednh x\u00e1c tr\u1ea1ng th\u00e1i tr\u01b0\u1edbc \u0111\u00f3.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Ngo\u00e0i ra, Redis h\u1ed7 tr\u1ee3 nhi\u1ec1u ki\u1ec3u d\u1eef li\u1ec7u \u0111a d\u1ea1ng nh\u01b0:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Strings: ki\u1ec3u d\u1eef li\u1ec7u c\u01a1 b\u1ea3n.<\/li>\n\n\n\n<li>Lists: danh s\u00e1ch c\u00f3 th\u1ee9 t\u1ef1.<\/li>\n\n\n\n<li>Sets: t\u1eadp h\u1ee3p kh\u00f4ng tr\u00f9ng l\u1eb7p.<\/li>\n\n\n\n<li>Sorted Sets: t\u1eadp h\u1ee3p c\u00f3 th\u1ee9 t\u1ef1 theo \u0111i\u1ec3m s\u1ed1.<\/li>\n\n\n\n<li>Hashes: b\u1ea3ng b\u0103m (key-value l\u1ed3ng nhau).<\/li>\n\n\n\n<li>Bitmaps, HyperLogLogs: ph\u1ee5c v\u1ee5 t\u00ednh to\u00e1n s\u1ed1 l\u01b0\u1ee3ng ph\u1ea7n t\u1eed duy nh\u1ea5t v\u1edbi chi ph\u00ed th\u1ea5p.<\/li>\n\n\n\n<li>Geospatial Indexes: l\u01b0u tr\u1eef v\u00e0 truy v\u1ea5n theo v\u1ecb tr\u00ed \u0111\u1ecba l\u00fd.<\/li>\n\n\n\n<li>Streams: qu\u1ea3n l\u00fd d\u1eef li\u1ec7u lu\u1ed3ng theo th\u1eddi gian.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-redis-command-la-gi\"><span class=\"ez-toc-section\" id=\"Redis_command_la_gi\"><\/span><strong>Redis command l\u00e0 g\u00ec?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Redis ho\u1ea1t \u0111\u1ed9ng ch\u1ee7 y\u1ebfu th\u00f4ng qua c\u00e1c l\u1ec7nh (commands) \u0111\u01b0\u1ee3c g\u1eedi qua giao th\u1ee9c TCP ho\u1eb7c Unix socket. M\u1ed7i thao t\u00e1c t\u1eeb l\u01b0u d\u1eef li\u1ec7u, \u0111\u1ecdc d\u1eef li\u1ec7u, \u0111\u1ebfn c\u1ea5u h\u00ecnh h\u1ec7 th\u1ed1ng \u0111\u1ec1u \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n th\u00f4ng qua c\u00e2u l\u1ec7nh Redis.<\/p>\n\n\n\n<p>C\u00f3 th\u1ec3 th\u1ef1c thi Redis command b\u1eb1ng nhi\u1ec1u c\u00e1ch:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>D\u00f2ng l\u1ec7nh (redis-cli)<\/strong>: c\u00f4ng c\u1ee5 CLI m\u1eb7c \u0111\u1ecbnh \u0111i k\u00e8m Redis.<\/li>\n\n\n\n<li><strong>T\u1eeb \u1ee9ng d\u1ee5ng<\/strong>: s\u1eed d\u1ee5ng client library nh\u01b0 ioredis, node-redis (Node.js), redis-py (Python), Jedis (Java), StackExchange.Redis (.NET),&#8230;<\/li>\n\n\n\n<li><strong>Th\u00f4ng qua UI tr\u1ef1c quan<\/strong> nh\u01b0 RedisInsight, Redis Commander \u0111\u1ec3 quan s\u00e1t d\u1eef li\u1ec7u v\u00e0 g\u1eedi l\u1ec7nh d\u1ec5 d\u00e0ng.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-h\u1ee3p-redis-commands\"><span class=\"ez-toc-section\" id=\"Tong_hop_Redis_commands\"><\/span><strong>T\u1ed5ng h\u1ee3p Redis commands<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-thao-tac-v\u1edbi-key-value-c\u01a1-b\u1ea3n\"><strong>Nh\u00f3m l\u1ec7nh thao t\u00e1c v\u1edbi key-value (c\u01a1 b\u1ea3n)<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>SET key value [EX seconds] [NX|XX]<\/code><\/td><td>D\u00f9ng \u0111\u1ec3 l\u01b0u gi\u00e1 tr\u1ecb cho m\u1ed9t key; n\u1ebfu key \u0111\u00e3 t\u1ed3n t\u1ea1i th\u00ec gi\u00e1 tr\u1ecb s\u1ebd b\u1ecb ghi \u0111\u00e8.<br>&#8211; <code>EX seconds<\/code>: thi\u1ebft l\u1eadp th\u1eddi gian s\u1ed1ng (expire time) cho key, t\u00ednh b\u1eb1ng gi\u00e2y.<br>&#8211; <code>NX<\/code>: ch\u1ec9 th\u1ef1c hi\u1ec7n n\u1ebfu key ch\u01b0a t\u1ed3n t\u1ea1i (th\u01b0\u1eddng d\u00f9ng trong c\u01a1 ch\u1ebf locking \u0111\u1ec3 tr\u00e1nh ghi \u0111\u00e8).<br>&#8211; <code>XX<\/code>: ch\u1ec9 th\u1ef1c hi\u1ec7n n\u1ebfu key \u0111\u00e3 t\u1ed3n t\u1ea1i (d\u00f9ng \u0111\u1ec3 c\u1eadp nh\u1eadt c\u00f3 \u0111i\u1ec1u ki\u1ec7n).<\/td><td>L\u01b0u t\u00ean ng\u01b0\u1eddi d\u00f9ng:<br><code>SET user:name \"Alice\"<\/code><br>\u2192 L\u01b0u chu\u1ed7i &#8220;<em>Alice<\/em>&#8221; v\u00e0o key user:name.&nbsp;N\u1ebfu key ch\u01b0a t\u1ed3n t\u1ea1i, Redis s\u1ebd t\u1ea1o m\u1edbi.&nbsp;<br><code>SET session:token \"abc123\" EX 3600 NX<\/code><\/td><\/tr><tr><td><code>GET key<\/code><\/td><td>Truy xu\u1ea5t gi\u00e1 tr\u1ecb \u0111\u00e3 l\u01b0u t\u1eeb key v\u00e0 tr\u1ea3 v\u1ec1 <code>nil <\/code>n\u1ebfu key kh\u00f4ng t\u1ed3n t\u1ea1i.<\/td><td>L\u1ea5y t\u00ean \u0111\u00e3 l\u01b0u:<br><code>GET user:name<br><\/code>\u2192 Tr\u1ea3 v\u1ec1 &#8220;<em>Alice<\/em>&#8221; n\u1ebfu key t\u1ed3n t\u1ea1i.<\/td><\/tr><tr><td><code>DEL key [key ...]<\/code><\/td><td>X\u00f3a m\u1ed9t ho\u1eb7c nhi\u1ec1u key kh\u1ecfi Redis. Tr\u1ea3 v\u1ec1 s\u1ed1 l\u01b0\u1ee3ng key \u0111\u00e3 x\u00f3a th\u00e0nh c\u00f4ng.<\/td><td>Xo\u00e1 t\u00ean \u0111\u00e3 l\u01b0u:<br><code>DEL user:name<br><\/code>X\u00f3a nhi\u1ec1u key:&nbsp;<br><code>DEL user:name user:email user:age<\/code><\/td><\/tr><tr><td>EXPIRE key seconds<\/td><td>Thi\u1ebft l\u1eadp th\u1eddi gian s\u1ed1ng cho m\u1ed9t key (t\u00ednh b\u1eb1ng gi\u00e2y). Sau th\u1eddi gian n\u00e0y, key s\u1ebd t\u1ef1 \u0111\u1ed9ng b\u1ecb x\u00f3a.<\/td><td>Thi\u1ebft l\u1eadp th\u1eddi gian s\u1ed1ng cho key:EXPIRE session:123 3600<br>\u2192 Sau 60 gi\u00e2y, key user:name s\u1ebd b\u1ecb Redis t\u1ef1 \u0111\u1ed9ng x\u00f3a.<\/td><\/tr><tr><td><code>TTL key<\/code><\/td><td>Ki\u1ec3m tra th\u1eddi gian c\u00f2n l\u1ea1i (t\u00ednh b\u1eb1ng gi\u00e2y) tr\u01b0\u1edbc khi key h\u1ebft h\u1ea1n. Tr\u1ea3 v\u1ec1 -1 n\u1ebfu key kh\u00f4ng c\u00f3 expire, -2 n\u1ebfu key kh\u00f4ng t\u1ed3n t\u1ea1i.<\/td><td><code>TTL session:123<\/code>&nbsp;<br>\u2192 Tr\u1ea3 v\u1ec1 s\u1ed1 gi\u00e2y c\u00f2n l\u1ea1i ho\u1eb7c -1\/-2<\/td><\/tr><tr><td><code>SETEX key seconds value<\/code><\/td><td>Thi\u1ebft l\u1eadp gi\u00e1 tr\u1ecb v\u00e0 th\u1eddi gian h\u1ebft h\u1ea1n cho key trong m\u1ed9t b\u01b0\u1edbc.<\/td><td><code>SETEX session:token 3600 \"abc123\"<\/code><br>\u2192 L\u01b0u gi\u00e1 tr\u1ecb &#8220;<em>abc123<\/em>&#8221; v\u00e0o key <em>session:token<\/em> v\u00e0 thi\u1ebft l\u1eadp th\u1eddi gian s\u1ed1ng cho key l\u00e0 3600 gi\u00e2y.<\/td><\/tr><tr><td><code>PSETEX key milliseconds value<\/code><\/td><td>Thi\u1ebft l\u1eadp gi\u00e1 tr\u1ecb v\u00e0 th\u1eddi gian h\u1ebft h\u1ea1n cho key t\u00ednh b\u1eb1ng millisecond.<\/td><td><code>PSETEX session:token 3600000 \"abc123\"<\/code><br>\u2192 L\u01b0u gi\u00e1 tr\u1ecb &#8220;<em>abc123<\/em>&#8221; v\u00e0o key <em>session:token<\/em> v\u00e0 thi\u1ebft l\u1eadp th\u1eddi gian s\u1ed1ng cho key l\u00e0 3600000 milliseconds (60 ph\u00fat).<\/td><\/tr><tr><td><code>SETNX key value<\/code><\/td><td>Thi\u1ebft l\u1eadp gi\u00e1 tr\u1ecb cho key n\u1ebfu key ch\u01b0a t\u1ed3n t\u1ea1i.<\/td><td><code>SETNX user:name \"Alice\"<\/code><br>\u2192 L\u1ec7nh n\u00e0y ch\u1ec9 th\u1ef1c hi\u1ec7n n\u1ebfu key <em>user:name<\/em> ch\u01b0a t\u1ed3n t\u1ea1i.<\/td><\/tr><tr><td><code>ARRAPPEND key value [value ...]<\/code><\/td><td>Th\u00eam ph\u1ea7n t\u1eed v\u00e0o cu\u1ed1i m\u1ea3ng (array) trong key.<\/td><td><code>ARRAPPEND arr1 \"value1\" \"value2\"<br><\/code>\u2192 Th\u00eam c\u00e1c ph\u1ea7n t\u1eed &#8220;value1&#8221;, &#8220;value2&#8221; v\u00e0o cu\u1ed1i m\u1ea3ng <em>arr1<\/em>.<\/td><\/tr><tr><td><code>ARRINDEX key value<\/code><\/td><td>Tr\u1ea3 v\u1ec1 ch\u1ec9 s\u1ed1 c\u1ee7a m\u1ed9t ph\u1ea7n t\u1eed trong m\u1ea3ng (array) c\u1ee7a key.<\/td><td><code>ARRINDEX arr1 \"value1\"<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 ch\u1ec9 s\u1ed1 c\u1ee7a ph\u1ea7n t\u1eed &#8220;value1&#8221; trong m\u1ea3ng <em>arr1<\/em>.<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-thao-tac-v\u1edbi-s\u1ed1-va-chu\u1ed7i\"><strong>Nh\u00f3m l\u1ec7nh thao t\u00e1c v\u1edbi s\u1ed1 v\u00e0 chu\u1ed7i<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>INCR key<\/code><\/td><td>T\u0103ng gi\u00e1 tr\u1ecb s\u1ed1 nguy\u00ean c\u1ee7a key l\u00ean 1. N\u1ebfu key ch\u01b0a t\u1ed3n t\u1ea1i, Redis s\u1ebd kh\u1edfi t\u1ea1o n\u00f3 v\u1edbi gi\u00e1 tr\u1ecb 0 r\u1ed3i t\u0103ng.<\/td><td><code>T\u1ea1o b\u1ed9 \u0111\u1ebfm l\u01b0\u1ee3t truy c\u1eadp:INCR page:views<br><\/code>\u2192 Redis s\u1ebd t\u0103ng gi\u00e1 tr\u1ecb t\u1ea1i key page:views th\u00eam 1. N\u1ebfu key ch\u01b0a t\u1ed3n t\u1ea1i, n\u00f3 s\u1ebd \u0111\u01b0\u1ee3c t\u1ea1o v\u00e0 kh\u1edfi t\u1ea1o b\u1eb1ng 1.<\/td><\/tr><tr><td><code>DECR key<\/code><\/td><td>Gi\u1ea3m gi\u00e1 tr\u1ecb s\u1ed1 nguy\u00ean c\u1ee7a key \u0111i 1.<\/td><td><code>DECR page:views<\/code><\/td><\/tr><tr><td><code>STRLEN key<\/code><\/td><td>Tr\u1ea3 v\u1ec1 \u0111\u1ed9 d\u00e0i chu\u1ed7i gi\u00e1 tr\u1ecb c\u1ee7a key.<\/td><td><code>STRLEN msg&nbsp;<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 6 n\u1ebfu msg = &#8220;<em>Hello!<\/em>&#8220;.<\/td><\/tr><tr><td><code>APPEND key value<\/code><\/td><td>Th\u00eam chu\u1ed7i v\u00e0o cu\u1ed1i gi\u00e1 tr\u1ecb hi\u1ec7n t\u1ea1i c\u1ee7a key.<\/td><td><code>APPEND msg \"!\"&nbsp;<\/code><br>\u2192 N\u1ebfu key = &#8220;Hello&#8221;, k\u1ebft qu\u1ea3 l\u00e0 &#8220;Hello!&#8221;.<\/td><\/tr><tr><td><code>MGET key [key ...]<\/code><\/td><td>Tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb c\u1ee7a nhi\u1ec1u keys c\u00f9ng m\u1ed9t l\u00fac.<\/td><td><code>MGET user:name user:email<br><\/code>\u2192 Tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c key user:name v\u00e0 user:email n\u1ebfu ch\u00fang t\u1ed3n t\u1ea1i.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-thao-tac-v\u1edbi-danh-sach-list\"><strong>Nh\u00f3m l\u1ec7nh thao t\u00e1c v\u1edbi danh s\u00e1ch (List)<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>LPUSH key value [value ...]<\/code><\/td><td>Th\u00eam ph\u1ea7n t\u1eed v\u00e0o \u0111\u1ea7u danh s\u00e1ch. N\u1ebfu key ch\u01b0a t\u1ed3n t\u1ea1i, Redis s\u1ebd t\u1ea1o m\u1edbi.<\/td><td>Th\u00eam c\u00f4ng vi\u1ec7c v\u00e0o h\u00e0ng \u0111\u1ee3i:<br><code>LPUSH tasks \"email\"<br><\/code>\u2192 Redis s\u1ebd th\u00eam c\u00f4ng vi\u1ec7c v\u00e0o \u0111\u1ea7u danh s\u00e1ch, trong \u0111\u00f3:<br>&#8211; <code>tasks <\/code>l\u00e0 danh s\u00e1ch h\u00e0ng \u0111\u1ee3i<br>&#8211; &#8220;<code>send-email<\/code>&#8221; l\u00e0 c\u00f4ng vi\u1ec7c m\u1edbi.<\/td><\/tr><tr><td><code>RPUSH key value [value ...]<\/code><\/td><td>Th\u00eam m\u1ed9t ph\u1ea7n t\u1eed v\u00e0o cu\u1ed1i c\u1ee7a danh s\u00e1ch.<\/td><td>Th\u00eam c\u00f4ng vi\u1ec7c v\u00e0o cu\u1ed1i h\u00e0ng \u0111\u1ee3i:<br><code>LPUSH job:queue \"send-email\"<\/code><br>Trong \u0111\u00f3:<br>&#8211; <code>job:queue<\/code>: t\u00ean danh s\u00e1ch<br>&#8211; &#8220;<code>send-email<\/code>&#8220;: c\u00f4ng vi\u1ec7c c\u1ea7n x\u1eed l\u00fd<br>\u2192 Redis s\u1ebd th\u00eam &#8220;<code>send-email<\/code>&#8221; v\u00e0o \u0111\u1ea7u danh s\u00e1ch. C\u00e1c ph\u1ea7n t\u1eed m\u1edbi s\u1ebd x\u1ebfp sau c\u00e1c ph\u1ea7n t\u1eed c\u0169. Th\u00edch h\u1ee3p cho ki\u1ec3u x\u1eed l\u00fd&nbsp; FIFO (First In &#8211; First Out)<\/td><\/tr><tr><td><code>LPOP key [count]<\/code><\/td><td>L\u1ea5y v\u00e0 lo\u1ea1i b\u1ecf ph\u1ea7n t\u1eed \u1edf \u0111\u1ea7u danh s\u00e1ch. C\u00f3 th\u1ec3 l\u1ea5y nhi\u1ec1u ph\u1ea7n t\u1eed c\u00f9ng l\u00fac v\u1edbi tham s\u1ed1 count.<\/td><td>L\u1ea5y c\u00f4ng vi\u1ec7c ti\u1ebfp theo:<br><code>LPOP tasks<\/code><br><code>LPOP tasks 3 (l\u1ea5y 3 ph\u1ea7n t\u1eed)<\/code>&nbsp;<br>\u2192 L\u1ea5y v\u00e0 x\u00f3a ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean trong danh s\u00e1ch.<\/td><\/tr><tr><td><code>LRANGE key start stop<\/code><\/td><td>Truy v\u1ea5n m\u1ed9t ph\u1ea1m vi ph\u1ea7n t\u1eed t\u1eeb danh s\u00e1ch, v\u1edbi ch\u1ec9 s\u1ed1 b\u1eaft \u0111\u1ea7u v\u00e0 k\u1ebft th\u00fac.<\/td><td><code>LRANGE tasks 0 2<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 c\u00e1c ph\u1ea7n t\u1eed trong danh s\u00e1ch tasks t\u1eeb ch\u1ec9 s\u1ed1 0 \u0111\u1ebfn ch\u1ec9 s\u1ed1 2 (bao g\u1ed3m c\u1ea3 ch\u1ec9 s\u1ed1 0 v\u00e0 2).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-thao-tac-v\u1edbi-b\u1ea3ng-bam-hash\"><strong>Nh\u00f3m l\u1ec7nh thao t\u00e1c v\u1edbi b\u1ea3ng b\u0103m (Hash)<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>HSET key field value<\/code><\/td><td>G\u00e1n m\u1ed9t field trong b\u1ea3ng hash (gi\u1ed1ng nh\u01b0 object JSON).<\/td><td>L\u01b0u th\u00f4ng tin ng\u01b0\u1eddi d\u00f9ng d\u1ea1ng object:<br><code>HSET user:1 name \"Alice\"<\/code><br>\u2192 T\u1ea1o b\u1ea3ng hash user:1, g\u00e1n field name l\u00e0 &#8220;<em>Alice<\/em>&#8220;.<\/td><\/tr><tr><td><code>HGET key field<\/code><\/td><td>L\u1ea5y gi\u00e1 tr\u1ecb field t\u1eeb hash.<\/td><td><code>HGET user:1 name<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 &#8220;<em>Alice<\/em>&#8221;&nbsp;<\/td><\/tr><tr><td><code>HDEL key field [field ...]<\/code><\/td><td>X\u00f3a m\u1ed9t ho\u1eb7c nhi\u1ec1u tr\u01b0\u1eddng trong b\u1ea3ng hash.<\/td><td><code>HDEL user:1 age<\/code><br>\u2192 X\u00f3a tr\u01b0\u1eddng age trong b\u1ea3ng hash <em>user:1<\/em>.<\/td><\/tr><tr><td><code>HGETALL key<\/code><\/td><td>L\u1ea5y t\u1ea5t c\u1ea3 c\u00e1c tr\u01b0\u1eddng v\u00e0 gi\u00e1 tr\u1ecb c\u1ee7a m\u1ed9t b\u1ea3ng hash.<\/td><td><code>HGETALL user:1<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 t\u1ea5t c\u1ea3 c\u00e1c tr\u01b0\u1eddng v\u00e0 gi\u00e1 tr\u1ecb trong b\u1ea3ng hash <em>user:1<\/em>.<\/td><\/tr><tr><td><code>HINCRBY key field increment<\/code><\/td><td>T\u0103ng gi\u00e1 tr\u1ecb c\u1ee7a m\u1ed9t tr\u01b0\u1eddng trong b\u1ea3ng hash.<\/td><td><code>HINCRBY user:1 age 1<\/code><br>\u2192 T\u0103ng gi\u00e1 tr\u1ecb c\u1ee7a tr\u01b0\u1eddng age trong b\u1ea3ng hash user:1 l\u00ean 1.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-thao-tac-v\u1edbi-t\u1eadp-h\u1ee3p-set\"><strong>Nh\u00f3m l\u1ec7nh thao t\u00e1c v\u1edbi t\u1eadp h\u1ee3p (Set)<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>SADD key value<\/code><\/td><td>Th\u00eam ph\u1ea7n t\u1eed v\u00e0o t\u1eadp h\u1ee3p (set), kh\u00f4ng th\u00eam n\u1ebfu ph\u1ea7n t\u1eed \u0111\u00e3 t\u1ed3n t\u1ea1i.<\/td><td>T\u1ea1o danh s\u00e1ch tag kh\u00f4ng tr\u00f9ng l\u1eb7p:<br><code>SADD tags \"redis\"<\/code><br>\u2192 Th\u00eam &#8220;<em>redis<\/em>&#8221; v\u00e0o t\u1eadp h\u1ee3p tags. N\u1ebfu &#8220;<em>redis<\/em>&#8221; \u0111\u00e3 c\u00f3 th\u00ec kh\u00f4ng th\u00eam l\u1ea1i.<\/td><\/tr><tr><td><code>SISMEMBER key value<\/code><\/td><td>Ki\u1ec3m tra xem ph\u1ea7n t\u1eed c\u00f3 trong t\u1eadp h\u1ee3p kh\u00f4ng.<\/td><td><code>SISMEMBER tags \"redis\"<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 1 n\u1ebfu &#8220;<em>redis<\/em>&#8221; c\u00f3 trong t\u1eadp, 0 n\u1ebfu kh\u00f4ng.<\/td><\/tr><tr><td><code>SPOP key [count]<\/code><\/td><td>L\u1ea5y v\u00e0 x\u00f3a m\u1ed9t ho\u1eb7c nhi\u1ec1u ph\u1ea7n t\u1eed ng\u1eabu nhi\u00ean t\u1eeb t\u1eadp h\u1ee3p.<\/td><td><code>SPOP tags 2<\/code><br>\u2192 L\u1ea5y v\u00e0 x\u00f3a 2 ph\u1ea7n t\u1eed ng\u1eabu nhi\u00ean t\u1eeb t\u1eadp h\u1ee3p <em>tags<\/em>.<\/td><\/tr><tr><td><code>SMEMBERS key<\/code><\/td><td>L\u1ea5y t\u1ea5t c\u1ea3 c\u00e1c ph\u1ea7n t\u1eed trong m\u1ed9t t\u1eadp h\u1ee3p.<\/td><td><code>SMEMBERS tags<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 t\u1ea5t c\u1ea3 c\u00e1c ph\u1ea7n t\u1eed trong t\u1eadp h\u1ee3p <em>tags<\/em>.<\/td><\/tr><tr><td><code>SDIFF key1 key2 [key3 ...]<\/code><\/td><td>Tr\u1ea3 v\u1ec1 ph\u1ea7n t\u1eed ch\u1ec9 c\u00f3 trong t\u1eadp h\u1ee3p \u0111\u1ea7u ti\u00ean nh\u01b0ng kh\u00f4ng c\u00f3 trong c\u00e1c t\u1eadp h\u1ee3p c\u00f2n l\u1ea1i.<\/td><td><code>SDIFF tags other_tags<\/code><br>\u2192 Tr\u1ea3 v\u1ec1 ph\u1ea7n t\u1eed trong t\u1eadp h\u1ee3p <em>tags<\/em> m\u00e0 kh\u00f4ng c\u00f3 trong t\u1eadp h\u1ee3p <em>other_tags<\/em>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-thao-tac-v\u1edbi-sorted-set-zset\"><strong>Nh\u00f3m l\u1ec7nh thao t\u00e1c v\u1edbi Sorted Set (ZSet)<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>ZADD key score member<\/code><\/td><td>Th\u00eam ph\u1ea7n t\u1eed v\u00e0o sorted set k\u00e8m \u0111i\u1ec3m s\u1ed1 (d\u00f9ng \u0111\u1ec3 s\u1eafp x\u1ebfp).<\/td><td>T\u1ea1o b\u1ea3ng x\u1ebfp h\u1ea1ng:<br><code>ZADD leaderboard 1500 \"player1\"<\/code><br>\u2192 Th\u00eam &#8220;<em>player1<\/em>&#8221; v\u1edbi \u0111i\u1ec3m 1500 v\u00e0o sorted set leaderboard.<\/td><\/tr><tr><td><code>ZRANGE key start stop [WITHSCORES]<\/code><\/td><td>Truy xu\u1ea5t c\u00e1c ph\u1ea7n t\u1eed theo th\u1ee9 t\u1ef1 \u0111i\u1ec3m t\u1eeb th\u1ea5p \u0111\u1ebfn cao.<\/td><td><code>ZRANGE leaderboard 0 -1 WITHSCORES<br><\/code>\u2192 L\u1ea5y to\u00e0n b\u1ed9 danh s\u00e1ch theo th\u1ee9 t\u1ef1 t\u1eeb th\u1ea5p \u0111\u1ebfn cao k\u00e8m \u0111i\u1ec3m.<br><\/td><\/tr><tr><td>ZREM key member [member &#8230;]<\/td><td>X\u00f3a m\u1ed9t ho\u1eb7c nhi\u1ec1u ph\u1ea7n t\u1eed kh\u1ecfi sorted set.<\/td><td>ZREM leaderboard &#8220;player1&#8243;\u2192 X\u00f3a ph\u1ea7n t\u1eed &#8220;player1&#8221; kh\u1ecfi sorted set <em>leaderboard<\/em>.<\/td><\/tr><tr><td><code>ZINCRBY key increment member<\/code><\/td><td>T\u0103ng \u0111i\u1ec3m c\u1ee7a m\u1ed9t ph\u1ea7n t\u1eed trong sorted set.<\/td><td><code>ZINCRBY leaderboard 10 \"player1\"<\/code><br>\u2192 T\u0103ng \u0111i\u1ec3m c\u1ee7a ph\u1ea7n t\u1eed &#8220;player1&#8221; trong sorted set <em>leaderboard<\/em> l\u00ean 10.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-thao-tac-v\u1edbi-stream-d\u1eef-li\u1ec7u-lu\u1ed3ng\"><strong>Nh\u00f3m l\u1ec7nh thao t\u00e1c v\u1edbi Stream (d\u1eef li\u1ec7u lu\u1ed3ng)<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>XADD key ID field value [field value ...]<\/code><\/td><td>Th\u00eam m\u1ed9t m\u1ee5c v\u00e0o stream (d\u00f2ng) Redis.<\/td><td><code>XADD mystream * sensor-id 1234 temperature 19<br><\/code>\u2192 Th\u00eam m\u1ee5c m\u1edbi v\u00e0o stream <em>mystream<\/em> v\u1edbi ID t\u1ef1 \u0111\u1ed9ng v\u00e0 c\u00e1c tr\u01b0\u1eddng <em>sensor-id<\/em> v\u00e0 <em>temperature<\/em>.<\/td><\/tr><tr><td><code>XREAD key [COUNT count] [BLOCK milliseconds] STREAMS key [key ...]<\/code><\/td><td>\u0110\u1ecdc t\u1eeb stream Redis. L\u1ec7nh n\u00e0y th\u01b0\u1eddng d\u00f9ng trong h\u1ec7 th\u1ed1ng th\u1eddi gian th\u1ef1c (real-time).<\/td><td><code>XREAD COUNT 2 BLOCK 0 STREAMS mystream<\/code><br>\u2192 \u0110\u1ecdc c\u00e1c m\u1ee5c t\u1eeb stream <em>mystream<\/em>, tr\u1ea3 v\u1ec1 2 m\u1ee5c \u0111\u1ea7u ti\u00ean, v\u00e0 ch\u1eb7n cho \u0111\u1ebfn khi c\u00f3 m\u1ee5c m\u1edbi n\u1ebfu c\u1ea7n.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-pub-sub-va-transaction\"><strong>Nh\u00f3m l\u1ec7nh Pub\/Sub v\u00e0 Transaction<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>PUBLISH channel message<\/code><\/td><td>G\u1eedi m\u1ed9t tin nh\u1eafn \u0111\u1ebfn m\u1ed9t k\u00eanh.<\/td><td><code>PUBLISH news \"Hello, Redis!\"<\/code><br>\u2192 G\u1eedi tin nh\u1eafn &#8220;Hello, Redis!&#8221; \u0111\u1ebfn k\u00eanh <em>news<\/em>.<\/td><\/tr><tr><td><code>SUBSCRIBE channel [channel ...]<\/code><\/td><td>\u0110\u0103ng k\u00fd \u0111\u1ec3 nh\u1eadn tin nh\u1eafn t\u1eeb m\u1ed9t k\u00eanh.<\/td><td><code>SUBSCRIBE news<\/code><br>\u2192 \u0110\u0103ng k\u00fd \u0111\u1ec3 nh\u1eadn t\u1ea5t c\u1ea3 tin nh\u1eafn t\u1eeb k\u00eanh <em>news<\/em>.<\/td><\/tr><tr><td><code>MULTI<\/code><\/td><td>B\u1eaft \u0111\u1ea7u m\u1ed9t giao d\u1ecbch Redis, \u0111\u01b0\u1ee3c th\u1ef1c thi sau khi g\u1ecdi l\u1ec7nh <code>EXEC<\/code><\/td><td><\/td><\/tr><tr><td><code>EXEC<\/code><\/td><td>Th\u1ef1c thi c\u00e1c l\u1ec7nh trong giao d\u1ecbch \u0111\u00e3 b\u1eaft \u0111\u1ea7u v\u1edbi l\u1ec7nh <code>MULTI<\/code>.<\/td><td><\/td><\/tr><tr><td><code>DISCARD<\/code><\/td><td>H\u1ee7y b\u1ecf giao d\u1ecbch.<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nhom-l\u1ec7nh-qu\u1ea3n-tr\u1ecb-va-quan-sat-h\u1ec7-th\u1ed1ng\"><strong>Nh\u00f3m l\u1ec7nh qu\u1ea3n tr\u1ecb v\u00e0 quan s\u00e1t h\u1ec7 th\u1ed1ng<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>C\u00fa ph\u00e1p l\u1ec7nh<\/strong><\/td><td><strong>M\u00f4 t\u1ea3 chi ti\u1ebft<\/strong><\/td><td><strong>V\u00ed d\u1ee5 l\u1ec7nh<\/strong><\/td><\/tr><tr><td><code>SCAN cursor [MATCH pattern] [COUNT count]<\/code><\/td><td>Duy\u1ec7t qua c\u00e1c keys trong Redis theo c\u00e1ch kh\u00f4ng \u0111\u1ed3ng b\u1ed9, gi\u00fap tr\u00e1nh ch\u1eb7n Redis.<\/td><td><code>SCAN 0 MATCH user:* COUNT 100<\/code><br>\u2192 T\u00ecm ki\u1ebfm t\u1ea5t c\u1ea3 c\u00e1c keys b\u1eaft \u0111\u1ea7u v\u1edbi &#8220;user:&#8221; v\u00e0 gi\u1edbi h\u1ea1n k\u1ebft qu\u1ea3 l\u00e0 100 keys.<\/td><\/tr><tr><td><code>INFO<\/code><\/td><td>Tr\u1ea3 v\u1ec1 th\u00f4ng tin v\u1ec1 Redis server.<\/td><td><\/td><\/tr><tr><td><code>MONITOR<\/code><\/td><td>Gi\u00e1m s\u00e1t t\u1ea5t c\u1ea3 c\u00e1c l\u1ec7nh \u0111\u01b0\u1ee3c g\u1eedi \u0111\u1ebfn Redis server.<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-6-vi-d\u1ee5-th\u1ef1c-t\u1ebf-dung-redis-command-hi\u1ec7u-qu\u1ea3\"><span class=\"ez-toc-section\" id=\"6_vi_du_thuc_te_dung_Redis_command_hieu_qua\"><\/span><strong>6 v\u00ed d\u1ee5 th\u1ef1c t\u1ebf d\u00f9ng Redis command hi\u1ec7u qu\u1ea3<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cache-d\u1eef-li\u1ec7u-api-d\u1ec3-tang-t\u1ed1c-d\u1ed9-ph\u1ea3n-h\u1ed3i\"><strong>Cache d\u1eef li\u1ec7u API \u0111\u1ec3 t\u0103ng t\u1ed1c \u0111\u1ed9 ph\u1ea3n h\u1ed3i<\/strong><\/h3>\n\n\n\n<p>Trong c\u00e1c \u1ee9ng d\u1ee5ng hi\u1ec7n \u0111\u1ea1i, d\u1eef li\u1ec7u th\u01b0\u1eddng \u0111\u01b0\u1ee3c l\u1ea5y t\u1eeb API b\u00ean ngo\u00e0i (th\u1eddi ti\u1ebft, t\u1ef7 gi\u00e1, d\u1eef li\u1ec7u th\u1ecb tr\u01b0\u1eddng,\u2026). N\u1ebfu m\u1ed7i l\u1ea7n ng\u01b0\u1eddi d\u00f9ng thao t\u00e1c \u0111\u1ec1u ph\u1ea3i g\u1ecdi API th\u00ec \u1ee9ng d\u1ee5ng s\u1ebd ch\u1eadm v\u00e0 d\u1ec5 b\u1ecb rate limit.&nbsp;<\/p>\n\n\n\n<p>Gi\u1ea3i ph\u00e1p l\u00e0 cache k\u1ebft qu\u1ea3 t\u1ea1m th\u1eddi trong Redis. V\u00ed d\u1ee5 \u0111\u1ec3 cache d\u1eef li\u1ec7u API trong 5 ph\u00fat, ta d\u00f9ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET weather:saigon\n# N\u1ebfu ch\u01b0a c\u00f3, g\u1ecdi API r\u1ed3i:\nSET weather:saigon \"{temp:31}\" EX 300<\/code><\/pre>\n\n\n\n<p>=&gt; D\u1eef li\u1ec7u \u0111\u01b0\u1ee3c l\u01b0u v\u00e0o Redis v\u1edbi key <code>weather:saigon<\/code>, th\u1eddi h\u1ea1n 300 gi\u00e2y. L\u1ea7n truy c\u1eadp sau ch\u1ec9 c\u1ea7n <code>GET<\/code>, kh\u00f4ng c\u1ea7n g\u1ecdi l\u1ea1i API.<\/p>\n\n\n\n<p><strong><em>M\u1eb9o: <\/em><\/strong><em>N\u00ean \u00e1p d\u1ee5ng quy t\u1eafc \u0111\u1eb7t t\u00ean (naming convention) nh\u1ea5t qu\u00e1n cho c\u00e1c key trong Redis, v\u00ed d\u1ee5: <code>api:weather:saigon<\/code>. Ngo\u00e0i ra, b\u1ea1n c\u0169ng c\u00f3 th\u1ec3 tri\u1ec3n khai c\u00e1c chi\u1ebfn l\u01b0\u1ee3c nh\u01b0 cache warming (t\u1ef1 \u0111\u1ed9ng n\u1ea1p s\u1eb5n d\u1eef li\u1ec7u ph\u1ed5 bi\u1ebfn v\u00e0o cache sau khi kh\u1edfi \u0111\u1ed9ng h\u1ec7 th\u1ed1ng) v\u00e0 cache invalidation (l\u00e0m m\u1edbi ho\u1eb7c x\u00f3a cache khi d\u1eef li\u1ec7u g\u1ed1c thay \u0111\u1ed5i) \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o hi\u1ec7u su\u1ea5t v\u00e0 t\u00ednh nh\u1ea5t qu\u00e1n d\u1eef li\u1ec7u.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-l\u01b0u-phien-dang-nh\u1eadp-ng\u01b0\u1eddi-dung-session-store\"><strong>L\u01b0u phi\u00ean \u0111\u0103ng nh\u1eadp ng\u01b0\u1eddi d\u00f9ng (Session Store)<\/strong><\/h3>\n\n\n\n<p>Khi ng\u01b0\u1eddi d\u00f9ng \u0111\u0103ng nh\u1eadp, h\u1ec7 th\u1ed1ng th\u01b0\u1eddng c\u1ea7n l\u01b0u tr\u1ea1ng th\u00e1i \u0111\u0103ng nh\u1eadp t\u1ea1m th\u1eddi (token, userID,&#8230;). Redis l\u00e0 n\u01a1i l\u01b0u session l\u00fd t\u01b0\u1edfng v\u00ec t\u1ed1c \u0111\u1ed9 cao v\u00e0 h\u1ed7 tr\u1ee3 TTL.&nbsp;<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u01b0u session c\u00f3 th\u1eddi h\u1ea1n 1 gi\u1edd, ta d\u00f9ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET session:abc123 \"userId:42\"\nEXPIRE session:abc123 3600<\/code><\/pre>\n\n\n\n<p>=&gt; Session <code>abc123 <\/code>l\u01b0u th\u00f4ng tin ng\u01b0\u1eddi d\u00f9ng, sau 3600 gi\u00e2y s\u1ebd t\u1ef1 h\u1ebft h\u1ea1n. Ph\u00f9 h\u1ee3p cho c\u00e1c h\u1ec7 th\u1ed1ng c\u1ea7n logout t\u1ef1 \u0111\u1ed9ng ho\u1eb7c ch\u1ed1ng session r\u00e1c.<\/p>\n\n\n\n<p><strong><em>M\u1eb9o<\/em><\/strong>: <em>Khi c\u1ea7n l\u01b0u tr\u1eef d\u1eef li\u1ec7u phi\u00ean (session) c\u00f3 c\u1ea5u tr\u00fac ph\u1ee9c t\u1ea1p, b\u1ea1n n\u00ean s\u1eed d\u1ee5ng l\u1ec7nh <code>HSET<\/code>. Redis Hash cho ph\u00e9p l\u01b0u nhi\u1ec1u c\u1eb7p tr\u01b0\u1eddng\u2013gi\u00e1 tr\u1ecb (field\u2013value) trong c\u00f9ng m\u1ed9t key, gi\u00fap b\u1ea1n d\u1ec5 d\u00e0ng c\u1eadp nh\u1eadt t\u1eebng ph\u1ea7n c\u1ee7a session m\u00e0 kh\u00f4ng c\u1ea7n ghi \u0111\u00e8 to\u00e0n b\u1ed9 d\u1eef li\u1ec7u.&nbsp;<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-t\u1ea1o-hang-d\u1ee3i-x\u1eed-ly-cong-vi\u1ec7c-n\u1ec1n-job-queue\"><strong>T\u1ea1o h\u00e0ng \u0111\u1ee3i x\u1eed l\u00fd c\u00f4ng vi\u1ec7c n\u1ec1n (Job Queue)<\/strong><\/h3>\n\n\n\n<p>Trong c\u00e1c h\u1ec7 th\u1ed1ng nh\u01b0 g\u1eedi email, resize \u1ea3nh, x\u1eed l\u00fd \u0111\u01a1n h\u00e0ng\u2026, c\u00e1c t\u00e1c v\u1ee5 th\u01b0\u1eddng ch\u1ea1y n\u1ec1n v\u00e0 c\u1ea7n \u0111\u01b0\u1ee3c x\u1eed l\u00fd l\u1ea7n l\u01b0\u1ee3t. Redis list k\u1ebft h\u1ee3p LPUSH v\u00e0 LPOP l\u00e0 m\u1ed9t gi\u1ea3i ph\u00e1p \u0111\u01a1n gi\u1ea3n v\u00e0 hi\u1ec7u qu\u1ea3.&nbsp;<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 push v\u00e0 pop c\u00f4ng vi\u1ec7c t\u1eeb h\u00e0ng \u0111\u1ee3i:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LPUSH job:queue \"send-email\"\nLPOP job:queue<\/code><\/pre>\n\n\n\n<p>=&gt; LPUSH th\u00eam c\u00f4ng vi\u1ec7c m\u1edbi v\u00e0o h\u00e0ng \u0111\u1ee3i; <code>LPOP <\/code>l\u1ea5y ra c\u00f4ng vi\u1ec7c \u0111\u1ea7u ti\u00ean \u0111\u1ec3 x\u1eed l\u00fd. D\u1ec5 t\u00edch h\u1ee3p v\u1edbi worker ch\u1ea1y song song.<\/p>\n\n\n\n<p><strong><em>M\u1eb9o<\/em><\/strong><em>: D\u00f9ng <code>BLPOP <\/code>\u0111\u1ec3 tr\u00e1nh polling li\u00ean t\u1ee5c, gi\u00fap ti\u1ebft ki\u1ec7m CPU v\u00ec worker ch\u1ec9 \u201cth\u1ee9c d\u1eady\u201d khi c\u00f3 d\u1eef li\u1ec7u v\u00e0 c\u00f3 th\u1ec3 tri\u1ec3n khai h\u00e0ng \u0111\u1ee3i \u01b0u ti\u00ean b\u1eb1ng c\u00e1ch d\u00f9ng nhi\u1ec1u list.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-t\u1ea1o-b\u1ea3ng-x\u1ebfp-h\u1ea1ng-ng\u01b0\u1eddi-ch\u01a1i-leaderboard\"><strong>T\u1ea1o b\u1ea3ng x\u1ebfp h\u1ea1ng ng\u01b0\u1eddi ch\u01a1i (Leaderboard)<\/strong><\/h3>\n\n\n\n<p>Khi c\u1ea7n x\u1ebfp h\u1ea1ng ng\u01b0\u1eddi d\u00f9ng theo \u0111i\u1ec3m s\u1ed1 (v\u00ed d\u1ee5 game, thi \u0111ua, KPI&#8230;), Redis Sorted Set gi\u00fap l\u01b0u v\u00e0 truy v\u1ea5n d\u1eef li\u1ec7u \u0111\u00e3 s\u1eafp x\u1ebfp r\u1ea5t nhanh.&nbsp;<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 th\u00eam ng\u01b0\u1eddi ch\u01a1i v\u00e0o leaderboard v\u00e0 xem b\u1ea3ng x\u1ebfp h\u1ea1ng:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ZADD leaderboard 2000 \"playerA\"\nZADD leaderboard 3200 \"playerB\"\nZRANGE leaderboard 0 -1 WITHSCORES<\/code><\/pre>\n\n\n\n<p>=&gt; Redis s\u1eafp x\u1ebfp theo \u0111i\u1ec3m (<code>score<\/code>), cho ph\u00e9p l\u1ea5y danh s\u00e1ch top theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n (ho\u1eb7c d\u00f9ng <code>ZREVRANGE <\/code>\u0111\u1ec3 l\u1ea5y top gi\u1ea3m d\u1ea7n).<\/p>\n\n\n\n<p><strong><em>M\u1eb9o<\/em><\/strong>: <em>D\u00f9ng <code>ZREVRANGE <\/code>\u0111\u1ec3 l\u1ea5y danh s\u00e1ch x\u1ebfp h\u1ea1ng t\u1eeb cao xu\u1ed1ng (top list), <code>ZRANK <\/code>ho\u1eb7c <code>ZREVRANK<\/code> \u0111\u1ec3 bi\u1ebft v\u1ecb tr\u00ed c\u1ee7a m\u1ed9t user. C\u00f3 th\u1ec3 t\u1ea1o nhi\u1ec1u b\u1ea3ng x\u1ebfp h\u1ea1ng (leaderboard) t\u01b0\u01a1ng \u1ee9ng v\u1edbi t\u1eebng kho\u1ea3ng th\u1eddi gian kh\u00e1c nhau nh\u01b0 ng\u00e0y, tu\u1ea7n, th\u00e1ng.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-d\u1ebfm-l\u01b0\u1ee3t-truy-c\u1eadp-trang-page-views-counter\"><strong>\u0110\u1ebfm l\u01b0\u1ee3t truy c\u1eadp trang (Page Views Counter)<\/strong><\/h3>\n\n\n\n<p>\u0110\u1ebfm s\u1ed1 l\u01b0\u1ee3t xem trang l\u00e0 t\u00ednh n\u0103ng r\u1ea5t ph\u1ed5 bi\u1ebfn. Redis h\u1ed7 tr\u1ee3 <code>INCR <\/code>\u0111\u1ec3 t\u0103ng s\u1ed1 nguy\u00ean nhanh, g\u1ecdn m\u00e0 kh\u00f4ng c\u1ea7n read-modify-write nh\u01b0 database truy\u1ec1n th\u1ed1ng.&nbsp;<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u0103ng l\u01b0\u1ee3t truy c\u1eadp m\u1ed7i l\u1ea7n trang \u0111\u01b0\u1ee3c truy c\u1eadp:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INCR page:homepage<\/code><\/pre>\n\n\n\n<p>=&gt; Redis s\u1ebd t\u0103ng gi\u00e1 tr\u1ecb t\u1ea1i key <code>page:homepage<\/code> th\u00eam 1 m\u1ed7i l\u1ea7n ng\u01b0\u1eddi d\u00f9ng truy c\u1eadp. V\u00ec d\u1eef li\u1ec7u l\u01b0u \u1edf d\u1ea1ng s\u1ed1 nguy\u00ean, thao t\u00e1c r\u1ea5t nh\u1eb9, t\u1ed1c \u0111\u1ed9 cao v\u00e0 \u0111\u1eb7c bi\u1ec7t l\u00e0 atomic &#8211; \u0111\u1ea3m b\u1ea3o t\u00ednh nh\u1ea5t qu\u00e1n ngay c\u1ea3 khi c\u00f3 nhi\u1ec1u ng\u01b0\u1eddi truy c\u1eadp c\u00f9ng l\u00fac.<\/p>\n\n\n\n<p><strong><em>M\u1eb9o<\/em><\/strong><em>: K\u1ebft h\u1ee3p <code>INCR <\/code>v\u1edbi <code>EXPIRE <\/code>\u0111\u1ec3 t\u1ea1o b\u1ed9 \u0111\u1ebfm tr\u01b0\u1ee3t theo th\u1eddi gian (sliding window counters). Ngo\u00e0i ra, c\u00f3 th\u1ec3 d\u00f9ng <code>HINCRBY <\/code>\u0111\u1ec3 nh\u00f3m c\u00e1c b\u1ed9 \u0111\u1ebfm theo ng\u00e0y ho\u1eb7c ph\u00e2n lo\u1ea1i kh\u00e1c, v\u00ed d\u1ee5: <code>HINCRBY stats:daily 2025-07-12 1 <\/code>=&gt; T\u0103ng b\u1ed9 \u0111\u1ebfm truy c\u1eadp cho ng\u00e0y 2025-07-12 trong nh\u00f3m <code>stats:daily<\/code>.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-l\u01b0u-va-g\u1ee3i-y-t\u1eeb-khoa-tim-ki\u1ebfm-khong-trung-l\u1eb7p\"><strong>L\u01b0u v\u00e0 g\u1ee3i \u00fd t\u1eeb kh\u00f3a t\u00ecm ki\u1ebfm kh\u00f4ng tr\u00f9ng l\u1eb7p<\/strong><\/h3>\n\n\n\n<p>Trong h\u1ec7 th\u1ed1ng t\u00ecm ki\u1ebfm, ta c\u1ea7n l\u01b0u danh s\u00e1ch t\u1eeb kh\u00f3a ng\u01b0\u1eddi d\u00f9ng \u0111\u00e3 t\u00ecm, nh\u01b0ng kh\u00f4ng mu\u1ed1n l\u01b0u tr\u00f9ng. Redis Set l\u00e0 c\u1ea5u tr\u00fac ph\u00f9 h\u1ee3p v\u00ec t\u1ef1 lo\u1ea1i b\u1ecf tr\u00f9ng l\u1eb7p.&nbsp;<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u01b0u t\u1eeb kh\u00f3a t\u00ecm ki\u1ebfm:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SADD search:tags \"redis\"\nSADD search:tags \"nodejs\"\nSMEMBERS search:tags<\/code><\/pre>\n\n\n\n<p>=&gt; <code>SADD <\/code>ch\u1ec9 th\u00eam n\u1ebfu ph\u1ea7n t\u1eed ch\u01b0a t\u1ed3n t\u1ea1i. <code>SMEMBERS <\/code>l\u1ea5y to\u00e0n b\u1ed9 t\u1eadp h\u1ee3p, l\u00e0 l\u1ef1a ch\u1ecdn l\u00fd t\u01b0\u1edfng cho autocomplete ho\u1eb7c filter.<\/p>\n\n\n\n<p><strong><em>M\u1eb9o<\/em><\/strong><em>: D\u00f9ng <code>SINTER <\/code>\u0111\u1ec3 t\u00ecm c\u00e1c tag chung gi\u1eefa ng\u01b0\u1eddi d\u00f9ng (intersection), <code>SUNION <\/code>\u0111\u1ec3 g\u1ed9p nhi\u1ec1u t\u1eadp tag l\u1ea1i v\u1edbi nhau. C\u00f3 th\u1ec3 d\u00f9ng <code>SPOP <\/code>\u0111\u1ec3 xoay v\u00f2ng ho\u1eb7c ch\u1ecdn ng\u1eabu nhi\u00ean m\u1ed9t tag trong c\u00e1c k\u1ecbch b\u1ea3n nh\u01b0 g\u1ee3i \u00fd n\u1ed9i dung lu\u00e2n phi\u00ean.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cau-h\u1ecfi-th\u01b0\u1eddng-g\u1eb7p-v\u1ec1-redis-command\"><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_ve_Redis_command\"><\/span><strong>C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 Redis command<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-sao-ki\u1ec3m-tra-key-con-t\u1ed3n-t\u1ea1i-hay-khong\"><strong>L\u00e0m sao ki\u1ec3m tra key c\u00f2n t\u1ed3n t\u1ea1i hay kh\u00f4ng?<\/strong><\/h3>\n\n\n\n<p>\u0110\u1ec3 ki\u1ec3m tra m\u1ed9t key c\u00f3 t\u1ed3n t\u1ea1i trong Redis hay kh\u00f4ng, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng l\u1ec7nh <code>EXISTS &lt;key&gt;<\/code>. L\u1ec7nh n\u00e0y s\u1ebd tr\u1ea3 v\u1ec1 1 n\u1ebfu key t\u1ed3n t\u1ea1i v\u00e0 0 n\u1ebfu kh\u00f4ng t\u1ed3n t\u1ea1i. V\u00ed d\u1ee5: <code>EXISTS user:1<\/code> s\u1ebd tr\u1ea3 v\u1ec1 <code>1<\/code> n\u1ebfu key <code>user:1<\/code> \u0111ang c\u00f3 trong Redis. \u0110\u00e2y l\u00e0 c\u00e1ch ki\u1ec3m tra \u0111\u01a1n gi\u1ea3n v\u00e0 r\u1ea5t h\u1eefu d\u1ee5ng tr\u01b0\u1edbc khi th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c nh\u01b0 <code>GET<\/code>, <code>DEL <\/code>hay <code>EXPIRE<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-redis-command-co-h\u1ed7-tr\u1ee3-atomic-khong\"><strong>Redis command c\u00f3 h\u1ed7 tr\u1ee3 atomic kh\u00f4ng?<\/strong><\/h3>\n\n\n\n<p>C\u00f3. Redis \u0111\u1ea3m b\u1ea3o r\u1eb1ng t\u1ea5t c\u1ea3 c\u00e1c l\u1ec7nh \u0111\u01a1n l\u1ebb \u0111\u1ec1u \u0111\u01b0\u1ee3c th\u1ef1c thi theo c\u00e1ch atomic &#8211; t\u1ee9c l\u00e0 m\u1ed7i l\u1ec7nh \u0111\u01b0\u1ee3c x\u1eed l\u00fd ho\u00e0n to\u00e0n tr\u01b0\u1edbc khi chuy\u1ec3n sang l\u1ec7nh k\u1ebf ti\u1ebfp, kh\u00f4ng b\u1ecb gi\u00e1n \u0111o\u1ea1n hay ghi \u0111\u00e8 b\u1edfi l\u1ec7nh t\u1eeb c\u00e1c client kh\u00e1c.<\/p>\n\n\n\n<p>Ngo\u00e0i ra, Redis c\u0169ng h\u1ed7 tr\u1ee3 th\u1ef1c hi\u1ec7n giao d\u1ecbch (transaction) th\u00f4ng qua nh\u00f3m l\u1ec7nh <code>MULTI<\/code>, <code>EXEC<\/code>, <code>WATCH <\/code>v\u00e0 <code>DISCARD<\/code>, gi\u00fap \u0111\u1ea3m b\u1ea3o t\u00ednh nh\u1ea5t qu\u00e1n khi c\u1ea7n th\u1ef1c hi\u1ec7n nhi\u1ec1u l\u1ec7nh li\u00ean ti\u1ebfp nh\u01b0 m\u1ed9t kh\u1ed1i th\u1ed1ng nh\u1ea5t.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-co-command-nao-d\u1ec3-backup-ho\u1eb7c-dump-d\u1eef-li\u1ec7u-redis-khong\"><strong>C\u00f3 command n\u00e0o \u0111\u1ec3 backup ho\u1eb7c dump d\u1eef li\u1ec7u Redis kh\u00f4ng?<\/strong><\/h3>\n\n\n\n<p>Redis kh\u00f4ng c\u00f3 l\u1ec7nh xu\u1ea5t d\u1eef li\u1ec7u th\u00e0nh file nh\u01b0 database truy\u1ec1n th\u1ed1ng, nh\u01b0ng cung c\u1ea5p hai c\u01a1 ch\u1ebf sao l\u01b0u l\u00e0 RDB (Redis Database Backup) v\u00e0 AOF (Append Only File). V\u1edbi RDB, ta c\u00f3 th\u1ec3 d\u00f9ng l\u1ec7nh <code>SAVE<\/code> (\u0111\u1ed3ng b\u1ed9) ho\u1eb7c <code>BGSAVE <\/code>(ch\u1ea1y n\u1ec1n) \u0111\u1ec3 t\u1ea1o file <code>dump.rdb<\/code> ch\u1ee9a snapshot d\u1eef li\u1ec7u to\u00e0n b\u1ed9 Redis. V\u1edbi AOF, Redis s\u1ebd ghi l\u1ea1i m\u1ecdi l\u1ec7nh ghi d\u1eef li\u1ec7u, c\u00f3 th\u1ec3 d\u00f9ng \u0111\u1ec3 kh\u00f4i ph\u1ee5c tr\u1ea1ng th\u00e1i g\u1ea7n nh\u1ea5t.<\/p>\n\n\n\n<p>Ngo\u00e0i ra, Redis c\u0169ng h\u1ed7 tr\u1ee3 ch\u1ebf \u0111\u1ed9 hybrid persistence, k\u1ebft h\u1ee3p c\u1ea3 RDB v\u00e0 AOF \u0111\u1ec3 t\u0103ng t\u00ednh an to\u00e0n. N\u1ebfu mu\u1ed1n backup, b\u1ea1n ch\u1ec9 c\u1ea7n sao ch\u00e9p c\u00e1c file n\u00e0y t\u1eeb th\u01b0 m\u1ee5c l\u01b0u tr\u1eef c\u1ee7a Redis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-k\u1ebft\"><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><strong>T\u1ed5ng k\u1ebft<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Redis kh\u00f4ng ch\u1ec9 l\u00e0 m\u1ed9t kho l\u01b0u t\u1ea1m \u0111\u01a1n thu\u1ea7n, m\u00e0 l\u00e0 m\u1ed9t n\u1ec1n t\u1ea3ng d\u1eef li\u1ec7u m\u1ea1nh m\u1ebd c\u00f3 th\u1ec3 x\u1eed l\u00fd \u0111\u1ee7 lo\u1ea1i b\u00e0i to\u00e1n&nbsp; t\u1eeb cache, h\u00e0ng \u0111\u1ee3i, session store cho \u0111\u1ebfn leaderboard hay counting. Vi\u1ec7c n\u1eafm v\u1eefng c\u00e1c Redis command gi\u00fap ta vi\u1ebft code hi\u1ec7u qu\u1ea3 h\u01a1n, t\u1eadn d\u1ee5ng t\u1ed1i \u0111a t\u00e0i nguy\u00ean h\u1ec7 th\u1ed1ng v\u00e0 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng c\u00f3 hi\u1ec7u su\u1ea5t v\u01b0\u1ee3t tr\u1ed9i.&nbsp;<\/p>\n\n\n\n<p>ITviec hy v\u1ecdng v\u1edbi cheatsheet v\u00e0 c\u00e1c v\u00ed d\u1ee5 c\u1ee5 th\u1ec3 trong b\u00e0i vi\u1ebft, b\u1ea1n \u0111\u00e3 c\u00f3 n\u1ec1n t\u1ea3ng v\u1eefng v\u00e0ng \u0111\u1ec3 b\u1eaft \u0111\u1ea7u \u1ee9ng d\u1ee5ng Redis v\u00e0o d\u1ef1 \u00e1n c\u1ee7a m\u00ecnh m\u1ed9t c\u00e1ch t\u1ef1 tin v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis l\u00e0 m\u1ed9t h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u in-memory n\u1ed5i b\u1eadt, kh\u00f4ng ch\u1ec9 nh\u1edd hi\u1ec7u n\u0103ng truy xu\u1ea5t c\u1ef1c nhanh m\u00e0 c\u00f2n b\u1edfi kh\u1ea3 n\u0103ng x\u1eed l\u00fd linh ho\u1ea1t v\u1edbi nhi\u1ec1u c\u1ea5u tr\u00fac d\u1eef li\u1ec7u. \u0110\u1ec3 khai th\u00e1c t\u1ed1i \u0111a s\u1ee9c m\u1ea1nh c\u1ee7a Redis, ta c\u1ea7n hi\u1ec3u v\u00e0 s\u1eed d\u1ee5ng th\u00e0nh th\u1ea1o c\u00e1c Redis [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":90866,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-90317","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5 - ITviec Blog<\/title>\n<meta name=\"description\" content=\"Cheatsheet Redis command t\u1ed5ng h\u1ee3p c\u00e1c l\u1ec7nh Redis t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao: H\u01b0\u1edbng d\u1eabn c\u00fa ph\u00e1p, v\u00ed d\u1ee5 th\u1ef1c t\u1ebf, m\u1eb9o t\u1ed1i \u01b0u \u0111\u1ec3 t\u0103ng t\u1ed1c \u1ee9ng d\u1ee5ng.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5\" \/>\n<meta property=\"og:description\" content=\"Redis l\u00e0 m\u1ed9t h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u in-memory n\u1ed5i b\u1eadt, kh\u00f4ng ch\u1ec9 nh\u1edd hi\u1ec7u n\u0103ng truy xu\u1ea5t c\u1ef1c nhanh m\u00e0 c\u00f2n b\u1edfi kh\u1ea3 n\u0103ng x\u1eed l\u00fd linh ho\u1ea1t v\u1edbi nhi\u1ec1u c\u1ea5u tr\u00fac\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2025-09-01T07:00:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-01T07:21:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/08\/redis-command-scaled.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"421\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"M\u1ef9 Duy\u00ean\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"M\u1ef9 Duy\u00ean\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5 - ITviec Blog","description":"Cheatsheet Redis command t\u1ed5ng h\u1ee3p c\u00e1c l\u1ec7nh Redis t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao: H\u01b0\u1edbng d\u1eabn c\u00fa ph\u00e1p, v\u00ed d\u1ee5 th\u1ef1c t\u1ebf, m\u1eb9o t\u1ed1i \u01b0u \u0111\u1ec3 t\u0103ng t\u1ed1c \u1ee9ng d\u1ee5ng.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/","og_locale":"vi_VN","og_type":"article","og_title":"T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5","og_description":"Redis l\u00e0 m\u1ed9t h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u in-memory n\u1ed5i b\u1eadt, kh\u00f4ng ch\u1ec9 nh\u1edd hi\u1ec7u n\u0103ng truy xu\u1ea5t c\u1ef1c nhanh m\u00e0 c\u00f2n b\u1edfi kh\u1ea3 n\u0103ng x\u1eed l\u00fd linh ho\u1ea1t v\u1edbi nhi\u1ec1u c\u1ea5u tr\u00fac","og_url":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-09-01T07:00:25+00:00","article_modified_time":"2025-09-01T07:21:54+00:00","og_image":[{"width":800,"height":421,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/08\/redis-command-scaled.png","type":"image\/png"}],"author":"M\u1ef9 Duy\u00ean","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"M\u1ef9 Duy\u00ean","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"17 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5","datePublished":"2025-09-01T07:00:25+00:00","dateModified":"2025-09-01T07:21:54+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/"},"wordCount":4480,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/08\/redis-command-scaled.png","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/","url":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/","name":"T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5 - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/08\/redis-command-scaled.png","datePublished":"2025-09-01T07:00:25+00:00","dateModified":"2025-09-01T07:21:54+00:00","description":"Cheatsheet Redis command t\u1ed5ng h\u1ee3p c\u00e1c l\u1ec7nh Redis t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao: H\u01b0\u1edbng d\u1eabn c\u00fa ph\u00e1p, v\u00ed d\u1ee5 th\u1ef1c t\u1ebf, m\u1eb9o t\u1ed1i \u01b0u \u0111\u1ec3 t\u0103ng t\u1ed1c \u1ee9ng d\u1ee5ng.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/tong-hop-redis-command\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/08\/redis-command-scaled.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/08\/redis-command-scaled.png","width":800,"height":421,"caption":"redis command - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/tong-hop-redis-command\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"T\u1ed5ng h\u1ee3p Redis command: Chi ti\u1ebft l\u1ec7nh Redis ph\u1ed5 bi\u1ebfn k\u00e8m v\u00ed d\u1ee5"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7","name":"M\u1ef9 Duy\u00ean","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","caption":"M\u1ef9 Duy\u00ean"},"url":"https:\/\/itviec.com\/blog\/author\/my-duyen\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/90317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/209"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=90317"}],"version-history":[{"count":5,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/90317\/revisions"}],"predecessor-version":[{"id":90874,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/90317\/revisions\/90874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/90866"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=90317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=90317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=90317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}