当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
字节跳动秋招提前批高频面试问题汇总!(内附答案!)
阿里巴巴2022届秋招面试真题和答案!
docker 部署redis集群
LeetCode: 406. 根据身高重建队列
Rt-thread [三] link.lds链接脚本详解
Domestic PMP certificate of gold content how
【QT】回调函数的实现
CPU、内存、显卡等硬件因素也影响着你的深度学习模型性能
com.jacob.com.ComFailException: Invoke of: ActiveDocument
湖仓一体电商项目(五):内网穿透工具-网云穿
[larave]关于laravel使用form submit()不能获取值问题
单片机原理[一] 学好单片机必会的五张图
rk3399-9.0 first-level and second-level dormancy
Debian防火墙的开关以及状态
CountDownLatch使用及原理
关于el-table列表渲染
As hot as ever, reborn | ISC2022 HackingClub White Hat Summit was successfully held!
年薪40W测试工程师成长之路,你在哪个阶段?
使用cpolar优化树莓派上的网页(1)
rk3399-0.0 svc命令