码迷,mamicode.com
首页 > 其他好文 > 详细

redis

时间:2019-07-23 09:17:22      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:mysq   db2   tar   reconnect   net   tomat   内存分配   end   emc   

Overview of Redis
The word Redis means Remote DIctionary Server
Initial release in 2009
It is an advanced key-value store or a data structure store
Runs entirely in memory
All data is kept in memory
Quick data access since it is maintained in memory
Data can be backed up to disk periodically
Single threaded server
Extensible via Lua scripts
Able to replicate data between servers
Clustering also available

About Redis
"Redis is an open source, BSD licensed, advanced key-value cache and store. It is oftenreferred to as a data structure server since keys can contain strings, hashs, lists, sets, sorted sets, bitmaps and hyperloglogs."

Redis:
KV cache and store
in-memory
持久化
主从(借助于sentinel实现一定意义上的HA)
Clustering(分布式)

    数据结构服务器:
        String, List, Hash, Set, Sorted Set, Bitmap, HyperLoglog

Redis
Redis is an in-memory but persistent on disk database
1 Million small key --> String value pairs use ~ 100MB of memory
Single threaded - but CPU should not be the bottleneck
Average Linux system can deliver even 500k requests per second
Limit is likely the available memory in your system
max. 232 keys

Persistence
Snapshotting
Data is asynchronously thransferred from memory to disk
AOF(Append Only File)
Each medifying operation is written to a file
Can recreate data store by replaying operations
Without interrupting service, will rebuild AOF as the shortest sequence of commands neded to rebuild the current dataset in memory

Replication
Redis supports master-slave replication
Master-slave replication can be chained
Be careful:
Slaves are writeable!
Potential for data inconsistency
Fully compatible with Pub/Sub features

Differences to Memcached
Memcached is a "distributed memory object caching system"
Redis persists data to disk eventually
Memcached is an LRU cache
Redis has different data types and more features
Memcached is multithreaded
Similar speed

Redis的优势
丰富的(资料形态)操作
Hashs, Lists, Sets, Sorted Sets, HyperLoglog等
内建replication及cluster
就地更新(in-place update)操作
支持持久化(磁盘)
避免雪崩效应
Memcached的优势
多线程
善用多核CPU
更少的阻塞操作
更少的内存开销
更少的内存分配压力
可能有更少的内存碎片

Prominent Adopters
Twitter
Pinterest
Tumblr
GitHub
Stack Overflow
digg
Blizard
flickr
WeiBo
... ...

Redis 3.0
2015年4月1日正式推出
Redis Cluster
新的"embedded string"
LRU演算法的改进
预设随机取5个样本,插入并排序至一个pool,移除最佳者,如此反复,直到内存用量小于maxmemory的设定
样本5比先前的3多
从局部最优趋向全局最优

Redis特性
BDBMS
Oracle, DB2, PostgreSQL, MySQL, SQL Server, ...
NoSQL
Cassandra, HBase, Memcached, MongoDB, Redis, ...
NewSQL
Aerospike, FoundationDB, RethinkDB, ...

存储系统有三类:
RDBMS:关系型数据库
NoSQL:非关系型数据库
KV NoSQL:redis
Colum Family NoSQL:HBase
Documention NoSQL:MongoDB
Graph NoSQL:Neo4j
NewSQL:分布式关系型数据库

Redis特性
Key-value NoSQL
Memcached, Redis, ...
Column family NoSQL
Cassandra, HBase, ...
Documen NoSQL
MongoDB, ...
Graph NoSQL
Neo4j, ...

Commands
Redis-server
redis-cli
Command line interface
Redis-benchmark
Benchmarking utility
redis-check-dump & redis-check-aof
Corrupted RDB/AOF files utilities

Redis的组件:
redis.io

Overview
Family of fundamental data structures
Strings and string containers
Accessed / indexed by key
Directly exposed -- No abstraction layers
Rich set of atomic operations over the structures
Detailed reference using big-O notation for complexities
Basic publish / subscribe infrastructure

官方站点:www.redis.io

Redis守护进程:
监听端口:6379/tcp

Keys
Arbitrary ASCII strings
Define some format convention and adhere to it
Key length matters!
Multiple name spaces are available
Separate DBs indexed by an integer value
SELECT command
Multiples DBs vs. Single DB + key prefixes
Keys can expire automatically

Data structures
Strings
Caching, counters, realtime metrice...
Hashes
"Object" storage...
Lists
Logs, queues, message passing...
Sets
Membership, tracking...
Ordered sets
Leaderboards, activity feeds...

Strings
help @string
SET
GET
EXISTS
Integers
DECR
INCR

实验环境:
系统版本:Centos6.5
内核版本:2.6.32-504.el6.x86_64
网卡1:VMnet0 172.16.100.6
网卡2:VMnet8 NAT DHCP

[root@CentOS6 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@CentOS6 ~]# yum info redis
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile

  • base: mirrors.aliyun.com
  • extras: mirrors.aliyun.com
  • updates: mirrors.aliyun.com
    Available Packages
    Name : redis
    Arch : x86_64
    Version : 3.2.12
    Release : 2.el6
    Size : 522 k
    Repo : epel
    Summary : A persistent key-value database
    URL : http://redis.io
    License : BSD
    Description : Redis is an advanced key-value store. It is often referred to as a data
    : structure server since keys can contain strings, hashes, lists, sets and
    : sorted sets.
    :
    : You can run atomic operations on these types, like appending to a string;
    : incrementing the value in a hash; pushing to a list; computing set
    : intersection, union and difference; or getting the member with highest
    : ranking in a sorted set.
    :
    : In order to achieve its outstanding performance, Redis works with an
    : in-memory dataset. Depending on your use case, you can persist it either
    : by dumping the dataset to disk every once in a while, or by appending
    : each command to a log.
    :
    : Redis also supports trivial-to-setup master-slave replication, with very
    : fast non-blocking first synchronization, auto-reconnection on net split
    : and so forth.
    :
    : Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
    : limited time-to-live, and configuration settings to make Redis behave like
    : a cache.
    :
    : You can use Redis from most programming languages also.
    [root@CentOS6 ~]# yum -y install redis
    [root@CentOS6 ~]# cp /etc/redis.conf{,.orgi}
    [root@CentOS6 ~]# vim /etc/redis.conf
    bind 127.0.0.1 172.16.100.6
    [root@CentOS6 ~]# service redis start
    [root@CentOS6 ~]# ss -tnl
    State Recv-Q Send-Q Local Address:Port Peer Address:Port
    LISTEN 0 128 :::22 :::
    LISTEN 0 128
    :22 :
    LISTEN 0 128 127.0.0.1:6379 :
    [root@CentOS6 ~]# redis-cli -h 172.16.100.6
    172.16.100.6:6379> help

172.16.100.6:6379> help @STRING

172.16.100.6:6379> HELP APPEND

172.16.100.6:6379> CLIENT LIST #查看连接客户端
id=2 addr=172.16.100.6:59420 fd=6 name= age=228 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client

172.16.100.6:6379> SELECT 1 #打开1号数据库,默认16个数据库

172.16.100.6:6379[1]> SELECT 0 #默认为0号数据库

172.16.100.6:6379> HELP SET

SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string

172.16.100.6:6379> SET disto fedora
OK
172.16.100.6:6379> GET disto
"fedora"
172.16.100.6:6379> SET disto centos
OK
172.16.100.6:6379> append disto slackware
(integer) 15
172.16.100.6:6379> GET disto
"centosslackware"

redis

标签:mysq   db2   tar   reconnect   net   tomat   内存分配   end   emc   

原文地址:https://blog.51cto.com/smoke520/2422603

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!