Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement timed tasks in redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to achieve timed tasks in redis. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Technology stack

Redis / nodeJs / koa

Important and difficult technical points

Enable the keyspace notification function of redis (available only in versions 2.8.0 and above)

Try to use a separate redis db to implement

Using redis-based distributed locks to achieve that related events are not repeatedly consumed

The information that needs to be reused needs to be reflected in the key of the redis cache.

Redis cache key uses business prefixes to avoid duplicate name overrides

Prevent nodejs-level snooping failure caused by business service restart

"talk is cheap, show me the code?"

Core code

The core code const {saveClient, subClient} = require ('. / db/redis') / / Storage instance and subscription instance need to be two different instances const processor = require ('. / service/task') const config = require ('. / config/index') const innerDistributedLockKey ='& & _ &'/ the eigenvalue of key for internally used distributed locks const innerDistributedLockKeyReg = new RegExp (`^ ${innerDistributedLockKey} `) saveClient.on ('ready') Async () = > {saveClient.config ('SET',' notify-keyspace-events', 'Ex') / / Storage instance is set to push key expiration event console.log (' redis init success')}) subClient.on ('ready' () = > {/ / all processor subClient.subscribe can still be initialized after service restart (`_ _ keyevent@$ {config.redis.sub.db} _: subscrired`) / / subscription instance is responsible for subscription message subClient.on ('message', async (cahnnel, expiredKey) = > {/ / key of distributed lock does not listen if (expiredKey.match (innerDistributedLockKeyReg)) return / / simple distributed lock Instance consumption event const cackeKey = `$ {innerDistributedLockKey}-${expiredKey} `const lock = await saveClient.set (cackeKey, 2, 'ex', 5,' nx') / / the usage here enables a simple distributed lock if (lock = = 'OK') {await saveClient.del (cackeKey) for (let key in processor) {processor [key] (expiredKey) / / processor corresponds to the business logic executed after receiving the critical expiration notification Such as pushing text messages. Then set a timed expired key}) console.log ('subClient init success')}) servide/task (processor) exports.sendMessage = async function sendMessage (expiredKey, subClient) {/ / only handle expired events if (expiredKey.match (/ ^ send_message/)) {const [prefix, userId) in the relevant processor Type] = expiredKey.split ('-') let user = getUser (userId) if (user.phone) {push (message) / / pseudocode resetRedisKey (expiredKey, ttl) / / reset key to expire after a period of time This logic will be triggered again after expiration.

Summary

This function makes use of the key space notification function of redis to realize the timed task function based on users or different business scenarios. Since keyspace event notification is a more CPU-consuming operation, it is recommended to use a separate DB to handle it.

The basic usage shown here does not take into account the persistence of scheduled tasks. If redis fails to restart during use, it will cause all scheduled tasks to be lost. This event will also be lost if the subscription service fails and is not online when redis publishes the notification of key failure, or if the network problem is not received by the consumer.

The expired event for redis is not triggered when the key expires, but when the key is deleted. Redis will regularly clean up expired key, or check whether it expires when accessing key. Only then will the expired key trigger deletion operation, so there will be some small time gap (personal actual use does not affect the user experience).

Therefore, it is necessary to weigh the usage scenarios of scheduled tasks implemented using redis's expiration mechanism.

This is the end of this article on "how to achieve scheduled tasks in redis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report