/twitter

A minimal Twitter clone, built with Node and Redis.

Primary LanguageJavaScript

twitter

Node.js CI

A minimal Twitter clone, built with Node and Redis.

Features

Currently the following features are implemented.

  • Sign up and log in
  • Follow and unfollow user
  • Post tweet

Data layout

Users

User IDs are generated sequencially by Redis's INCR:

INCR user:ids => 123

Each User is stored in Redis Hashes. The Redis key of user is something like user:123 and fields are:

name (User name)
pass (Encripted password)
fullname (User's full name)

Every time someone registered, this app execute the following steps.

INCR user:ids => 123
HMSET user:123 name "bob" pass "asdfjkl;" fullname "Bob Marley"

Followers and followings

For followers and followings, Redis's Sorted Set data structure is a good fit because whenever someone start following you, or you start following someone else, that history is always sorted by insertion order (the timestamp) using the score of Sorted Set.

user:123:followers => Sorted Set of user IDs of all the followers users
user:123:followings => Sorted Set of user IDs of all the follwings users

When adding new follower:

ZADD user:123:followers 1401267618 456 =>> Add user 456 with time 1401267618

Tweets

Tweets also have sequencial IDs, generated by Redis's INCR which is same as User ID.

INCR tweet:ids => 789

Every time someone tweets, that tweet is stored in a hash, where each key is something like tweet:789, with the following fields:

text (Body of the tweet)
created_at (Timestamp created)
user_id (User ID)

Timeline

Another interesting part of this application is the Timeline. where users see all the latest updates in user's home page. Since we show those updates in chronological order, from the most recent update to the oldest, again, the Sorted Set data structure is the good fit for this use.

The app needs to create two Timelines, one is home timeline where all new users will see, and the other is user timeline where all updates are customized per user.

user:123:user_timeline => Sorted Set of tweet IDs posted by the user
user:123:home_timeline => Sorted Set of tweet IDs posted by the user and the users they follow

For example, retrieving latest 5 tweet IDs from the user timeline is:

ZREVRANGE user:123:home_timeline 0 4

Development

Download project

Clone this repository and cd to the project directory, run:

$ npm install

This will install all required dependencies.

Install Docker

This project uses Express as a web application framework. The Express app is containerized with Docker enabling you to run the Node.js application and Redis in containers.

To build and run the Node.js application, you can use Docker Compose commands. Compose is a tool for defining and running multi-container Docker applications. With Compose, you can create and start the application and dependent middleware with a single command.

From the project directory, start up the application by running:

docker-compose up

Docker Compose pulls a Redis image, builds an image for the application code, and starts the services defined in docker-compose.yml file.

Enter http://localhost:49160/ in a browser to see the application running.

Deployment

For deployment, Procfile is packaged as a default configuration to integrate with Heroku. If you are not familiar with Heroku I would recommend you to first read through the documentation of how to get started with Node.js on Heroku.

The whole deployment process should be as simple as the follwing 3 steps.

Login to Heroku:

$ heroku login

Create an app on Heroku:

$ heroku create

Deploy an app:

$ git push heroku master

References

  • antirez/retwis - A Twitter-toy clone written in PHP and Redis
  • twissandra/twissandra - Twissandra is an example project, created to learn and demonstrate how to use Cassandra.

License

MIT © Tatsuya Oiwa