Designing Twitter kind of application in Java using redis

In our previous post we had built StackOverflow/Quora type of application: Designing a stackoverflow kind of application

In this article, we will try to design Twitter kind of application using redis as backend. A twitter app will basically have a collection of users where each user can have followers, each user can post tweets and those tweets should come in the timeline of the followers.

1. First lets create a jedis client which is a java wrapper for communicating with redis server

private Jedis jedis;
 
	public TwitterClone() {
		this.jedis = new Jedis("localhost");
	}

2. Next step would be to save users. A user will have some username and password. We will store them in a redis hashmap where key would be “user:USER_ID“. Unique USER_ID will be generated using createUserId method.
We will also maintain a map of userid-username mapping with the key “users“. This will help us in fetching username for a given userid easily

private Long saveUser(String username, String password) {
		Long id = createUserId();
		jedis.hset("user:" + id, "username", username);
		jedis.hset("user:" + id, "password", password);
 
		jedis.hset("users", id + "", username);
 
		return id;
	}
 
private Long createUserId() {
		return jedis.hincrBy("unique_ids", "user", 1);
	}

3. Each user can follow any other user. So we will maintain a list of userids of user’s followers where key would be “followers:USERID“. We are also maintaining the list of userids which each user is following to.

private void saveFollower(Long userid1, long timestamp, Long userid2) {
		saveFollowing(userid2, timestamp, userid1);
		jedis.zadd("followers:" + userid1, timestamp, userid2 + "");
	}
 
	private void saveFollowing(Long userid2, long timestamp, Long userid1) {
		jedis.zadd("following:" + userid1, timestamp, userid2 + "");
	}

4. Finally, we need to save tweets. Each tweet will have a unique tweet_id and for each tweet we will save text, timestamp and user_id of the user who tweeted it.
Each user will have a timeline of tweets which will be visible to him. We need to push this tweet to the timeline of each follower of the person who just tweeted. So now timeline of the user himself, as well as his followers, will have this tweet.

private Long saveTweet(Long user_id, String tweet, long timestamp) {
		Long id = createTweetId();
		jedis.hset("tweet:" + id, "tweet", tweet);
		jedis.hset("tweet:" + id, "timestamp", timestamp + "");
		jedis.hset("tweet:" + id, "ownerid", user_id + "");
 
		jedis.lpush("tweet_ids", id + "");
 
		Set followers = getFollowers(user_id + "");
		followers.add(user_id + "");
		for (String follower : followers) {
			jedis.lpush("timeline:" + follower, id + "");
		}
 
		return id;
	}

5. Let’s print the timeline of the given user

private void printTimeline(String userid) {
		List tweet_ids = jedis.lrange("timeline:" + userid, 0, 5);
		System.out.println("This is twitter timeline of : "
				+ jedis.hget("users", userid));
		for (String tweet_id : tweet_ids) {
			List tweet = jedis.hmget("tweet:" + tweet_id, "tweet",
					"timestamp", "ownerid");
			System.out
					.println("Tweet : " + tweet.get(0) + " - Posted at "
							+ tweet.get(1) + " by "
							+ jedis.hget("users", tweet.get(2)));
		}
 
		System.out.println(jedis.hget("users", userid) + "'s followers are : ");
		Set followers = jedis.zrange("followers:" + userid,0,3);
		for (String follower : followers) {
			System.out.println(jedis.hget("users", follower));
		}
 
	}

So this is a simple redis based implementation of basic twitter application. Here is the whole code :

package Cache;
 
import java.util.Date;
import java.util.List;
import java.util.Set;
 
import redis.clients.jedis.Jedis;
 
public class TwitterClone {
 
	private Jedis jedis;
 
	public TwitterClone() {
		this.jedis = new Jedis("localhost");
	}
 
	private Long createTweetId() {
		return jedis.hincrBy("unique_ids", "tweet", 1);
	}
 
	private Long createUserId() {
		return jedis.hincrBy("unique_ids", "user", 1);
	}
 
	private Long saveTweet(Long user_id, String tweet, long timestamp) {
		Long id = createTweetId();
		jedis.hset("tweet:" + id, "tweet", tweet);
		jedis.hset("tweet:" + id, "timestamp", timestamp + "");
		jedis.hset("tweet:" + id, "ownerid", user_id + "");
 
		jedis.lpush("tweet_ids", id + "");
 
		Set followers = getFollowers(user_id + "");
		followers.add(user_id + "");
		for (String follower : followers) {
			jedis.lpush("timeline:" + follower, id + "");
		}
 
		return id;
	}
 
	private void printTimeline(String userid) {
		List tweet_ids = jedis.lrange("timeline:" + userid, 0, 5);
		System.out.println("This is twitter timeline of : "
				+ jedis.hget("users", userid));
		for (String tweet_id : tweet_ids) {
			List tweet = jedis.hmget("tweet:" + tweet_id, "tweet",
					"timestamp", "ownerid");
			System.out
					.println("Tweet : " + tweet.get(0) + " - Posted at "
							+ tweet.get(1) + " by "
							+ jedis.hget("users", tweet.get(2)));
		}
 
		System.out.println(jedis.hget("users", userid) + "'s followers are : ");
		Set followers = jedis.zrange("followers:" + userid,0,3);
		for (String follower : followers) {
			System.out.println(jedis.hget("users", follower));
		}
 
	}
 
	private Long saveUser(String username, String password) {
		Long id = createUserId();
		jedis.hset("user:" + id, "username", username);
		jedis.hset("user:" + id, "password", password);
 
		jedis.hset("users", id + "", username);
 
		return id;
	}
 
	private void saveFollower(Long userid1, long timestamp, Long userid2) {
		saveFollowing(userid2, timestamp, userid1);
		jedis.zadd("followers:" + userid1, timestamp, userid2 + "");
	}
 
	private Set getFollowers(String userid) {
		return jedis.zrange("followers:" + userid, 0, -1);
	}
 
	private void saveFollowing(Long userid2, long timestamp, Long userid1) {
		jedis.zadd("following:" + userid1, timestamp, userid2 + "");
	}
 
	public static void main(String[] args) {
		TwitterClone twitter = new TwitterClone();
		Long userid1 = twitter.saveUser("UdayOgra", "******");
		Long userid2 = twitter.saveUser("ShikhaSharma", "******");
		Long userid3 = twitter.saveUser("ViratKohli", "******");
 
		twitter.saveFollower(userid1, System.currentTimeMillis(), userid2);
		twitter.saveFollower(userid1, System.currentTimeMillis(), userid3);
		twitter.saveFollower(userid2, System.currentTimeMillis(), userid1);
 
		twitter.saveTweet(userid1, "This is Uday's first tweet",
				System.currentTimeMillis());
		twitter.saveTweet(userid1, "This is Uday's second tweet",
				System.currentTimeMillis());
		twitter.saveTweet(userid2, "This is Shikha's first tweet",
				System.currentTimeMillis());
 
		twitter.printTimeline(userid1 + "");
	}
 
}

The output of above code would be:

This is twitter timeline of : UdayOgra
Tweet : This is Shikha’s first tweet – Posted at 1492194264270 by ShikhaSharma
Tweet : This is Uday’s second tweet – Posted at 1492194264269 by UdayOgra
Tweet : This is Uday’s first tweet – Posted at 1492194264255 by UdayOgra
UdayOgra’s followers are :
ShikhaSharma
ViratKohli

 

Mar Java Mit Java 🙂

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *