What can Redis do besides caching

Cache

Everyone knows what Redis is, a non-relational database. In most cases, we use Redis for caching. The use of caching is generally like this:

Redis cache

(1) Get data from the Redis cache, if there is data, return the value directly.

(2) If it does not exist, execute the query method of the database

(3) Put the value in the database into the cache and return the value

code show as below:

Of course, we can also use Spring's cache annotations @Cacheble. We need to enable the cache annotation on the configuration class @EnableCaching, which is used as follows:

If you are interested in children's shoes, you can read this article I wrote:

Use AOP to customize Redis cache annotations

In this article, I re-developed the cache annotation and added two functions: setting the timeout period and setting the number of concurrent requests. Different parameters can be set in different scenarios.

queue

The above is actually our commonly used scenario, so in addition to caching, what else can Redis do?

We can also use Redis as a queue.

Redis queue

So when can we use Redis's queue function?

The sample code is as above.

For specific use cases, you can read this article I wrote:

My multi-threaded crawler project actual combat

In the crawler project of this article, the crawled piece of data may fail due to network and other reasons. At this time, I will log the failed urlOR code, and store the crawling abnormal urlOR codeinto the Redis queue.

I restarted a thread in the background, spinning out the data in the Redis queue in a blocking manner. Then crawl again.

Redis crawler

Sign-in statistics

We can use Redis Bitmap to store sign-in data.

Bitmap is a binary array with unlimited length (when the length is 2 billion, it occupies more than 200 MB of memory). The value in the array is 0 or 1.

Using Redis Bitmap is very fast and has better performance in high concurrency situations. Moreover, it occupies a small space, and the Bitmap can store about one bit (the length of the bit array is about five or six billion).

For example: sign:1:202009 indicates the check-in record of the user with id 1 in September 2020.

The Java sample code is as follows:

jedis.bitfield(buildSignKey(userId, date), "GET", type, "0");

For specific usage, please see this article I wrote:

I made a sign-in function, and the architect felt it could be optimized after looking at it

Atomic deduction of inventory

In the spike system, Redis is used to store the inventory quantity. When a user initiates a rush purchase request, it is first judged whether the inventory in Redis is available. If available, put the snap-up request into the distributed queue, process subsequent operations in an asynchronous manner, and complete the order. At the same time, make inventory deductions in Redis.

Redis inventory deduction

The sample code is as follows:

If you are interested in children's shoes, you can read this article I wrote:

During an interview with Ali, I was asked how to design a spike system

Redis distributed lock

The current system is deployed in a cluster, and each service is not a single node. For example, the inventory service may be deployed on three machines named node 1, node 2, and node 3 . Inventory Service (using databases such as MySQL) required inventory deduction, the deduction of stock certainly need to lock it, if you use Lockor synchronizedcan only lock their own node. Access from the front desk is randomly routed to these 3 nodes. If the thread came in the node 1 on the lock, when the threads come in two possible access to that node 2 , then the node 2 has not been locked, then the stock will be deducted errors. And inventory deduction is still a core operation, and there are bugs, which is terrible to think about.

Redis distributed lock implementation ideas are as follows:

  • setnx + Use luascript to ensure atomicity of expiration time

  • Lock holding heartbeat detection (to prevent unlocking and lock failure problems)

  • Threads choose to acquire locks

We can also use the existing wheel Redissonframe.

Redis delay queue

Redis zsetcan be used as a delay queue. It scoreis the time point of delay. The value of the port is obtained sequentially when obtaining, and it scorecan be taken out if the current timestamp is equal to .

The sample code is shown in the figure above.

other

Redis also has many richer functions. For example, generating a global id number (atomic self-increment), statistics of Weibo likes (atomic self-increment), Bloom filter (Bitmap), ranking implementation (zset), geographic location query (GEO), producer and consumer (Stream), publish and subscribe.

Those who are interested can read this article of mine:

Redis advanced

This article is finished here, and you are welcome to leave a message on how you use Redis in your project.

 

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/109325092