Contributions welcome!! A lot of the missing stuff is really easy to add!
Performance
Performance differs, e.g. lists are implemented using arrays (hence RPUSH is
okayish, LPUSH is very slow).
But looking at just the simple GET/SET, it is surprisingly close to the
highly optimized C implementation:
helge@ZeaPro ~ $ redis-benchmark -p 1337 -t SET,GET,RPUSH,INCR -n 500000 -q
SET: 48003.07 requests per second
GET: 48459.00 requests per second
INCR: 43890.45 requests per second
RPUSH: 46087.20 requests per second
helge@ZeaPro ~ $ redis-benchmark -t SET,GET,RPUSH,INCR -n 500000 -q
SET: 54884.74 requests per second
GET: 54442.51 requests per second
INCR: 54692.62 requests per second
RPUSH: 54013.18 requests per second
Redi/S on RaspberryPi 3B+
$ redis-benchmark -h zpi3b.local -p 1337 -t SET,GET,RPUSH,INCR -n 50000 -q
SET: 4119.29 requests per second
GET: 5056.12 requests per second
INCR: 3882.59 requests per second
RPUSH: 3872.07 requests per second
There are Performance notes,
looking at the specific NIO implementation of Redi/S.
Persistence is really inefficient,
the databases are just dumped as JSON via Codable.
Easy to fix.
Notice how the server says: “Port 1337”. This is the port the server is running
on.
Via telnet/netcat
You can directly connect to the server and issue Redis commands (the server
is then running the connection in telnet mode, which is different to the
regular RESP protocol):
$ nc localhost 1337
KEYS *
*0
SET theanswer 42
+OK
GET theanswer
$2
42
Redis is a key/value store. That is, it acts like a big Dictionary that
can be modified from multiple processes. Above we list the available
KEYS, then we set the key theanswer to the value 42, and retrieve it.
(Redis provides great documentation
on the available commands, Redi/S implements many, but not all of them).
Via redis-cli
Redis provides a tool called redis-cli, which is a much more convenient
way to access the server.
On macOS you can install that using brew install redis (which also gives
you the real server),
on Ubuntu you can grab it via sudo apt-get install redis-tools.
The same thing we did in telnet above:
$ redis-cli -p 1337
127.0.0.1:1337> KEYS *
1) "theanswer"
127.0.0.1:1337> SET theanswer 42
OK
127.0.0.1:1337> GET theanswer
"42"
Key Expiration
Redis is particularily useful for HTTP session stores, and for caches.
When setting a key, you can set an “expiration” (in seconds, milliseconds,
or Unix timestamps):
We are using “strings” here. In Redis “strings” are actually “Data” objects,
i.e. binary arrays of bytes (this is even true for bytes!).
For example in a web application, you could use the “session-id” you generate,
serialize your session into a Data object, and then store it like
SET session-id <session> TTL 600.
Key Generation
But how do we generate keys (e.g. session-ids) in a distributed setting?
As usual there are many ways to do this.
For example you could use a Redis integer key which provides atomic increment
and decrement operations:
127.0.0.1:1337> SET idsequence 0
OK
127.0.0.1:1337> INCR idsequence
(integer) 1
127.0.0.1:1337> INCR idsequence
(integer) 2
Or if you generate keys on the client side, you can validate that they are
unique using SETNX. For example:
127.0.0.1:1337> SETNX mykey 10
(integer) 1
And another client will get
127.0.0.1:1337> SETNX mykey 10
(integer) 0
Simple Lists
Redis cannot only store string (read: Data) values, it can also store
lists, sets and hashes (dictionaries).
As well as some other datatypes:
Data Types Intro.
127.0.0.1:1337> RPUSH chatchannel "Hi guys!"
(integer) 1
127.0.0.1:1337> RPUSH chatchannel "How is it going?"
(integer) 2
127.0.0.1:1337> LLEN chatchannel
(integer) 2
127.0.0.1:1337> LRANGE chatchannel 0 -1
1) "Hi guys!"
2) "How is it going?"
127.0.0.1:1337> RPOP chatchannel
"How is it going?"
127.0.0.1:1337> RPOP chatchannel
"Hi guys!"
127.0.0.1:1337> RPOP chatchannel
(nil)
Monitoring
Assume you want to debug what’s going on on your Redis server.
You can do this by connecting w/ a fresh client and put that into
“monitoring” mode. The Redis server will echo all commands it receives
to that monitor:
Redis includes a simple publish/subscribe server.
Any numbers of clients can subscribe to any numbers of channels.
Other clients can then push “messages” to a channel, and all
subscribed clients will receive them.
Another client (the reply contains the number of consumers):
127.0.0.1:1337> PUBLISH thermostats:kitchen "temperature set to 42℃"
(integer) 1
The subscribed client will get:
1) message
2) "thermostats:kitchen"
3) "temperatur set to 4242℃"
Note: PubSub is separate to the key-value store. You cannot watch keys using
that! (there are blocking list operations for producer/consumer scenarios,
but those are not yet supported by Redi/S)
Benchmarking
Redis tools also include a tool called redis-benchmark which can be,
similar to apache-bench or wrk be used to measure the server performance.
For example, to exercise the server with half a million SET, GET, RPUSH and INCR
requests each:
$ redis-benchmark -p 1337 -t SET,GET,RPUSH,INCR -n 500000 -q
SET: 43192.81 requests per second
GET: 46253.47 requests per second
INCR: 38952.95 requests per second
RPUSH: 39305.09 requests per second
Who
Brought to you by
ZeeZide.
We like
feedback,
GitHub stars,
cool contract work,
presumably any form of praise you can think of.
Redi/S
Redi/S is a Redis server implementation in the Swift programming language. Based on Apple’s SwiftNIO framework.
What is Redis? Checkout the home page, but it is an easy to use and very popular Key-Value Store, w/ PubSub functionality.
It is not meant to replace the C based Redis server, but the goal is to make it feature complete and well performing.
Use cases:
Supported Commands
Redi/S supports a lot, including PubSub and monitoring.
Redi/S supports a lot not, including transactions or HyperLogLogs.
There is a list of supported commands.
Contributions welcome!! A lot of the missing stuff is really easy to add!
Performance
Performance differs, e.g. lists are implemented using arrays (hence RPUSH is okayish, LPUSH is very slow). But looking at just the simple GET/SET, it is surprisingly close to the highly optimized C implementation:
Redi/S (2 NIO threads on MacPro 3,7 GHz Quad-Core Intel Xeon E5):
Redis 4.0.8 (same MacPro 3,7 GHz Quad-Core Intel Xeon E5):
Redi/S on RaspberryPi 3B+
There are Performance notes, looking at the specific NIO implementation of Redi/S.
Persistence is really inefficient, the databases are just dumped as JSON via Codable. Easy to fix.
How to run
Status
There are a few inefficiencies, the worst being the persistent storage. Yet generally this seems to work fine.
The implementation has grown a bit and could use a little refactoring, specially the database dump parts.
Playing with the Server
You’d like to play with this, but never used Redis before? OK, a small tutorial on what you can do with it.
First make sure the server runs in one shell:
Notice how the server says: “Port 1337”. This is the port the server is running on.
Via telnet/netcat
You can directly connect to the server and issue Redis commands (the server is then running the connection in
telnet mode
, which is different to the regular RESP protocol):Redis is a key/value store. That is, it acts like a big Dictionary that can be modified from multiple processes. Above we list the available
KEYS
, then we set the keytheanswer
to the value 42, and retrieve it. (Redis provides great documentation on the available commands, Redi/S implements many, but not all of them).Via redis-cli
Redis provides a tool called
redis-cli
, which is a much more convenient way to access the server. On macOS you can install that usingbrew install redis
(which also gives you the real server), on Ubuntu you can grab it viasudo apt-get install redis-tools
.The same thing we did in
telnet
above:Key Expiration
Redis is particularily useful for HTTP session stores, and for caches. When setting a key, you can set an “expiration” (in seconds, milliseconds, or Unix timestamps):
We are using “strings” here. In Redis “strings” are actually “Data” objects, i.e. binary arrays of bytes (this is even true for bytes!). For example in a web application, you could use the “session-id” you generate, serialize your session into a Data object, and then store it like
SET session-id <session> TTL 600
.Key Generation
But how do we generate keys (e.g. session-ids) in a distributed setting? As usual there are many ways to do this. For example you could use a Redis integer key which provides atomic increment and decrement operations:
Or if you generate keys on the client side, you can validate that they are unique using SETNX. For example:
And another client will get
Simple Lists
Redis cannot only store string (read: Data) values, it can also store lists, sets and hashes (dictionaries). As well as some other datatypes: Data Types Intro.
Monitoring
Assume you want to debug what’s going on on your Redis server. You can do this by connecting w/ a fresh client and put that into “monitoring” mode. The Redis server will echo all commands it receives to that monitor:
Some other client:
The monitor will print:
Publish/Subscribe
Redis includes a simple publish/subscribe server. Any numbers of clients can subscribe to any numbers of channels. Other clients can then push “messages” to a channel, and all subscribed clients will receive them.
One client:
Another client (the reply contains the number of consumers):
The subscribed client will get:
Benchmarking
Redis tools also include a tool called
redis-benchmark
which can be, similar toapache-bench
orwrk
be used to measure the server performance.For example, to exercise the server with half a million SET, GET, RPUSH and INCR requests each:
Who
Brought to you by ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.
There is a
#swift-nio
channel on the swift-server Slack.