SlideShare a Scribd company logo
Redis Modules TL;DR
Dvir Volk,
Senior Architect, Redis Labs
So… Modules, huh?
• I’ve been waiting 6 years
• Will change Redis forever IMO
• The key is the API concept
What Modules Actually Are
• Dynamically loaded libraries into redis
• Extend redis with new commands
• Written in C (C++ if you really insist)
• Using a C API designed to isolate redis internals
The Race For JS Modules is ON!
What Modules Can’t Do (YET)
• Register new data structures *
• Override existing commands *
• Block
• Not the entire API of redis is accessible directly
• All Coming Soon
* Not completely true!
Modules in Action
127.0.0.1:9875> MODULE LOAD example.so
OK
127.0.0.1:9875> HSET foo bar baz
(integer) 1
127.0.0.1:9875> HGETSET foo bar w00t!
"baz"
127.0.0.1:9875> HGETSET foo bar wat?
"w00t!"
The Main API Layers
High Level API
● Generic RedisCall
● Basically Lua on steroids
Operational API
● Compose responses, examine call replies
● Memory Management
Low Level API
● Selected parts of redis commands (L*, Z*)
● String DMA
Main Building Blocks
RedisModuleString*
Memory managed strings
for the module
Opaque to the developer
Args and replies
RedisModuleCallReply*
Returned from most calls
Like the protocol can be:
● Strings
● Arrays
● Integers
● Errors
● Null
RedisModuleKey*
References to keys we
are working with, mainly
for the lower level API
Acquired with Open
Released automatically
or with Close
RedisModuleCtx *
● Redis’ execution context for a command
● Opaque to the module developer
● Passed to most functions
● Behind the scenes manages resources
○ Allocated Objects
○ Opened Keys
○ Automatic free at exit
High Level
API
RedisModuleCallReply *rep =
RedisModule_Call(ctx,
"HGET",
"cl",
"foo",
1337);
High Level API
● More or less like LUA’s API
● Generic RedisModule_Call
● Examine replies with RedisModule_CallReplyType
● Reply to the client with RedisModule_ReplyWith*
● Slower than lower level API, but flexible
Low Level API
● MUCH faster
Low Level API
● MUCH faster
● Hash Get/Set
Low Level API
● MUCH faster
● Hash Get/Set
● ZSET operations
Low Level API
● MUCH faster
● Hash Get/Set
● ZSET operations
● ZSET Iterators
Low Level API
● MUCH faster
● Hash Get/Set
● ZSET operations
● ZSET Iterators
● String DMA / truncate
Low Level API
● MUCH faster
● Hash Get/Set
● ZSET operations
● ZSET Iterators
● String DMA / truncate
● List push/pop
Low Level API
● MUCH faster
● Hash Get/Set
● ZSET operations
● ZSET Iterators
● String DMA / truncate
● List push/pop
● Expire
Low Level API
● MUCH faster
● Hash Get/Set
● ZSET operations
● ZSET Iterators
● String DMA / truncate
● List push/pop
● Expire
● More to come!
String DMA
● Use Redis strings as raw memory
● Zero Copy Overhead
● Resize strings with RedisModule_Truncate
● BYODS - Bring Your Own Data Structure
• Basic Module Template
• Complete Documentation
• Automating Boring Stuff
• Argument Parsing
• String Manipulation
• Response Validation
• Testing
https://github.com/RedisLabs/RedisModulesSDK
Shameless Plug: RedisModulesSDK
Let’s Make a Module!
HGETSET <key> <element> <newval>
Step 1: Command Handler
#include “redismodule.h”
/* HGETSET <key> <element> <value> */
int HGetSetCmd(RedisModuleCtx *ctx,
RedisModuleString **argv, int argc) {
return REDISMODULE_OK;
}
Step 2: validate args
/* HGETSET <key> <element> <value> */
int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString
**argv, int argc) {
if (argc != 4) {
return RedisModule_WrongArity(ctx);
}
return REDISMODULE_OK;
}
Step 3: Auto Memory
/* HGETSET <key> <element> <value> */
int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString
**argv, int argc) {
if (argc != 4) {
return RedisModule_WrongArity(ctx);
}
RedisModule_AutoMemory(ctx);
return REDISMODULE_OK;
}
Step 4: Making Calls!
/* HGETSET <key> <element> <value> */
int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString
**argv, int argc) {
…
RedisModuleCallReply *rep = RedisModule_Call(ctx,
"HGET", "ss", argv[1], argv[2]);
RMUTIL_ASSERT_NOERROR(rep)
…
}
Step 4: MOAR CALLZ PLZ
/* HGETSET <key> <element> <value> */
int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString
**argv, int argc) {
…
RedisModuleCallReply *srep = RedisModule_Call(ctx,
"HSET", "sss", argv[1], argv[2], argv[3]);
REDIS_ASSERT_NOERROR(srep)
return REDISMODULE_OK;
}
Step 5: Returning a reply
/* HGETSET <key> <element> <value> */
int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString
**argv, int argc) {
…
RedisModule_ReplyWithCallReply(ctx, rep);
return REDISMODULE_OK;
}
Step 6: Initializing
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
if (RedisModule_Init(ctx,"EXAMPLE",1,REDISMODULE_APIVER_1)
== REDISMODULE_ERR) return REDISMODULE_ERR;
if (RedisModule_CreateCmd(ctx,"HGETSET",
HGetSetCommand, “write”, 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
return REDISMODULE_OK;
}
Step 6: Initializing
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
if (RedisModule_Init(ctx,"EXAMPLE",1,REDISMODULE_APIVER_1)
== REDISMODULE_ERR) return REDISMODULE_ERR;
if (RedisModule_CreateCmd(ctx,"HGETSET",
HGetSetCommand, “write”, 1, 1, 1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
return REDISMODULE_OK;
}
• No special Linking required
• Just #include “redismodule.h”
Building and Makefile
CFLAGS = -Wall -fPIC
LDFLAGS = -shared -Bsymbolic -lc
module.so: module.o
$(LD) -o $@ module.o $(LDFLAGS)
IT WORKS!
127.0.0.1:9875> MODULE LOAD example.so
OK
127.0.0.1:9875> HSET foo bar baz
(integer) 1
127.0.0.1:9875> HGETSET foo bar w00t!
"baz"
127.0.0.1:9875> HGETSET foo bar wat?
"w00t!"
The Low Level API
Low Level API: Example
/* ZSUMRANGE <key> <startscore> <endscore> */
int ZsumRange_RedisCommand(RedisModuleCtx *ctx,
RedisModuleString **argv, int argc) {
return REDISMODULE_OK;
}
Low Level API: Read Arguments
int ZsumRange_RedisCommand(...) {
double min, max;
double sum = 0;
if (MUtil_ParseArgs(argv, argc, 2, "dd", &min, &max)
!= REDISMODULE_OK) {
RedisModule_WrongArity(ctx);
}
...
}
Low Level API: Open The Key
int ZsumRange_RedisCommand(...) {
...
RedisModuleKey *k =
RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ);
if (RedisModule_KeyType(k) != REDISMODULE_KEYTYPE_ZSET){
return RedisModule_ReplyWithError(...);
}
}
Low Level API: Iterate ZSET
int ZsumRange_RedisCommand(...) {
...
// Open The iterator
RedisModule_ZsetFirstInScoreRange(k, min, max, 0, 0);
while(!RedisModule_ZsetRangeEndReached(k)) {
...
RedisModule_ZsetRangeNext(k);
}
RedisModule_ZsetRangeStop(k);
Low Level API: Read Iterator Values
while(!RedisModule_ZsetRangeEndReached(k)) {
double score;
RedisModuleString *ele =
RedisModule_ZsetRangeCurrentElement(k, &score);
RedisModule_FreeString(ctx, ele);
sum += score;
RedisModule_ZsetRangeNext(k);
}
Low Level API: Return the value
/* ZSUMRANGE key startscore endscore */
int ZsumRange_RedisCommand(RedisModuleCtx *ctx,
RedisModuleString **argv, int argc) {
...
RedisModule_CloseKey(key);
RedisModule_ReplyWithDouble(ctx, sum);
return REDISMODULE_OK;
}
A Little Benchmark
● Sum the scores of 1,000,000 ZSET Elements
● Python client, Lua script, High/Low Level Modules
● Low Level API uses iterators
● The rest use ZRANGEBYSCORE
Benchmark: Results (seconds)
KTHXBAI!

More Related Content

PPTX
Introduction to Redis
PDF
Introduction to redis - version 2
PDF
Extend Redis with Modules
PDF
Tales Of The Black Knight - Keeping EverythingMe running
PPTX
Caching solutions with Redis
KEY
Redis overview for Software Architecture Forum
KEY
Building Scalable, Distributed Job Queues with Redis and Redis::Client
PDF
Introduction to Redis
Introduction to Redis
Introduction to redis - version 2
Extend Redis with Modules
Tales Of The Black Knight - Keeping EverythingMe running
Caching solutions with Redis
Redis overview for Software Architecture Forum
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Introduction to Redis

What's hot (20)

ODP
An Introduction to REDIS NoSQL database
ODP
Introduction to Redis
PPTX
PDF
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
KEY
Scaling php applications with redis
PDF
Boosting Machine Learning with Redis Modules and Spark
PDF
Background Tasks in Node - Evan Tahler, TaskRabbit
PDF
Everything you always wanted to know about Redis but were afraid to ask
PPTX
A simple introduction to redis
PDF
Redis 101
PDF
Centralized + Unified Logging
PPT
Redis in Practice: Scenarios, Performance and Practice with PHP
PDF
Ceph BlueStore - новый тип хранилища в Ceph / Максим Воронцов, (Redsys)
PPTX
Redis Indices (#RedisTLV)
PDF
PostgreSQL and Redis - talk at pgcon 2013
PPT
Introduction to redis
PDF
Redis — The AK-47 of Post-relational Databases
PDF
What's new in Redis v3.2
PPTX
Redis and it's data types
PPTX
Redis/Lessons learned
An Introduction to REDIS NoSQL database
Introduction to Redis
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Scaling php applications with redis
Boosting Machine Learning with Redis Modules and Spark
Background Tasks in Node - Evan Tahler, TaskRabbit
Everything you always wanted to know about Redis but were afraid to ask
A simple introduction to redis
Redis 101
Centralized + Unified Logging
Redis in Practice: Scenarios, Performance and Practice with PHP
Ceph BlueStore - новый тип хранилища в Ceph / Максим Воронцов, (Redsys)
Redis Indices (#RedisTLV)
PostgreSQL and Redis - talk at pgcon 2013
Introduction to redis
Redis — The AK-47 of Post-relational Databases
What's new in Redis v3.2
Redis and it's data types
Redis/Lessons learned
Ad

Viewers also liked (20)

PDF
Redis High availability and fault tolerance in a multitenant environment
PPTX
Redis data modeling examples
PPTX
Redis Use Patterns (DevconTLV June 2014)
PDF
Kicking ass with redis
PDF
HIgh Performance Redis- Tague Griffith, GoPro
PPTX
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
PPTX
Why Your MongoDB Needs Redis
PDF
Ha of load balancer
PDF
Couchdb and me
PDF
Mysql HandleSocket技术在SNS Feed存储中的应用
PDF
Ooredis
PPTX
Ocean base海量结构化数据存储系统 hadoop in china
PDF
Consistency Models in New Generation Databases
PPT
8 minute MongoDB tutorial slide
ODP
Consistency in Distributed Systems
PDF
SDEC2011 NoSQL Data modelling
PDF
Big Challenges in Data Modeling: NoSQL and Data Modeling
PPT
skip list
PDF
Build a Geospatial App with Redis 3.2- Andrew Bass, Sean Yesmunt, Sergio Prad...
PDF
Getting Started with Redis
Redis High availability and fault tolerance in a multitenant environment
Redis data modeling examples
Redis Use Patterns (DevconTLV June 2014)
Kicking ass with redis
HIgh Performance Redis- Tague Griffith, GoPro
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
Why Your MongoDB Needs Redis
Ha of load balancer
Couchdb and me
Mysql HandleSocket技术在SNS Feed存储中的应用
Ooredis
Ocean base海量结构化数据存储系统 hadoop in china
Consistency Models in New Generation Databases
8 minute MongoDB tutorial slide
Consistency in Distributed Systems
SDEC2011 NoSQL Data modelling
Big Challenges in Data Modeling: NoSQL and Data Modeling
skip list
Build a Geospatial App with Redis 3.2- Andrew Bass, Sean Yesmunt, Sergio Prad...
Getting Started with Redis
Ad

Similar to Redis modules 101 (20)

PPTX
Developing a Redis Module - Hackathon Kickoff
PDF
Redis Modules API - an introduction
PDF
Redispresentation apac2012
PPTX
PDF
Tuga IT 2017 - Redis
PDF
How I Implemented the #1 Requested Feature In Redis In Less than 1 Hour with ...
PDF
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
PDF
Paris Redis Meetup Introduction
PPT
Python redis talk
ODP
Redis - from client to execution
PDF
Redis - Usability and Use Cases
PDF
quickguide-einnovator-9-redis
PDF
A Brief Introduction to Redis
PDF
Fun with Ruby and Redis
PPTX
Get more than a cache back! - ConFoo Montreal
PDF
An Introduction to Redis for Developers.pdf
PPT
Redis And python at pycon_2011
PDF
An Introduction to Redis for .NET Developers.pdf
PPTX
RediSearch Mumbai Meetup 2020
Developing a Redis Module - Hackathon Kickoff
Redis Modules API - an introduction
Redispresentation apac2012
Tuga IT 2017 - Redis
How I Implemented the #1 Requested Feature In Redis In Less than 1 Hour with ...
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
Paris Redis Meetup Introduction
Python redis talk
Redis - from client to execution
Redis - Usability and Use Cases
quickguide-einnovator-9-redis
A Brief Introduction to Redis
Fun with Ruby and Redis
Get more than a cache back! - ConFoo Montreal
An Introduction to Redis for Developers.pdf
Redis And python at pycon_2011
An Introduction to Redis for .NET Developers.pdf
RediSearch Mumbai Meetup 2020

Recently uploaded (20)

PDF
System and Network Administration Chapter 2
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Nekopoi APK 2025 free lastest update
PDF
Digital Strategies for Manufacturing Companies
PPTX
ai tools demonstartion for schools and inter college
PDF
top salesforce developer skills in 2025.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
history of c programming in notes for students .pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administration Chapter 2
ISO 45001 Occupational Health and Safety Management System
Wondershare Filmora 15 Crack With Activation Key [2025
ManageIQ - Sprint 268 Review - Slide Deck
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Which alternative to Crystal Reports is best for small or large businesses.pdf
How Creative Agencies Leverage Project Management Software.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Odoo Companies in India – Driving Business Transformation.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Nekopoi APK 2025 free lastest update
Digital Strategies for Manufacturing Companies
ai tools demonstartion for schools and inter college
top salesforce developer skills in 2025.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Online Work Permit System for Fast Permit Processing
history of c programming in notes for students .pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...

Redis modules 101

  • 1. Redis Modules TL;DR Dvir Volk, Senior Architect, Redis Labs
  • 2. So… Modules, huh? • I’ve been waiting 6 years • Will change Redis forever IMO • The key is the API concept
  • 3. What Modules Actually Are • Dynamically loaded libraries into redis • Extend redis with new commands • Written in C (C++ if you really insist) • Using a C API designed to isolate redis internals
  • 4. The Race For JS Modules is ON!
  • 5. What Modules Can’t Do (YET) • Register new data structures * • Override existing commands * • Block • Not the entire API of redis is accessible directly • All Coming Soon * Not completely true!
  • 6. Modules in Action 127.0.0.1:9875> MODULE LOAD example.so OK 127.0.0.1:9875> HSET foo bar baz (integer) 1 127.0.0.1:9875> HGETSET foo bar w00t! "baz" 127.0.0.1:9875> HGETSET foo bar wat? "w00t!"
  • 7. The Main API Layers High Level API ● Generic RedisCall ● Basically Lua on steroids Operational API ● Compose responses, examine call replies ● Memory Management Low Level API ● Selected parts of redis commands (L*, Z*) ● String DMA
  • 8. Main Building Blocks RedisModuleString* Memory managed strings for the module Opaque to the developer Args and replies RedisModuleCallReply* Returned from most calls Like the protocol can be: ● Strings ● Arrays ● Integers ● Errors ● Null RedisModuleKey* References to keys we are working with, mainly for the lower level API Acquired with Open Released automatically or with Close
  • 9. RedisModuleCtx * ● Redis’ execution context for a command ● Opaque to the module developer ● Passed to most functions ● Behind the scenes manages resources ○ Allocated Objects ○ Opened Keys ○ Automatic free at exit
  • 10. High Level API RedisModuleCallReply *rep = RedisModule_Call(ctx, "HGET", "cl", "foo", 1337);
  • 11. High Level API ● More or less like LUA’s API ● Generic RedisModule_Call ● Examine replies with RedisModule_CallReplyType ● Reply to the client with RedisModule_ReplyWith* ● Slower than lower level API, but flexible
  • 12. Low Level API ● MUCH faster
  • 13. Low Level API ● MUCH faster ● Hash Get/Set
  • 14. Low Level API ● MUCH faster ● Hash Get/Set ● ZSET operations
  • 15. Low Level API ● MUCH faster ● Hash Get/Set ● ZSET operations ● ZSET Iterators
  • 16. Low Level API ● MUCH faster ● Hash Get/Set ● ZSET operations ● ZSET Iterators ● String DMA / truncate
  • 17. Low Level API ● MUCH faster ● Hash Get/Set ● ZSET operations ● ZSET Iterators ● String DMA / truncate ● List push/pop
  • 18. Low Level API ● MUCH faster ● Hash Get/Set ● ZSET operations ● ZSET Iterators ● String DMA / truncate ● List push/pop ● Expire
  • 19. Low Level API ● MUCH faster ● Hash Get/Set ● ZSET operations ● ZSET Iterators ● String DMA / truncate ● List push/pop ● Expire ● More to come!
  • 20. String DMA ● Use Redis strings as raw memory ● Zero Copy Overhead ● Resize strings with RedisModule_Truncate ● BYODS - Bring Your Own Data Structure
  • 21. • Basic Module Template • Complete Documentation • Automating Boring Stuff • Argument Parsing • String Manipulation • Response Validation • Testing https://github.com/RedisLabs/RedisModulesSDK Shameless Plug: RedisModulesSDK
  • 22. Let’s Make a Module! HGETSET <key> <element> <newval>
  • 23. Step 1: Command Handler #include “redismodule.h” /* HGETSET <key> <element> <value> */ int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { return REDISMODULE_OK; }
  • 24. Step 2: validate args /* HGETSET <key> <element> <value> */ int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 4) { return RedisModule_WrongArity(ctx); } return REDISMODULE_OK; }
  • 25. Step 3: Auto Memory /* HGETSET <key> <element> <value> */ int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 4) { return RedisModule_WrongArity(ctx); } RedisModule_AutoMemory(ctx); return REDISMODULE_OK; }
  • 26. Step 4: Making Calls! /* HGETSET <key> <element> <value> */ int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { … RedisModuleCallReply *rep = RedisModule_Call(ctx, "HGET", "ss", argv[1], argv[2]); RMUTIL_ASSERT_NOERROR(rep) … }
  • 27. Step 4: MOAR CALLZ PLZ /* HGETSET <key> <element> <value> */ int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { … RedisModuleCallReply *srep = RedisModule_Call(ctx, "HSET", "sss", argv[1], argv[2], argv[3]); REDIS_ASSERT_NOERROR(srep) return REDISMODULE_OK; }
  • 28. Step 5: Returning a reply /* HGETSET <key> <element> <value> */ int HGetSetCmd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { … RedisModule_ReplyWithCallReply(ctx, rep); return REDISMODULE_OK; }
  • 29. Step 6: Initializing int RedisModule_OnLoad(RedisModuleCtx *ctx) { if (RedisModule_Init(ctx,"EXAMPLE",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR) return REDISMODULE_ERR; if (RedisModule_CreateCmd(ctx,"HGETSET", HGetSetCommand, “write”, 1, 1, 1) == REDISMODULE_ERR) return REDISMODULE_ERR; return REDISMODULE_OK; }
  • 30. Step 6: Initializing int RedisModule_OnLoad(RedisModuleCtx *ctx) { if (RedisModule_Init(ctx,"EXAMPLE",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR) return REDISMODULE_ERR; if (RedisModule_CreateCmd(ctx,"HGETSET", HGetSetCommand, “write”, 1, 1, 1) == REDISMODULE_ERR) return REDISMODULE_ERR; return REDISMODULE_OK; }
  • 31. • No special Linking required • Just #include “redismodule.h” Building and Makefile CFLAGS = -Wall -fPIC LDFLAGS = -shared -Bsymbolic -lc module.so: module.o $(LD) -o $@ module.o $(LDFLAGS)
  • 32. IT WORKS! 127.0.0.1:9875> MODULE LOAD example.so OK 127.0.0.1:9875> HSET foo bar baz (integer) 1 127.0.0.1:9875> HGETSET foo bar w00t! "baz" 127.0.0.1:9875> HGETSET foo bar wat? "w00t!"
  • 34. Low Level API: Example /* ZSUMRANGE <key> <startscore> <endscore> */ int ZsumRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { return REDISMODULE_OK; }
  • 35. Low Level API: Read Arguments int ZsumRange_RedisCommand(...) { double min, max; double sum = 0; if (MUtil_ParseArgs(argv, argc, 2, "dd", &min, &max) != REDISMODULE_OK) { RedisModule_WrongArity(ctx); } ... }
  • 36. Low Level API: Open The Key int ZsumRange_RedisCommand(...) { ... RedisModuleKey *k = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ); if (RedisModule_KeyType(k) != REDISMODULE_KEYTYPE_ZSET){ return RedisModule_ReplyWithError(...); } }
  • 37. Low Level API: Iterate ZSET int ZsumRange_RedisCommand(...) { ... // Open The iterator RedisModule_ZsetFirstInScoreRange(k, min, max, 0, 0); while(!RedisModule_ZsetRangeEndReached(k)) { ... RedisModule_ZsetRangeNext(k); } RedisModule_ZsetRangeStop(k);
  • 38. Low Level API: Read Iterator Values while(!RedisModule_ZsetRangeEndReached(k)) { double score; RedisModuleString *ele = RedisModule_ZsetRangeCurrentElement(k, &score); RedisModule_FreeString(ctx, ele); sum += score; RedisModule_ZsetRangeNext(k); }
  • 39. Low Level API: Return the value /* ZSUMRANGE key startscore endscore */ int ZsumRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { ... RedisModule_CloseKey(key); RedisModule_ReplyWithDouble(ctx, sum); return REDISMODULE_OK; }
  • 40. A Little Benchmark ● Sum the scores of 1,000,000 ZSET Elements ● Python client, Lua script, High/Low Level Modules ● Low Level API uses iterators ● The rest use ZRANGEBYSCORE