SlideShare a Scribd company logo
Comredis
Iuri Fernandes
Redis, Elixir, Ruby, Metaprogramming,
Testing and other Buzzwords
Ruby
require "redis"
redis = Redis.new
redis.set("hello ", "world") # => "OK"
redis.get("hello") # => "world"
Connection
management
Authentication
Communication
(Request-response)
Client
RESP
Server
“Requests are sent from the client to
the Redis server as arrays of strings
representing the arguments of the
command to execute.”
–Redis documentation
RESP
(REdis Serialization Protocol)
Ruby
# Get the value of a key.
#
# @param [String] key
# @return [String]
def get(key)
synchronize do |client|
client.call([:get, key])
end
end
https://github.com/redis/redis-rb/blob/master/lib/
redis.rb
Comredis
Elixir
{:ok, conn} = Redix.start_link
# => {:ok, #PID<0.310.0>}
Redix.command(conn, ~w(SET hello world))
# => {:ok, "OK"}
Redix.command(conn, ~w(GET hello))
# => {:ok, "world"}
Elixir
Redix.pipeline(conn, [
~w(INCR foo), ~w(INCR foo), ~w(INCR foo 2)
])
#=> {:ok, [1, 2, 4]}
Elixir
Redix.pipeline(conn, [
~w(INCR foo), ~w(INCR foo), ~w(INCRBY foo 2)
])
#=> {:ok, [1, 2, 4]}
Comredis
Ruby
redis = Redis.new
redis.set("hello ", "world") # => "OK"
redis.get("hello") # => "world"
Elixir
{:ok, conn} = Redix.start_link
# => {:ok, #PID<0.310.0>}
Redix.command(conn, ~w(SET hello world))
# => {:ok, "OK"}
Redix.command(conn, ~w(GET hello))
# => {:ok, "world"}
Elixir
{:ok, conn} = Redix.start_link
# => {:ok, #PID<0.310.0>}
Redix.command(conn, set("hello", "world")))
# => {:ok, "OK"}
Redix.command(conn, get("hello"))
# => {:ok, "world"}
Elixir
{:ok, conn} = Redix.start_link
# => {:ok, #PID<0.310.0>}
conn |> Redix.command(set("hello", "world")))
# => {:ok, "OK"}
conn |> Redix.command(get("hello"))
# => {:ok, "world"}
Elixir
defmodule Commands do
def get(key) do
~w(GET #{key})
end
def set(key, value) do
~w(SET #{key} #{value})
end
# ...
end
Bad idea!
lib/redis.rb =>
2730

LOC*
* Includes a lot of documentation
* Handles responses
* It does not support all the commands
Not for the redis-rb
Imagine if I could generate all these
functions automatically!
You can!
Metaprogramming
FTW!
Better, at compile
time!
{
...
"GET": {
"summary": "Get the value of a key",
"complexity": "O(1)",
"arguments": [
{
"name": "key",
"type": "key"
}
],
"since": "1.0.0",
"group": "string"
},
...
}
https://github.com/antirez/redis-doc/blob/master/commands.json
Parser
(Poison)
Commands
JSON
specification
Command
structured
data
Elixir
macros
Functions
Macro rules
Rule 1: Don’t Write Macros
Rule 2: Use Macros
Gratuitously
Elixir
defmodule Comredis do
use Comredis.Command.Generator
...
end
Comredis
Abstract Syntax Tree
Expression
quoteunquote
1 + 1
quoteunquote
1 + 1
{:+, [context: Elixir, import: Kernel], [1, 1]}
Elixir
defmodule Comredis.Command.Generator do
defmacro __using__(_options) do
for command <- FileReader.load do
generate(command)
end
end
defp generate(command = %Command{}) do
quote do
@doc unquote doc(command)
unquote bodies(command,
Argument.split_options(command.arguments))
end
end
...
end
Elixir
defp bodies(command, {required_args, []}) do
args = argument_names(required_args)
quote do
def unquote(command.canonical_name)(unquote_splicing(args)) do
List.flatten [unquote(command.name), unquote_splicing(args)]
end
end
end
Elixir
defp bodies(%Command{canonical_name: :get, name: "GET"},
{[%Argument{canonical_name: :key}], []}) do
args = [{:key, [], Elixir}]
quote do
def unquote(command.canonical_name)(unquote_splicing(args)) do
List.flatten [unquote(command.name), unquote_splicing(args)]
end
end
end
defp bodies(%Command{canonical_name: :get, name: "GET"},
{%Argument{canonical_name: :key}, []}) do
quote do
def get(key) do
List.flatten ["GET", key]
end
end
end
Comredis
How to test it?
Don’t test code generation, but the
generated code
Write tests for each function?
No, automatically test all of them
Property-based testing
Generator: random arguments compliant to
command arguments
Property: commands have a valid syntax
Use a Redis server as a test oracle
github.com/parroty/excheck (triq)
Comredis
What is missing?
Type checking to use Dialyzer
More documentation
Something else?
Who uses similar ideas?
Elixir - unicode representation
ex2ms - ETS match expressions
Questions?
@fqiuri
github.com/iurifq/comredis

More Related Content

PDF
Redis - Usability and Use Cases
PDF
Pycon - Python for ethical hackers
PDF
Introduction to Nodejs
KEY
Redis, Resque & Friends
PDF
Fun with Ruby and Redis
PPTX
node.js workshop- node.js basics
PDF
Redis and its many use cases
PPTX
node.js workshop- node.js middleware
Redis - Usability and Use Cases
Pycon - Python for ethical hackers
Introduction to Nodejs
Redis, Resque & Friends
Fun with Ruby and Redis
node.js workshop- node.js basics
Redis and its many use cases
node.js workshop- node.js middleware

What's hot (20)

PDF
Node.js - async for the rest of us.
PDF
Node.js - A Quick Tour
PDF
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
PDF
Serializing Ruby Objects in Redis
PDF
Relayd: a load balancer for OpenBSD
PDF
Lua tech talk
PDF
Socket.IO
PPTX
Introduction to Node.js
KEY
Going real time with Socket.io
PDF
Socket.io (part 1)
KEY
Socket.io
PDF
"The little big project. From zero to hero in two weeks with 3 front-end engi...
PDF
OWASP Proxy
PDF
Nodejs - A quick tour (v6)
PDF
Atomicity In Redis: Thomas Hunter
KEY
A language for the Internet: Why JavaScript and Node.js is right for Internet...
KEY
Node.js - Best practices
PPTX
Shell Script Tutorial
PPTX
Async java8
PDF
Non-blocking I/O, Event loops and node.js
Node.js - async for the rest of us.
Node.js - A Quick Tour
Jan Stępień - GraalVM: Fast, Polyglot, Native - Codemotion Berlin 2018
Serializing Ruby Objects in Redis
Relayd: a load balancer for OpenBSD
Lua tech talk
Socket.IO
Introduction to Node.js
Going real time with Socket.io
Socket.io (part 1)
Socket.io
"The little big project. From zero to hero in two weeks with 3 front-end engi...
OWASP Proxy
Nodejs - A quick tour (v6)
Atomicity In Redis: Thomas Hunter
A language for the Internet: Why JavaScript and Node.js is right for Internet...
Node.js - Best practices
Shell Script Tutorial
Async java8
Non-blocking I/O, Event loops and node.js
Ad

Viewers also liked (13)

PPSX
Fireproof EDM webinar
PPTX
Bear out of parking lot
TXT
ff
PPT
Conoscere la videocamera
PPT
PaperCut-MF Education Features
PPT
3D visualisation of medical images
PPT
Traffic monitoring
DOC
Project report 3D visualization of medical imaging data
PPT
PaperCut-MF Business Features
PDF
Report medical image processing image slice interpolation and noise removal i...
PPTX
Cloud Computing & Cloud Architecture
PPTX
Differential in automobile
PPTX
Seminar on conversion of plastic wastes into fuels
Fireproof EDM webinar
Bear out of parking lot
ff
Conoscere la videocamera
PaperCut-MF Education Features
3D visualisation of medical images
Traffic monitoring
Project report 3D visualization of medical imaging data
PaperCut-MF Business Features
Report medical image processing image slice interpolation and noise removal i...
Cloud Computing & Cloud Architecture
Differential in automobile
Seminar on conversion of plastic wastes into fuels
Ad

Similar to Comredis (20)

PPTX
Developing a Redis Module - Hackathon Kickoff
PPTX
PDF
Extend Redis with Modules
PPT
Introduction to redis
PDF
REDIS intro and how to use redis
PPTX
PDF
Writing Redis Modules In Rust: Gavrie Philipson
PDF
Introduction to Elixir
PDF
Redis学习笔记
PDF
Redis Lua Scripts
PDF
Redis学习笔记
PDF
Ruby On Rails Introduction
PDF
Writing Redis in Python with asyncio
PDF
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
PDF
Building web framework with Rack
KEY
Rails web api 开发
PDF
KEY
Introduction to MongoDB
PDF
Redispresentation apac2012
PDF
Introduction to Redis
Developing a Redis Module - Hackathon Kickoff
Extend Redis with Modules
Introduction to redis
REDIS intro and how to use redis
Writing Redis Modules In Rust: Gavrie Philipson
Introduction to Elixir
Redis学习笔记
Redis Lua Scripts
Redis学习笔记
Ruby On Rails Introduction
Writing Redis in Python with asyncio
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Building web framework with Rack
Rails web api 开发
Introduction to MongoDB
Redispresentation apac2012
Introduction to Redis

Recently uploaded (20)

PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
medical staffing services at VALiNTRY
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Nekopoi APK 2025 free lastest update
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
System and Network Administraation Chapter 3
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
assetexplorer- product-overview - presentation
PPTX
CHAPTER 2 - PM Management and IT Context
PTS Company Brochure 2025 (1).pdf.......
How to Migrate SBCGlobal Email to Yahoo Easily
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
medical staffing services at VALiNTRY
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Nekopoi APK 2025 free lastest update
VVF-Customer-Presentation2025-Ver1.9.pptx
Digital Strategies for Manufacturing Companies
Understanding Forklifts - TECH EHS Solution
Softaken Excel to vCard Converter Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
System and Network Administraation Chapter 3
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms I-SECS-1021-03
assetexplorer- product-overview - presentation
CHAPTER 2 - PM Management and IT Context

Comredis