How to write your own Redis key expire listener in Python
Tags: python, redis, rediskeyvalueexpire
Published: 2020-06-23 14:56:18

---
title: How to write your own Redis key expire listener in Python
published: true
date: 2020-06-23 14:28:59 UTC
tags: Python,Redis,RedisKeyValueExpire
canonical_url: https://blog.tanka.la/2020/06/23/how-to-write-your-own-redis-key-expire-listener-in-python/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-write-your-own-redis-key-expire-listener-in-python
---

Right now we are building Chatbot for our company which will work as a stateless API where it gives response based on the user message. For managing chat conversations, workflow, session and all other stuff we built an application using [Rocket Chat](https://github.com/RocketChat/Rocket.Chat). So API which we are building basically takes user input then using NLP extracts intent and entities, using those it creates the response message. We used Redis for maintaining user context.

So far everything good but we want to persist user context in Database rather than keeping it in Redis so that we can load user context in Redis whenever required from Database. Todo this requirement, did bit of research and found out that we can listen to key expire events in Redis but there is a small problem it will tell you which key is expired but it won’t give you its value and you can’t get value because the key is expired already. Then [this answer](https://stackoverflow.com/questions/18328058/redis-notifications-get-key-and-value-on-expiration) came to rescue me in this situation.

Let me show you how I implemented.

First, we need to do a small change in Redis Config to enable emitting key expired notifications by default it is disabled to save some CPU power. To do this we have 2 approaches

1. Run below command  
`redis-cli config set notify-keyspace-events Ex`
2. Update Redis config file and restart it. If you are using Ubuntu like me most probably you will find Redis config file at _“/etc/redis/redis.conf”_. Open the config file and update  
`notify-keyspace-events ""`  
`to`  
`notify-keyspace-events Ex`

I took the 2nd approach by the way. If you want to know more about this setting read [here](https://redis.io/topics/notifications). Now our Redis ready to emit key expire events so we need to write code listen to them.

First let’s install Redis package

```
pip3 install redis
```

We have this problem “Event will tell you which key is expired but it won’t give you its value and you can’t get value because the key is expired already.”

To tackle this problem what we will do is whenever we create a key and value with that, we will create shadow key too and we will put expire time to the shadow key and when we get shadow key expire notification we will get the original key and value and delete the original key from Redis. Hurray, our problem is solved.

This code will help you with that

{%gist https://gist.github.com/tankala/c71c74853d8c0503bb1c780637d18c42 file=redis-key-expire-listener.py %}

Now run python file

```
python3 redis-key-expire-listener.py
```

Now to test it open redis-cli

```
$ redis-cli
```

Let’s set our key and value

```
127.0.0.1:6379> SET testkey 100
OK
```

Then let’s create shadow key with expiration time 10 secs

```
127.0.0.1:6379> SET shadowKey:testkey "" EX 10
OK
```

If you see terminal where we run our listener code you will get this output after 10 secs when the shadow key got expired

```
Handler {'type': 'pmessage', 'pattern': b' __keyevent@0__ :expired', 'channel': b' __keyevent@0__ :expired', 'data': b'shadowKey:testkey'}
Got Value: b'100'
```

Congrats!!! Your Redis key expire listener is ready.

Peace. Happy Coding.