I have build a REST API with Django REST framework. In the app there is a need for facebook-type notifications ( new friend request, new message etc. ). Currently I’m handling this using long-polling:
- front client sends GET request
- my REST view searches for new objects and returns them instantly if there are any, else its searching for 20 seconds and returns empty response if there are None
- a new GET request is sent instant after the response is received ( from the front client )
NOTE: we are not using websockets, if needed please write me
I want to replace this method with django/redis since I think my long-polling method is abusing the DB a lot and I think Redis with its speed and structure can help a lot.
Are there any suggestions on how would I accomplish something like this?
2
Answers
With Redis, you can cache the notifications and speed up the process of showing things on Front end only with abusing the database a lot less.
You could for example have keys like:
Now, everytime a user with user ID =
<user_id>
receives a new message, you can update the redis cache to update the<user_id>_new_message
to 1.Now there shall be a function/AJAX call checking for
<user_id>_new_message
, and update the notification on front end toprevious unread notif count + 1
. And you can go about updating Database (if you need to) say every 20 minutes in bulk for all the users.Now, coming to actual updations. Since your system requires interaction between the server and the user end (Like showing the person a message and be able to reply to it), Redis cannot help you here. You will require services like websockets. You require interaction between user and API both for messaging and friend requests.
Using a memory based key-value store, is much better suited for your current use case, since it will hugely decrease load on your database and speed up your application.
Redis, is more than an in memory key-value store, so you can utilize it to achieve your goal easier.
I’m writing a simple implementation of what you want down here, you can most probably build on this to achieve what you want.
In Short
We keep all notifications of a user in a HashMap in redis with the
key of the HashMap being named after the user.
HashMap is a mapping of notificationId to notificationData (In Json format).
Metadata of the notification is kept inside the notification data body. (Things like date, read status, …)
You can also build more functionality or play around with different capabilities of redis to create a near-realtime notification backend for you website/application. (I’ve duplicated the redis connection creation part, user_id and … so as the answer can be clearer)
Cheers