SSDB —— 开源NoSQL数据库 Redis之外的选择

简介: SSDB是一个快速的用来存储十亿级别列表数据的开源 NoSQL 数据库。

SSDB是一个快速的用来存储十亿级别列表数据的开源 NoSQL 数据库。


特性

  • 替代 Redis 数据库, Redis 的 100 倍容量
  • LevelDB 网络支持, 使用 C/C++ 开发
  • Redis API 兼容, 支持 Redis 客户端
  • 适合存储集合数据, 如 list, hash, zset...
  • 客户端 API 支持的语言包括: C++、PHP、Python、Cpy、Java、NodeJS、Ruby、Go。
  • 持久化的队列服务
  • 主从复制, 负载均衡


性能

1000请求:

writeseq  :    0.546 ms/op      178.7 MB/s

writerand :    0.519 ms/op      188.1 MB/s

readseq   :    0.304 ms/op      321.6 MB/s

readrand  :    0.310 ms/op      315.0 MB/s

并发:

==========set==========

qps:44251,time:0.226s

==========get==========

qps:55541,time:0.180s

==========del==========

qps:46080,time:0.217s

==========hset==========

qps:42338,time:0.236s

==========hget==========

qps:55601,time:0.180s

==========hdel==========

qps:46529,time:0.215s

==========zset==========

qps:37381,time:0.268s

==========zget==========

qps:41455,time:0.241s

==========zdel==========

qps:38792,time:0.258s

在MacBook Pro 13 (Retina屏幕)上运行。

与redis的比较:

性能数据使用 ssdb-bench(SSDB) 和 redis-benchmark(Redis) 来获取。

image.png

架构

image.png


安装

下载压缩包,解压缩

wget --no-check-certificate https://github.com/ideawu/ssdb/archive/master.zip

unzip master

cd ssdb-master

编译

make

安装(可选)

sudo make install

运行

./ssdb-server ssdb.conf

或者以后台的方式运行

./ssdb-server -d ssdb.conf

ssdb命令行

./tools/ssdb-cli -p 8888

停止ssdb-server

kill `cat ./var/ssdb.pid`

使用

PHP

<?php

include_once('SSDB.php');

try{

   $ssdb = new SimpleSSDB('127.0.0.1', 8888);

}catch(Exception$e){

   die(__LINE__ . ' ' . $e->getMessage());

}

$ret = $ssdb->set('key', 'value');

if($ret === false){

   // error!

}

echo$ssdb->get('key');

Python

使用pyssdb

>>>import pyssdb

>>>c = pyssdb.Client()

>>>c.set('key', 'value')

1

>>>c.get('key')

'value'

>>>c.hset('hash', 'item', 'value')

1

>>>c.hget('hash', 'item')

'value'

>>>c.hget('hash', 'not exist') is None

True

>>>c.incr('counter')

1

>>>c.incr('counter')

2

>>>c.incr('counter')

3

>>>c.keys('a', 'z', 1)

['counter']

>>>c.keys('a', 'z', 10)

['counter', 'key']

Ruby

使用ssdb-rb

require "ssdb"

ssdb = SSDB.new url: "ssdb://1.2.3.4:8889"

ssdb.set("mykey", "hello world")

# => true

ssdb.get("mykey")

# => "hello world"

ssdb.batch do

 ssdb.set"foo", "5"

 ssdb.get"foo"

 ssdb.incr "foo"

end

# => [true, "5", 6]

Go

package main

import (

       "fmt"

       "os"

       "./ssdb"

      )

func main(){

   ip := "127.0.0.1";

   port := 8888;

   db, err := ssdb.Connect(ip, port);

   if(err != nil){

       os.Exit(1);

   }

   var val interface{};

   db.Set("a", "xxx");

   val, err = db.Get("a");

   fmt.Printf("%s\n", val);

   db.Del("a");

   val, err = db.Get("a");

   fmt.Printf("%s\n", val);

   db.Do("zset", "z", "a", 3);

   db.Do("multi_zset", "z", "b", -2, "c", 5, "d", 3);

   resp, err := db.Do("zrange", "z", 0, 10);

   iferr != nil{

       os.Exit(1);

   }

   if len(resp) % 2 != 1{

       fmt.Printf("bad response");

       os.Exit(1);

   }

   fmt.Printf("Status: %s\n", resp[0]);

   for i:=1; i<len(resp); i+=2{

       fmt.Printf("  %s : %3s\n", resp[i], resp[i+1]);

   }

   return;

ngx_lua

使用lua-resty-ssdb

lua_package_path "/path/to/lua-resty-ssdb/lib/?.lua;;";

server {

   location /test {

       content_by_lua '

           local ssdb = require "resty.ssdb"

           local db = ssdb:new()

           db:set_timeout(1000) -- 1 sec

           local ok, err = db:connect("127.0.0.1", 8888)

           ifnot ok then

               ngx.say("failed to connect: ", err)

               return

           end

           ok, err = db:set("dog", "an animal")

           ifnot ok then

               ngx.say("failed to set dog: ", err)

               return

           end

           ngx.say("set result: ", ok)

           local res, err = db:get("dog")

           ifnot res then

               ngx.say("failed to get dog: ", err)

               return

           end

           if res == ngx.null then

               ngx.say("dog not found.")

               return

           end

           ngx.say("dog: ", res)

           db:init_pipeline()

           db:set("cat", "Marry")

           db:set("horse", "Bob")

           db:get("cat")

           db:get("horse")

           local results, err = db:commit_pipeline()

           ifnot results then

               ngx.say("failed to commit the pipelined requests: ", err)

               return

           end

           for i, res in ipairs(results) do

               if type(res) == "table"then

                   ifnot res[1] then

                       ngx.say("failed to run command ", i, ": ", res[2])

                   else

                       -- process the table value

                   end

               else

                   -- process the scalar value

               end

           end

           -- put it into the connection pool of size 100,

           -- with 0 idle timeout

           local ok, err = db:set_keepalive(0, 100)

           ifnot ok then

               ngx.say("failed to set keepalive: ", err)

               return

           end

           -- or just close the connection right away:

           -- local ok, err = db:close()

           -- if not ok then

           --     ngx.say("failed to close: ", err)

           --     return

           -- end

       ';

   }

}

C++

#include<stdio.h>

#include<stdlib.h>

#include<string>

#include<vector>

#include"SSDB.h"

intmain(int argc, char **argv){

   constchar *ip = (argc >= 2)? argv[1] : "127.0.0.1";

   int port = (argc >= 3)? atoi(argv[2]) : 8888;

   ssdb::Client *client = ssdb::Client::connect(ip, port);

   if(client == NULL){

       printf("fail to connect to server!\n");

       return0;

   }

   ssdb::Status s;

   s = client->set("k", "hello ssdb!");

   if(s.ok()){

       printf("k = hello ssdb!\n");

   }else{

       printf("error!\n");

   }

   delete client;

   return 0;

相关文章
|
5月前
|
人工智能 运维 NoSQL
云栖大会|AI浪潮下的NoSQL演进:下一代数据库的破局之道
AI浪潮下的NoSQL演进:下一代数据库的破局之道
|
9月前
|
存储 NoSQL 搜索推荐
NoSQL数据库分类概览
以上就是我们的NoSQL数据库奇幻之旅。每一种NoSQL数据库都有自己独特的魅力和专长,择选合适的数据库,就像在魔法世界中挑选最适合自己的魔杖,使你的数据管理变得更加高效和神奇。在当今数据驱动的时代,懂得这些数据库的秘密,就掌握了处理各种数据挑战的关键。
437 61
|
存储 NoSQL 关系型数据库
【赵渝强老师】什么是NoSQL数据库?
随着大数据技术的兴起,NoSQL数据库(Not Only SQL)得到广泛应用。它不局限于二维表结构,允许数据冗余。常见的NoSQL数据库包括Redis、MongoDB和HBase。Redis是基于内存的高性能数据库,采用单线程模型和多路复用I/O,支持高效的数据结构。MongoDB使用BSON格式存储文档,查询语言强大,类似关系型数据库。HBase基于HDFS,适合数据分析,采用列式存储,支持灵活的列族设计。视频讲解及更多内容见下文。
720 79
|
存储 缓存 NoSQL
常见的 NoSQL 数据库有哪些?
常见的 NoSQL 数据库有哪些?
983 59
|
存储 监控 NoSQL
NoSQL与Redis配置与优化
通过合理配置和优化Redis,可以显著提高其性能和可靠性。选择合适的数据结构、优化内存使用、合理设置持久化策略、使用Pipeline批量执行命令、以及采用分布式集群方案,都是提升Redis性能的重要手段。同时,定期监控和维护Redis实例,及时调整配置,能够确保系统的稳定运行。希望本文对您在Redis的配置与优化方面有所帮助。
217 23
|
存储 监控 NoSQL
NoSQL与Redis配置与优化
通过合理配置和优化Redis,可以显著提高其性能和可靠性。选择合适的数据结构、优化内存使用、合理设置持久化策略、使用Pipeline批量执行命令、以及采用分布式集群方案,都是提升Redis性能的重要手段。
250 7
|
存储 SQL JSON
介绍一下RDBMS和NoSQL数据库之间的区别
【10月更文挑战第21天】介绍一下RDBMS和NoSQL数据库之间的区别
524 2
|
存储 SQL NoSQL
数据库技术深度探索:从关系型到NoSQL的演变
【10月更文挑战第21天】数据库技术深度探索:从关系型到NoSQL的演变
351 1
|
6月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
454 158
|
6月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。