什么是Redis?为什么使用Readis?

Redis

Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string、list、set、zset(有序集合)和hash。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序和算法。
**

memcached

**

协议简单、基于libevent的事件处理、内置内存存储方式、memcached不互相通信的分布式。 各个memcached不会互相通信以共享信息,分布策略由客户端实现。不会对数据进行持久化,重启memcached、重启操作系统会导致全部数据消失。

Memcached常见的应用场景是存储一些读取频繁但更新较少的数据,如静态网页、系统配置及规则数据、活跃用户的基本数据和个性化定制数据、准实时统计信息等。

与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件(RDB和AOF两种方式),并且在此基础上实现了master-slave(主从)同步,机器重启后能通过持久化数据自动重建内存,使用Redis作为Cache时机器宕机后热点数据不会丢失。

Redis丰富的数据结构也使其拥有更加丰富的应用场景。Redis的命令都是原子性的,可以简单地利用INCR和DECR实现计数功能。使用list可以实现获取最近N个数的操作。sort set支持对数据排序,可以应用在排行榜中。set集合可以应用到数据排重。Redis还支持过期时间设置,可以应用到需要设定精确过期时间的应用。只要可以使用Redis支持的数据结构表示的场景,就可以使用Redis进行存储。

Redis基于乐观锁

为什么选择Redis?

1)Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash、String等数据结构的存储。
2)Redis支持master-slave(主-从)模式应用
3)Redis支持数据持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
4)Redis单个value的最大限制是1GB,memcached只能保存1MB的数据。

主要是从两个角度去考虑:性能和并发。当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有其他中间件(如zookpeer等)代替,并不是非要使用redis。因此,这个问题主要从性能和并发两个角度去答。

### Redis Master-Slave Replication Implementation Principle In Redis master-slave replication, data from one server (the master) is replicated to another server (the slave). This process ensures high availability and redundancy within a database setup. When a client writes data to the master, this information must propagate to all connected slaves. The initial synchronization between master and slave involves several steps: - **Full Resynchronization**: Upon first connection or when significant changes occur, the slave requests a full copy of the dataset held by the master. To achieve this efficiently without blocking operations on either side, Redis uses RDB snapshots[^1]. During this phase, the master creates an RDB file while continuing normal operation; once completed, it sends over the entire snapshot along with any commands received during creation time via buffered updates. After establishing the base state through resynchronization, incremental updates maintain consistency: - **Partial Resynchronizations Using Backlog Buffer**: For ongoing communication after the initial sync, both master and slave keep track of every write command executed using repl_backlog buffer which stores recent history of written instructions. If disconnection happens temporarily, upon reconnection instead of doing complete resync again, they attempt partial resynchronization where only missing commands since last successful transmission are transferred based on offset tracking mechanism provided by `psync` protocol introduced in version 2.8. To ensure fault tolerance further, mechanisms like forced manual failovers allow replicas under certain conditions to promote themselves into mastership roles should primary node failures happen unexpectedly as described elsewhere: ```bash CLUSTER FAILOVER TAKEOVER ``` This command specifically targets replica nodes instructing them to initiate takeover procedures for failing primaries ensuring minimal downtime and continuous service provision even amidst hardware faults or network partitions scenarios. --related questions-- 1. How does Redis handle read/write splitting between master and slave? 2. What role do heartbeats play in maintaining connections between Redis master and slave nodes? 3. Can you explain how Redis cluster mode differs from simple master-slave setups regarding replication strategies? 4. In what situations would a developer prefer synchronous versus asynchronous replication methods offered by Redis?
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值