当前位置:网站首页>Redis understanding
Redis understanding
2022-08-04 22:19:00 【Learning Micro Station】
Redis
1. WHY
1. High concurrent reading and writing of data 2. Massive data reading and writing 3. Infrequent changes, but high pressure on MySQL queries 4. Solutions that want to use distributed locks, as well as caching, data persistence, transactions, message queues.
Second, WHAT
nosql non-relational database
Redis is a memory cache database.The full name of Redis is: Remote Dictionary Server (remote data service), written in C language. Redis is a key-value storage system (key-value storage system) that supports rich data types, such as: String, list, set, zset, hash.
The difference between Redis and memcache is that the data stored in Redis is persistent, and the data will not be lost after power failure or restart.
3. HOW
Redisson (distributed), Jedis, lettuce,
Jedis is the client of Redis's Java implementation, and its API provides more comprehensive support for Redis commands.
Redisson implements a distributed and scalable Java data structure. Compared with Jedis, it has simpler functions, does not support string operations, and does not support Redis features such as sorting, transactions, pipelines, and partitioning.The purpose of Redisson is to promote the separation of users' concerns about Redis, so that users can focus more on processing business logic
Common frameworks are:
Redis Client provided by Spring, StringRedisTemplate is an API tool for manipulating Reids.
org.springframework.boot
spring-boot-starter-data-redis
And redis distributed lock
Many problems have been encountered in the process of using lock4j. It is not recommended to use lock4j. It is recommended to use the distributed lock of redisson directly.
com.baomidou
lock4j-redisson-spring-boot-starter
2.2.2
或者redisson自己的注解
org.redisson
redisson
3.5.0
或者spring-integration-redis中提供了Redis分布式锁的实现,
比如
@Configuration
public class RedisLockConfig {
@Bean
public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) {
return new RedisLockRegistry(redisConnectionFactory, “myRegistryKey”);
}
}
@Autowired
private RedisLockRegistry redisLockRegistry;
public void test() {
// Get the lock object
Lock lock = redisLockRegistry.obtain(“myLockKey”);
// Lock it
boolean lockRsp = lock.tryLock(2, TimeUnit.SECONDS);
try {
// business logic
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// Release the lock
lock.unlock();
}
}
In addition, set is implemented like this
private final RedisTemplate
@Autowired
public RedisUtil(RedisTemplate
this.redisTemplate = redisTemplate;
}
public void set(String field, T obj) {
final byte[] value = ProtoStuffUtil.serialize(obj);
redisTemplate.execute((RedisCallback) connection -> {
connection.set(field.getBytes(Charset.forName(“UTF8”)), value);
return null;
});
}
依赖如下
org.springframework.boot
spring-boot-starter-data-redis
Fourth, depth
Some friends think that it is enough to set the lock expiration time a little longer.In fact, let's imagine whether it is possible to start a timed daemon thread for the thread that obtained the lock, and check whether the lock still exists at regular intervals. If it exists, the expiration time of the lock will be extended to prevent the lock from being expired and released in advance.
The current open source framework Redisson solves this problem.Let's take a look at the underlying schematic diagram of Redisson: as long as the thread is successfully locked, a watch dog watchdog will be started. It is a background thread and will check every 10 seconds. If thread 1 still holds the lock,Then the lifetime of the lock key will be continuously extended.Therefore, Redisson uses Redisson to solve the problem of "lock expired release, business is not completed".
Six, why is redis single thread?
Because cpu is not the bottleneck of Redis, the bottleneck of Redis may be machine memory or network bandwidth.So Redis is single-threaded.A single-core CPU can process 1 million instructions per second, which corresponds to hundreds of thousands of requests.
Seven, what is cache penetration?How to deal with it?
Cache penetration: Refers to querying a data that must not exist. Since the cache misses, it needs to be queried from the database, and the data that cannot be found will not be written to the cache, which will cause the non-existent data to every timeEach request must go to the database to query, causing cache penetration.Solution: If the result returned by a query is empty, the empty result is cached, but the expiration time is set to a short time, and the longest is no more than 5 minutes.
Eighth, how to ensure the consistency of cache and database data?
Reasonably set the expiration time of the cache, update redis synchronously when adding, modifying, and deleting database operations, and the transaction mechanism can be used to ensure data consistency.
边栏推荐
- MQTT[一]基础知识介绍
- 【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
- 中大型商业银行堡垒机升级改造方案!必看!
- 今天是七夕,来看看程序员的土味情话。
- 一招包治pycharm DEBUG报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode
- Rocketchip RISC-V Debug调试硬件相关(四)hartIsInReset
- CPU、内存、显卡等硬件因素也影响着你的深度学习模型性能
- LeetCode: 406. 根据身高重建队列
- The upgrade and transformation plan of the fortress machine for medium and large commercial banks!Must see!
- LocalDateTime的详细使用方法
猜你喜欢
随机推荐
Hardware factors such as CPU, memory, and graphics card also affect the performance of your deep learning model
LeetCode143:重排链表
How to right use of WebSocket in project
【组成原理 六 存储器类型】
[QT] Implementation of callback function
备战9月,美团50道软件测试经典面试题及答案汇总
【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
BUG | 接口返回异常数据
promise详解
老叶的三束玫瑰
LeetCode 199: 二叉树的右视图
UDP通信
OC-拷贝
力扣24-两两交换链表中的节点——链表
Unknown point cloud structure file conversion requirements
中大型商业银行堡垒机升级改造方案!必看!
numpy关于两个array叠加操作
视频gif如何制作?试试这个视频制作gif神器
Altium Designer 19.1.18 - 保护锁定的对象
QT 子窗口—>主窗口 信号和槽的交互