redis 1.2.1

Redis client for dart #

[![Join the chat at](](

Redis protocol parser and client written in dart language
It is fast and simple by design. It requires no external package to run.

Supported features: #

Simple #

Redis client is simple serialiser and deserialiser of redis protocol. There are also some additional helper functions and classes available to make using redis features easier.

Redis protocol is a composition of array, strings (and bulk) and integers. For example executing command SET is no more than serializing array of strings ["SET","key","value"]. Commands can be executed by

Future f = command.send_object(["SET","key","value"]);

This enables sending any command. Before sending commands one needs to open a connection to redis. I will assume that you are running a redis server locally on port 6379. In this example we will open a connection, execute the command 'SET key 0' and then print result.

import 'package:redis/redis.dart';
RedisConnection conn = new RedisConnection();
conn.connect('localhost',6379).then((Command command){
    command.send_object(["SET","key","0"]).then((var response)

Due to the simple implementation, it is possible to execute commands in different ways. One an most straightforward way is one after another

RedisConnection conn = new RedisConnection();
conn.connect('localhost',6379).then((Command command){
  .then((var response){
    assert(response == 'OK');
    return command.send_object(["INCR","key"]);
  .then((var response){
    assert(response == 1);  
    return command.send_object(["INCR","key"]);
  .then((var response){
    assert(response == 2);
    return command.send_object(["INCR","key"]);
  .then((var response){
    assert(response == 3);
    return command.send_object(["GET","key"]);
  .then((var response){
    return print(response); // 3

Other possibility is to execute commands one by one without waiting for the previous command to complete. We can send all commands without needing to wait for a result, and we can still be sure that the response handled by Future will be completed in the correct order.

RedisConnection conn = new RedisConnection();
conn.connect('localhost',6379).then((Command command){
  .then((var response){
    assert(response == 'OK');
  .then((var response){
    assert(response == 1);  
  .then((var response){
    assert(response == 2);
  .then((var response){
    assert(response == 3);
  .then((var response){
     print(response); // 3

Difference is that there are 5 commands in last examples and only one in the previous example.

Generic #

Redis responses and requests can be arbitrarily nested. Mapping

| Redis | Dart | | ------------- |:-------------:| | String | String | | Integer | Integer |
| Array | List |
| Error | RedisError |

* Both simple string and bulk string from redis are serialied to dart string. String from dart to redis is converted to bulk string. UTF8 encoding is used in both directions.

Lists can be nested. This is usefull when executing EVAL command

command.send_object(["EVAL","return {KEYS[1],{KEYS[2],{ARGV[1]},ARGV[2]},2}","2","key1","key2","first","second"])

results in

[key1, [key2, [first], second], 2]

Fast #

Tested on a laptop, we can execute and process 180K INCR operations per second.

This is the code that yields such a result

const int N = 200000;
int start;
RedisConnection conn = new RedisConnection();
conn.connect('localhost',6379).then((Command command){
  print("test started, please wait ...");
  start =  new;
  for(int i=1;i<=N;i++){
      if(i != v)
        throw("wrong received value, we got $v");
  //last command will be executed and then processed last
    double diff = (new - start)/1000.0;
    double perf = N/diff;
    print("$N operations done in $diff s\nperformance $perf/s");

We are not just sending 200K commands here, but also checking result of every send command.

Using command.pipe_start(); and command.pipe_end(); is nothing more than enabling and disabling Nagle's algorhitm on socket. By default it is disabled to achieve shortest possible latency at expense of having more TCP packets and extra overhead. Enabling Nagle's algorithm during transactions can achieve greater data throughput and less overhead.

Transactions #

Transactions by redis protocol are started by the command MULTI and then completed with the command EXEC. .multi(), .exec() and class Transaction are implemented as additional helpers for checking the result of each command executed during transaction.

Future<Transaction> Command.multi();

Executing multi() will return a Future with Transaction. This class should be used to execute commands by calling .send_object. It returns a Future that is called after calling .exec().

import 'package:redis/redis.dart';

RedisConnection conn = new RedisConnection();
conn.connect('localhost',6379).then((Command command){
  command.multi().then((Transaction trans){
    for(int i=0;i<200000;++i){
      print("number is now $v");


It is impossible to write code that depends on the result of the previous command during a transaction, because all commands are executed at once. To overcome this case, user should employ technique CAS. Cas is a convenience class for simplifying this pattern.

Cas constructor requires Command as argument.

Cas implements two methods watch() and multiAndExec().
watch takes two arguments. First argument is list of keys to watch, and second argument is handler to call and to proceed with CAS.

for example:["key1,key2,key3"],(){
  //body of CAS

Failure happens if the watched key is modified out of the transaction. When this happens the handler is called until final transaction completes. multiAndExec is used to complete transation. Method takes handler where argument is Transaction.

For example:

//last part in body of CAS
cas.multiAndExec((Transaction trans){

imagine we have the need to atomically increment the value of a key by 1 (let's suppose Redis doesn't have INCR).

Cas cas = new Cas(command);["key"], (){
  command.send_object(["GET","key"]).then((String val){
    int i = int.parse(val);
    cas.multiAndExec((Transaction trans){

Unicode #

By default UTF8 encoding/decoding for string is used. Each string is converted in binary array using UTF8 encoding. This makes ascii string compatible in both direction.

PubSub #

PubSub is a helper for dispatching received messages. First, create a new PubSub from an existing Command

PubSub pubsub=new PubSub(command);

Once PubSub is created, Command is invalidated and should not be used on the same connection. PubSub allows commands

void subscribe(List<String> channels)
void psubscribe(List<String> channels)
void unsubscribe(List<String> channels)
void punsubscribe(List<String> channels)

and additional Stream getStream()

getStream returns Stream

Example for receiving and printing messages

  print("message: $message");

Sending messages can be done from different connection for example


Todo #

In the near future:

  • Better documentation
  • Implement all "generic commands" with named commands
  • Better error handling - that is ability to recover from error
  • Spell check code

Changes #

1.2.0 #

  • Received redis errors throws exception. Thanks to @eknoes for pull request.
  • Integers in array get auto converted to strings. Author @eknoes.
  • Improve transaction handling errors. Patch from @eknoes
  • Testing migrated on dart.test. Patch from @eknoes

1.1.0 #

  • Performance tweaks and simplified code

1.0.0 #

  • Dart 2.0 support

0.4.5 #

0.4.4 #

0.4.3 #

  • Cas helper
  • Improved unit tests

0.4.2 #

  • Improved performance by 10%
  • Pubsub interface uses Stream
  • Better test coverage
  • Improved documentation

0.4.1 #

  • Command raise error if used during transaction.

0.4.0 #

  • PubSub interface is made simpler but backward incompatible :(
  • README is updated

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  redis: ^1.2.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:redis/redis.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/connection.dart. (-5.36 points)

Analysis of lib/connection.dart reported 11 hints, including:

line 15 col 10: Don't explicitly initialize variables to null.

line 16 col 14: Don't explicitly initialize variables to null.

line 17 col 20: Unnecessary new keyword.

line 18 col 24: Unnecessary new keyword.

line 26 col 16: Unnecessary new keyword.

Fix lib/lazystream.dart. (-5.36 points)

Analysis of lib/lazystream.dart reported 11 hints, including:

line 20 col 25: The value of the field '_ss' isn't used.

line 26 col 14: Unnecessary new keyword.

line 40 col 24: Unnecessary new keyword.

line 67 col 22: Unnecessary new keyword.

line 85 col 7: The value of the field '_start_index' isn't used.

Fix lib/redisparser.dart. (-3.45 points)

Analysis of lib/redisparser.dart reported 7 hints, including:

line 85 col 18: Unnecessary cast.

line 94 col 38: Unnecessary cast.

line 100 col 9: DO use curly braces for all flow control structures.

line 118 col 16: Unnecessary new keyword.

line 128 col 9: DO use curly braces for all flow control structures.

Fix additional 6 files with analysis or formatting issues. (-9.43 points)

Additional issues in the following files:

  • lib/redisserialise.dart (6 hints)
  • lib/pubsub.dart (4 hints)
  • lib/transaction.dart (4 hints)
  • lib/cas.dart (3 hints)
  • lib/command.dart (2 hints)
  • lib/redis.dart (Run dartfmt to format lib/redis.dart.)

Maintenance issues and suggestions

Provide a file named (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and redis.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.6.5