SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Writing
Redis Module
with
Rust
Writing
Critical Systems
ina
distractedworld
Database isaCriticalSystem
» Single source of truth
» Scaling is hard
» Requires high performance
» Must be stable
Adistractedworld
Typical Web dev:
» 95% business logic: Ruby/Python/Javascript
» 5% optimization
» Mean time between optimization: 3+ months
Poga
» github.com/poga
» poga.po@gmail.com
» Consultant
» Web, Distributed System, P2P, Decentralization
Redis
» Fast, In-Memory Database
» Simple to Reason About
» NoSQL/Cache/Message Broker
» Cluster/Replication
» Extensible with Redis Module API
Redis
Redis is being used as
» Atomic Lock
» Cache
» Message Broker
» Big Data + AI
...requires safe, stable, and efficient
implementation
Redis Module
» Move Code to Data
» Free Redis Protocol
» Free Memory management/Serialization
» Free Replication/Clustering
» C API
CAPI is scary
» Memory Safety?
» Undefined Behaviour?
» Dependency? Include?
You need Focus
Towrite safe C code
...butwe onlyhave
5% Focus
Rust
» borrow checker: a smart and severe mentor
» cargo: simple dependency management
» Stable release cycle
The system won't collapse when you reopen it
3 month later
rustc
is never
distracted
WritingaRedis Module
build our own lib from redismodule.h
Redis doesn't provide a lib, only a header file
#include "redismodule.h"
int Export_RedisModule_Init(RedisModuleCtx *ctx,
const char *name,
int ver,
int apiver) {
return RedisModule_Init(ctx, name, ver, apiver);
}
build our own lib from redismodule.h
build.rs: Compile C/C++ code into a Rust lib
extern crate gcc;
fn main() {
gcc::Config::new()
.file("src/redismodule.c")
.include("include/")
.compile("libredismodule.a");
}
Static linking
#[derive(Clone, Copy)]
#[repr(C)]
pub struct RedisModuleCtx;
#[allow(improper_ctypes)]
#[link(name="redismodule", kind="static")]
extern "C" {
pub fn Export_RedisModule_Init(ctx: *mut RedisModuleCtx,
modulename: *const u8,
module_version: c_int,
api_version: c_int)
-> Status;
// ...
}
Free resourcewith Droptrait
» RedisModuleString: string type provided by Redis
» Need to be explicitly freed when you're done.
pub struct RedisString {
ctx: *mut raw::RedisModuleCtx,
str_inner: *mut raw::RedisModuleString,
}
Free resourcewith Droptrait
impl RedisString {
fn create(ctx: *mut raw::RedisModuleCtx, s: &str) -> RedisString {
let str_inner = raw::create_string(ctx, format!("{}0", s).as_ptr(), s.len());
// ...
}
}
impl Drop for RedisString {
// Frees resources appropriately as a RedisString goes out of scope.
fn drop(&mut self) {
raw::free_string(self.ctx, self.str_inner);
}
}
Function Pointer
typedef int (*RedisModuleCmdFunc)
(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
int REDISMODULE_API_FUNC(
RedisModule_CreateCommand
)(..., RedisModuleCmdFunc cmdfunc, ...)
Function Pointer
Use Option
pub type RedisModuleCmdFunc = extern "C" fn(
ctx: *mut RedisModuleCtx,
argv: *mut *mut RedisModuleString,
argc: c_int)
-> Status;
static RedisModule_CreateCommand: extern "C" fn(
...,
cmdfunc: Option<RedisModuleCmdFunc>,
...)
Performance
“Informal benchmarks show that redis-cell is pretty
fast, taking a little under twice as long to run as a
basic Redis SET”
https://github.com/brandur/redis-cell
Recap
Redis Module:
» Free Protocol
» Free Performance
» Free Serialization
» Free Replication
Rust:
write safe code
inadistractedworld
Developer:
Focus
on
Business Logic
and
Happiness
be
Fearless
References
» Redis-cell: github.com/brandur/redis-cell
» rate-limiting
» Redis-rating: github.com/poga/redis-rating
» average rating
Thanks

Contenu connexe

Tendances

Zero to 1 Billion+ Records: A True Story of Learning & Scaling GameChanger
Zero to 1 Billion+ Records: A True Story of Learning & Scaling GameChangerZero to 1 Billion+ Records: A True Story of Learning & Scaling GameChanger
Zero to 1 Billion+ Records: A True Story of Learning & Scaling GameChanger
MongoDB
 
Александр Сергиенко, Senior Android Developer, DataArt
Александр Сергиенко, Senior Android Developer, DataArtАлександр Сергиенко, Senior Android Developer, DataArt
Александр Сергиенко, Senior Android Developer, DataArt
Alina Vilk
 

Tendances (20)

Amethyst: Past, Present, Future
Amethyst: Past, Present, FutureAmethyst: Past, Present, Future
Amethyst: Past, Present, Future
 
DaNode - A home made web server in D
DaNode - A home made web server in DDaNode - A home made web server in D
DaNode - A home made web server in D
 
Ceph BlueStore - новый тип хранилища в Ceph / Максим Воронцов, (Redsys)
Ceph BlueStore - новый тип хранилища в Ceph / Максим Воронцов, (Redsys)Ceph BlueStore - новый тип хранилища в Ceph / Максим Воронцов, (Redsys)
Ceph BlueStore - новый тип хранилища в Ceph / Максим Воронцов, (Redsys)
 
Object Storage with Gluster
Object Storage with GlusterObject Storage with Gluster
Object Storage with Gluster
 
Travis
TravisTravis
Travis
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
 
«Scrapy internals» Александр Сибиряков, Scrapinghub
«Scrapy internals» Александр Сибиряков, Scrapinghub«Scrapy internals» Александр Сибиряков, Scrapinghub
«Scrapy internals» Александр Сибиряков, Scrapinghub
 
Java clients for elasticsearch
Java clients for elasticsearchJava clients for elasticsearch
Java clients for elasticsearch
 
Redis
RedisRedis
Redis
 
Gitbase, SQL interface to Git repositories
Gitbase, SQL interface to Git repositoriesGitbase, SQL interface to Git repositories
Gitbase, SQL interface to Git repositories
 
Fluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker containerFluentd and Docker - running fluentd within a docker container
Fluentd and Docker - running fluentd within a docker container
 
Zero to 1 Billion+ Records: A True Story of Learning & Scaling GameChanger
Zero to 1 Billion+ Records: A True Story of Learning & Scaling GameChangerZero to 1 Billion+ Records: A True Story of Learning & Scaling GameChanger
Zero to 1 Billion+ Records: A True Story of Learning & Scaling GameChanger
 
Redis Overview
Redis OverviewRedis Overview
Redis Overview
 
ScalaCache: simple caching in Scala
ScalaCache: simple caching in ScalaScalaCache: simple caching in Scala
ScalaCache: simple caching in Scala
 
Restinio - header-only http and websocket server
Restinio - header-only http and websocket serverRestinio - header-only http and websocket server
Restinio - header-only http and websocket server
 
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
Redis & MongoDB: Stop Big Data Indigestion Before It StartsRedis & MongoDB: Stop Big Data Indigestion Before It Starts
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
 
Александр Сергиенко, Senior Android Developer, DataArt
Александр Сергиенко, Senior Android Developer, DataArtАлександр Сергиенко, Senior Android Developer, DataArt
Александр Сергиенко, Senior Android Developer, DataArt
 
Gatling.for.load.and.performance.testing
Gatling.for.load.and.performance.testingGatling.for.load.and.performance.testing
Gatling.for.load.and.performance.testing
 
Nginx monitoring with graphite
Nginx monitoring with graphiteNginx monitoring with graphite
Nginx monitoring with graphite
 
Caching in Docker - the hardest thing in computer science
Caching in Docker - the hardest thing in computer scienceCaching in Docker - the hardest thing in computer science
Caching in Docker - the hardest thing in computer science
 

Similaire à Writing Redis Module with Rust

Writing Redis Modules In Rust: Gavrie Philipson
Writing Redis Modules In Rust: Gavrie PhilipsonWriting Redis Modules In Rust: Gavrie Philipson
Writing Redis Modules In Rust: Gavrie Philipson
Redis Labs
 
Legacy of Void*
Legacy of Void*Legacy of Void*
Legacy of Void*
Adam Crain
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
meharikiros2
 

Similaire à Writing Redis Module with Rust (20)

Developing a Redis Module - Hackathon Kickoff
 Developing a Redis Module - Hackathon Kickoff Developing a Redis Module - Hackathon Kickoff
Developing a Redis Module - Hackathon Kickoff
 
Writing Redis Modules In Rust: Gavrie Philipson
Writing Redis Modules In Rust: Gavrie PhilipsonWriting Redis Modules In Rust: Gavrie Philipson
Writing Redis Modules In Rust: Gavrie Philipson
 
Legacy of Void*
Legacy of Void*Legacy of Void*
Legacy of Void*
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
Developing with the Go client for Apache Kafka
Developing with the Go client for Apache KafkaDeveloping with the Go client for Apache Kafka
Developing with the Go client for Apache Kafka
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with Modules
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012
 
C++ Windows Forms L01 - Intro
C++ Windows Forms L01 - IntroC++ Windows Forms L01 - Intro
C++ Windows Forms L01 - Intro
 
Deploying Machine Learning Models to Production
Deploying Machine Learning Models to ProductionDeploying Machine Learning Models to Production
Deploying Machine Learning Models to Production
 
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexusMicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 
Writing a Gem with native extensions
Writing a Gem with native extensionsWriting a Gem with native extensions
Writing a Gem with native extensions
 
.NET6.pptx
.NET6.pptx.NET6.pptx
.NET6.pptx
 
I Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other ThingsI Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other Things
 
Bounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise EnvironmentBounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise Environment
 

Plus de Poga Po

Plus de Poga Po (7)

g0v.social:分散式社群媒體的四年旅程
g0v.social:分散式社群媒體的四年旅程g0v.social:分散式社群媒體的四年旅程
g0v.social:分散式社群媒體的四年旅程
 
Running a Service in Production without Losing Your Sanity
Running a Service in Production without Losing Your SanityRunning a Service in Production without Losing Your Sanity
Running a Service in Production without Losing Your Sanity
 
Wtt#20
Wtt#20Wtt#20
Wtt#20
 
萬事萬物皆是 LOG - 系統架構也來點科普
萬事萬物皆是 LOG - 系統架構也來點科普萬事萬物皆是 LOG - 系統架構也來點科普
萬事萬物皆是 LOG - 系統架構也來點科普
 
Full-stack go with GopherJS
Full-stack go with GopherJSFull-stack go with GopherJS
Full-stack go with GopherJS
 
聽說 KKTIX 都是用 Go 寫的 - ModernWeb 2015
聽說 KKTIX 都是用 Go 寫的 - ModernWeb 2015聽說 KKTIX 都是用 Go 寫的 - ModernWeb 2015
聽說 KKTIX 都是用 Go 寫的 - ModernWeb 2015
 
Gtg12
Gtg12Gtg12
Gtg12
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 

Writing Redis Module with Rust