Redis modules provide a powerful API that allows you to extend Redis with new data types, commands, and functionality. The modules API enables developers to write modules in C that integrate seamlessly with the Redis core.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/redis/redis/llms.txt
Use this file to discover all available pages before exploring further.
What are Redis Modules?
Redis modules are dynamic libraries that can be loaded into Redis at runtime to extend its functionality. Modules can:- Define new data types with custom encoding, persistence, and replication
- Register new commands that operate on native or custom data types
- Subscribe to keyspace events and server events
- Implement complex operations with access to Redis internals
- Share APIs with other modules
Module Architecture
Every module must implement theRedisModule_OnLoad entry point function, which is called when the module is loaded:
Core API Components
The Redis modules API is defined insrc/redismodule.h and provides several key components:
Context Management
- RedisModuleCtx: The context object passed to module commands and callbacks
- Thread-safe contexts for background operations
- Auto-memory management for temporary allocations
Memory Management
String Operations
Key Access
REDISMODULE_READ- Open key for readingREDISMODULE_WRITE- Open key for writingREDISMODULE_OPEN_KEY_NOTOUCH- Don’t update LRU/LFUREDISMODULE_OPEN_KEY_NOEXPIRE- Don’t delete expired keys
Reply Functions
API Constants
Key return values:REDISMODULE_OK(0) - SuccessREDISMODULE_ERR(1) - Error
REDISMODULE_KEYTYPE_EMPTYREDISMODULE_KEYTYPE_STRINGREDISMODULE_KEYTYPE_LISTREDISMODULE_KEYTYPE_HASHREDISMODULE_KEYTYPE_SETREDISMODULE_KEYTYPE_ZSETREDISMODULE_KEYTYPE_MODULEREDISMODULE_KEYTYPE_STREAM
Context Flags
TheRedisModule_GetContextFlags() function returns flags indicating the execution environment:
REDISMODULE_CTX_FLAGS_LUA- Running inside Lua scriptREDISMODULE_CTX_FLAGS_MULTI- Inside transactionREDISMODULE_CTX_FLAGS_MASTER- Instance is masterREDISMODULE_CTX_FLAGS_SLAVE- Instance is replicaREDISMODULE_CTX_FLAGS_READONLY- Instance is read-onlyREDISMODULE_CTX_FLAGS_CLUSTER- Running in cluster modeREDISMODULE_CTX_FLAGS_AOF- AOF is enabledREDISMODULE_CTX_FLAGS_RDB- RDB is enabledREDISMODULE_CTX_FLAGS_LOADING- Redis is loading data
Event Subscriptions
Modules can subscribe to server events:- Replication role changes
- Persistence operations (RDB/AOF)
- Database flush
- Client connections/disconnections
- Key modifications
- Loading progress
Logging
REDISMODULE_LOGLEVEL_DEBUGREDISMODULE_LOGLEVEL_VERBOSEREDISMODULE_LOGLEVEL_NOTICEREDISMODULE_LOGLEVEL_WARNING
Next Steps
Getting Started
Learn how to create your first Redis module
Custom Data Types
Implement custom data types with persistence
Module Commands
Register and implement module commands
Built-in Modules
Explore JSON, Search, TimeSeries, and Bloom modules
API Reference
For the complete API reference, see:- Module API documentation
- Source:
src/redismodule.hin the Redis repository