Reputation: 203
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:
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?
Upvotes: 1
Views: 1538
Reputation: 4516
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, ...)
from redis import StrictRedis
import json
# Function to add new notifications for user
def add_notification(user_id, notification_id, data):
r = StrictRedis(host='localhost', port=6379)
r.hset('%s_notifications' % user_id, notification_id, json.dumps(data))
# Function to set the notification as read, your Frontend looks at the "read" key in
# Notification's data to determine how to show the notification
def set_notification_as_read(user_id, notification_id):
r = StrictRedis(host='localhost', port=6379)
data = json.loads(r.hget('%s_notifications' % user_id, notification_id))
data['read'] = True
add_notification(user_id, notification_id, data)
# Gets all notifications for a user, you can sort them based on a key like "date" in Frontend
def get_notifications(user_id):
r = StrictRedis(host='localhost', port=6379)
return r.hgetall('%s_notifications' % user_id)
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
Upvotes: 2
Reputation: 4320
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:
<user_id>_new_message
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 to previous 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.
Upvotes: 0