Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a0618c8
Add server-side hot key detection using the Space-Saving algorithm
alon-arenberg Jul 30, 2026
3dad84f
Merge branch 'unstable' into hot-key-detection-mg
alon-arenberg Aug 4, 2026
d3094f9
hotkey: raise hotkey-window-seconds upper limit to INT_MAX
alon-arenberg Aug 4, 2026
c4b825e
Merge branch 'unstable' into hot-key-detection-mg
alon-arenberg Aug 11, 2026
f163184
fix formatting
alon-arenberg Aug 11, 2026
694e3a5
Merge remote-tracking branch 'origin/unstable' into hot-key-detection-mg
alon-arenberg Aug 12, 2026
29016a6
hotkey: address round-2 review feedback
alon-arenberg Aug 12, 2026
2dbbe3e
hotkey: add serverCron rotation and an explicit enable switch
alon-arenberg Aug 13, 2026
287b241
Merge remote-tracking branch 'origin/unstable' into hot-key-detection-mg
alon-arenberg Aug 13, 2026
c262523
hotkey: align spaceSavingManager comments (clang-format)
alon-arenberg Aug 13, 2026
6370a09
Merge remote-tracking branch 'origin/unstable' into hot-key-detection-mg
alon-arenberg Aug 18, 2026
fcc4ee3
hotkey: make the Space-Saving API concrete, add unit tests, and derivโ€ฆ
alon-arenberg Aug 18, 2026
bd503f3
hotkey: fix comment spacing in getNodeByQuery (clang-format)
alon-arenberg Aug 18, 2026
b21ca1a
reorder documentation
alon-arenberg Aug 18, 2026
2dffc68
hotkey: derive QPS from the window's measured duration
alon-arenberg Aug 19, 2026
1f67cd2
Merge branch 'unstable' into hot-key-detection-mg
alon-arenberg Aug 20, 2026
0b103b0
Merge branch 'unstable' into hot-key-detection-mg
alon-arenberg Aug 23, 2026
56a8581
hotkey: one window clock, and an explicit staleness cutoff on rotate
alon-arenberg Aug 25, 2026
e6c8080
Merge remote-tracking branch 'upstream/unstable' into hot-key-detectiโ€ฆ
alon-arenberg Aug 31, 2026
77bed78
hotkeys: spell the feature HOTKEYS everywhere, add HELP, and move recโ€ฆ
alon-arenberg Aug 31, 2026
e642484
hotkeys: rename the INFO section and its fields to the hotkeys spelling
alon-arenberg Aug 31, 2026
74d2aaf
hotkeys: rename the internal symbols to the hotkeys prefix
alon-arenberg Aug 31, 2026
5b57753
hotkeys: re-align the LOOKUP_* comment block (clang-format)
alon-arenberg Aug 31, 2026
0b0f9af
hotkeys: own the INFO section fields, and rotate from databasesCron
alon-arenberg Sep 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmake/Modules/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ set(VALKEY_SERVER_SRCS
${CMAKE_SOURCE_DIR}/src/queues.c
${CMAKE_SOURCE_DIR}/src/compression.c
${CMAKE_SOURCE_DIR}/src/compression_lz4.c
${CMAKE_SOURCE_DIR}/src/compression_stream.c)
${CMAKE_SOURCE_DIR}/src/compression_stream.c
${CMAKE_SOURCE_DIR}/src/hotkeys.c
${CMAKE_SOURCE_DIR}/src/space_saving.c)


# valkey-cli
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ ENGINE_SERVER_OBJ = \
geohash.o \
geohash_helper.o \
hashtable.o \
hotkeys.o \
space_saving.o \
hyperloglog.o \
intset.o \
io_threads.o \
Expand Down
2 changes: 1 addition & 1 deletion src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ clusterNode *getNodeByQuery(client *c, int *error_code) {
* slot migration, the channel will be served from the source
* node until the migration completes with CLUSTER SETSLOT <slot>
* NODE <node-id>. */
int flags = LOOKUP_NOTOUCH | LOOKUP_NOSTATS | LOOKUP_NONOTIFY | LOOKUP_NOEXPIRE;
int flags = LOOKUP_NOEFFECTS; /* not client key access */
if (!pubsubshard_included &&
(!c->flag.multi || (c->flag.multi && c->cmd->proc == execCommand))) {
/* Multi/Exec validation happens on exec */
Expand Down
7 changes: 7 additions & 0 deletions src/cluster_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/

#include "server.h"
#include "hotkeys.h"
#include "cluster.h"
#include "cluster_legacy.h"
#include "cluster_slot_stats.h"
Expand Down Expand Up @@ -1731,6 +1732,7 @@ void clusterReset(int hard) {
resetManualFailover();

/* Unassign all the slots. */
hotkeysPurgeAll(); /* Bulk purge before individual clusterDelSlot calls */
for (j = 0; j < CLUSTER_SLOTS; j++) clusterDelSlot(j);

/* Recreate shards dict */
Expand Down Expand Up @@ -6726,6 +6728,7 @@ int clusterDelSlot(int slot) {
/* Make owner_not_claiming_slot flag consistent with slot ownership information. */
bitmapClearBit(server.cluster->owner_not_claiming_slot, slot);
clusterSlotStatReset(slot);
hotkeysPurgeSlot(slot);
return C_OK;
}

Expand Down Expand Up @@ -7696,6 +7699,10 @@ unsigned int delKeysInSlot(unsigned int hashslot, int lazy, bool propagate_del,
kvstoreReleaseHashtableIterator(kvs_di);
}

/* The slot's keys have been removed locally (flushed or migrated away), so
* drop their hot-key state too. Sampling was suppressed during the loop via
* server_del_keys_in_slot (see hotkeysShouldRecord). */
hotkeysPurgeSlot(hashslot);
server.server_del_keys_in_slot = 0;
serverAssert(server.execution_nesting == before_execution_nesting);
return j;
Expand Down
77 changes: 77 additions & 0 deletions src/commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -7830,6 +7830,82 @@ struct COMMAND_ARG FLUSHDB_Args[] = {
{MAKE_ARG("flush-type",ARG_TYPE_ONEOF,-1,NULL,NULL,NULL,CMD_ARG_OPTIONAL,2,NULL),.subargs=FLUSHDB_flush_type_Subargs},
};

/********** HOTKEYS GET ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* HOTKEYS GET history */
#define HOTKEYS_GET_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* HOTKEYS GET tips */
#define HOTKEYS_GET_Tips NULL
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* HOTKEYS GET key specs */
#define HOTKEYS_GET_Keyspecs NULL
#endif

/********** HOTKEYS HELP ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* HOTKEYS HELP history */
#define HOTKEYS_HELP_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* HOTKEYS HELP tips */
#define HOTKEYS_HELP_Tips NULL
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* HOTKEYS HELP key specs */
#define HOTKEYS_HELP_Keyspecs NULL
#endif

/********** HOTKEYS RESET ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* HOTKEYS RESET history */
#define HOTKEYS_RESET_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* HOTKEYS RESET tips */
#define HOTKEYS_RESET_Tips NULL
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* HOTKEYS RESET key specs */
#define HOTKEYS_RESET_Keyspecs NULL
#endif

/* HOTKEYS command table */
struct COMMAND_STRUCT HOTKEYS_Subcommands[] = {
{MAKE_CMD("get","Get the hottest keys from the last completed window, ordered by estimated QPS descending","O(N) where N is the number of hot keys in the history","9.2.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,HOTKEYS_GET_History,0,HOTKEYS_GET_Tips,0,hotkeysGetCommand,2,CMD_ADMIN|CMD_LOADING|CMD_STALE,ACL_CATEGORY_ADMIN|ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_SLOW,NULL,HOTKEYS_GET_Keyspecs,0,NULL,0)},
{MAKE_CMD("help","Shows helpful text about the different subcommands","O(1)","9.2.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,HOTKEYS_HELP_History,0,HOTKEYS_HELP_Tips,0,hotkeysHelpCommand,2,CMD_LOADING|CMD_STALE,ACL_CATEGORY_SLOW,NULL,HOTKEYS_HELP_Keyspecs,0,NULL,0)},
{MAKE_CMD("reset","Reset all hot key statistics and history","O(N) where N is the number of keys to remove from history and statistics","9.2.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,HOTKEYS_RESET_History,0,HOTKEYS_RESET_Tips,0,hotkeysResetCommand,2,CMD_ADMIN|CMD_LOADING|CMD_STALE,ACL_CATEGORY_ADMIN|ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_SLOW,NULL,HOTKEYS_RESET_Keyspecs,0,NULL,0)},
{0}
};

/********** HOTKEYS ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* HOTKEYS history */
#define HOTKEYS_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* HOTKEYS tips */
#define HOTKEYS_Tips NULL
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* HOTKEYS key specs */
#define HOTKEYS_Keyspecs NULL
#endif

/********** INFO ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
Expand Down Expand Up @@ -12042,6 +12118,7 @@ struct COMMAND_STRUCT serverCommandTable[] = {
{MAKE_CMD("failover","Starts a coordinated failover from a server to one of its replicas.","O(1)","6.2.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,FAILOVER_History,0,FAILOVER_Tips,0,failoverCommand,-1,CMD_ADMIN|CMD_NOSCRIPT|CMD_STALE,ACL_CATEGORY_ADMIN|ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_SLOW,NULL,FAILOVER_Keyspecs,0,NULL,3),.args=FAILOVER_Args},
{MAKE_CMD("flushall","Removes all keys from all databases.","O(N) where N is the total number of keys in all databases","1.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,FLUSHALL_History,2,FLUSHALL_Tips,2,flushallCommand,-1,CMD_WRITE|CMD_ALL_DBS,ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_KEYSPACE|ACL_CATEGORY_SLOW|ACL_CATEGORY_WRITE,NULL,FLUSHALL_Keyspecs,0,NULL,1),.args=FLUSHALL_Args},
{MAKE_CMD("flushdb","Removes all keys from the current database.","O(N) where N is the number of keys in the selected database","1.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,FLUSHDB_History,2,FLUSHDB_Tips,2,flushdbCommand,-1,CMD_WRITE,ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_KEYSPACE|ACL_CATEGORY_SLOW|ACL_CATEGORY_WRITE,NULL,FLUSHDB_Keyspecs,0,NULL,1),.args=FLUSHDB_Args},
{MAKE_CMD("hotkeys","A container for hot key commands","Depends on subcommand.","9.2.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,HOTKEYS_History,0,HOTKEYS_Tips,0,NULL,-2,0,ACL_CATEGORY_SLOW,NULL,HOTKEYS_Keyspecs,0,NULL,0),.subcommands=HOTKEYS_Subcommands},
{MAKE_CMD("info","Returns information and statistics about the server.","O(1)","1.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,INFO_History,1,INFO_Tips,3,infoCommand,-1,CMD_LOADING|CMD_STALE|CMD_SENTINEL,ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_SLOW,NULL,INFO_Keyspecs,0,NULL,1),.args=INFO_Args},
{MAKE_CMD("lastsave","Returns the Unix timestamp of the last successful save to disk.","O(1)","1.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,LASTSAVE_History,0,LASTSAVE_Tips,1,lastsaveCommand,1,CMD_LOADING|CMD_STALE|CMD_FAST,ACL_CATEGORY_ADMIN|ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_FAST,NULL,LASTSAVE_Keyspecs,0,NULL,0)},
{MAKE_CMD("latency","A container for latency diagnostics commands.","Depends on subcommand.","2.8.13",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,LATENCY_History,0,LATENCY_Tips,0,NULL,-2,0,ACL_CATEGORY_SLOW,NULL,LATENCY_Keyspecs,0,NULL,0),.subcommands=LATENCY_Subcommands},
Expand Down
43 changes: 43 additions & 0 deletions src/commands/hotkeys-get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"GET": {
"summary": "Get the hottest keys from the last completed window, ordered by estimated QPS descending",
"complexity": "O(N) where N is the number of hot keys in the history",
"group": "server",
"since": "9.2.0",
"arity": 2,
"function": "hotkeysGetCommand",
"command_flags": [
"ADMIN",
"LOADING",
"STALE"
],
"acl_categories": [
"ADMIN",
"DANGEROUS",
"SLOW"
],
"container": "HOTKEYS",
"reply_schema": {
"description": "Hottest keys from the last completed window, ordered by estimated QPS (descending)",
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"key": {
"description": "Key name",
"type": "string"
},
"db": {
"description": "Database id",
"type": "integer"
},
"qps": {
"description": "Estimated accesses per second over the last completed window",
"type": "integer"
}
}
}
}
}
}
25 changes: 25 additions & 0 deletions src/commands/hotkeys-help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"HELP": {
"summary": "Shows helpful text about the different subcommands",
"complexity": "O(1)",
"group": "server",
"since": "9.2.0",
"arity": 2,
"container": "HOTKEYS",
"function": "hotkeysHelpCommand",
"command_flags": [
"LOADING",
"STALE"
],
"reply_schema": {
"type": "array",
"description": "Helpful text about subcommands.",
"items": {
"type": "string"
}
},
"acl_categories": [
"SLOW"
]
}
}
24 changes: 24 additions & 0 deletions src/commands/hotkeys-reset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"RESET": {
"summary": "Reset all hot key statistics and history",
"complexity": "O(N) where N is the number of keys to remove from history and statistics",
"group": "server",
"since": "9.2.0",
"arity": 2,
"function": "hotkeysResetCommand",
"command_flags": [
"ADMIN",
"LOADING",
"STALE"
],
"acl_categories": [
"ADMIN",
"DANGEROUS",
"SLOW"
],
"container": "HOTKEYS",
"reply_schema": {
"const": "OK"
}
}
}
12 changes: 12 additions & 0 deletions src/commands/hotkeys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"HOTKEYS": {
Comment thread
alon-arenberg marked this conversation as resolved.
Resolved
"summary": "A container for hot key commands",
"complexity": "Depends on subcommand.",
"group": "server",
"since": "9.2.0",
"arity": -2,
"acl_categories": [
"SLOW"
]
}
}
4 changes: 4 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "io_threads.h"
#include "sds.h"
#include "server.h"
#include "hotkeys.h"
#include "cluster.h"
#include "connection.h"
#include "bio.h"
Expand Down Expand Up @@ -3513,6 +3514,9 @@ standardConfig static_configs[] = {
createIntConfig("rdma-rx-size", NULL, IMMUTABLE_CONFIG, 64 * 1024, 16 * 1024 * 1024, server.rdma_ctx_config.rx_size, 1024 * 1024, INTEGER_CONFIG, NULL, NULL),
createIntConfig("rdma-completion-vector", NULL, IMMUTABLE_CONFIG, -1, 1024, server.rdma_ctx_config.completion_vector, -1, INTEGER_CONFIG, NULL, NULL),
createIntConfig("cluster-message-gossip-perc", NULL, MODIFIABLE_CONFIG | HIDDEN_CONFIG, 1, 100, server.cluster_message_gossip_perc, 10, INTEGER_CONFIG, NULL, NULL),
createIntConfig("hotkeys-sampling-percentage", NULL, MODIFIABLE_CONFIG, 1, 100, server.hotkeys_sampling_percentage, 1, INTEGER_CONFIG, NULL, hotkeysSamplingCallback),
createIntConfig("hotkeys-top-k", NULL, MODIFIABLE_CONFIG, 0, 1000, server.hotkeys_top_k, 0, INTEGER_CONFIG, NULL, hotkeysTopKCallback),
createIntConfig("hotkeys-window-seconds", NULL, MODIFIABLE_CONFIG, 1, 300, server.hotkeys_window_seconds, 1, INTEGER_CONFIG, NULL, hotkeysWindowCallback),

/* Unsigned int configs */
createUIntConfig("maxclients", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, server.maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients),
Expand Down
13 changes: 13 additions & 0 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/

#include "server.h"
#include "hotkeys.h"
#include "ordered_index.h"
#include "cluster.h"
#include "cluster_migrateslots.h"
Expand Down Expand Up @@ -123,6 +124,10 @@ robj *lookupKey(serverDb *db, robj *key, int flags) {
/* TODO: Use separate misses stats and notify event for WRITE */
}

/* Charge this lookup to hot-key detection. All the policy (whether detection
* is on, which lookups count, and sampling) lives in hotkeys.c. */
hotkeysRecordLookup(key, db->id, flags);

return val;
}

Expand Down Expand Up @@ -483,6 +488,7 @@ int dbGenericDeleteWithDictIndex(serverDb *db, robj *key, int async, int flags,
hashtablePosition pos;
void **ref = kvstoreHashtableTwoPhasePopFindRef(db->keys, dict_index, objectGetVal(key), &pos);
if (ref != NULL) {
hotkeysRecordDelete(key, db->id, flags);
robj *val = *ref;
/* VM_StringDMA may call dbUnshareStringValue which may free val, so we
* need to incr to retain val */
Expand Down Expand Up @@ -691,6 +697,13 @@ long long emptyData(int dbnum, int flags, void(callback)(hashtable *)) {
/* Empty the database structure. */
removed = emptyDbStructure(server.db, dbnum, async, callback);

if (hotkeysEnabled()) {
if (dbnum == -1)
hotkeysPurgeAll();
else
hotkeysPurgeDb(dbnum);
}

if (dbnum == -1) flushReplicaKeysWithExpireList(async);

if (with_functions) {
Expand Down
Loading
Loading