Skip to content

yaolizheng/twitter

Repository files navigation

Basic twitter server

Overview

This project is a basic twitter server.

Couple of rules simplifies the logic:

  • user can't follow or unfollow himself.
  • feeds will be generated by list of latest 10 tweets of all followee.

Architecture

Components:

  • Application load balancer
  • Twitter API server
  • Memcached server
  • Cassandra server
  • Log server

Basic workflow

Detail workflow refers twitter.py

client -> load balancer -> API server -> memcached server -> Cassandra server

Cache strategy

  • relation cache
<user_id, [followee_id, followee_id, followee_id...]>
  • tweet cache
<tweet_id, tweet_content>
  • 10 latest tweets per user
<user_id, [(tweet_id, tweet_time, tweet_user), (tweet_id, tweet_time, tweet_user)...]>

Getting Started

Prerequisites

You will need a Debian 8 box, memcached server and Cassandra server to run the server.

Installing

  • copy source code to /usr/local/lib/python2.7/dist-packages/twitter/
  • run deployment script
cd /usr/local/lib/python2.7/dist-packages/twitter/tools && ./deployment.sh
  • install config file and change parameters
cp config /etc
  • install bin file and service file
cp twitter /usr/sbin/
cp twitter.service /lib/systemd/system/
systemctl daemon-reload
systemctl enable twitter
service twitter restart

Modules

Server modules

http_server.py: main module for web application

db/model.py: cassandra database module

cache.py: client module for memcached

twitter.py: core service module

handlers/base.py: base handler module

handler/feed.py: handler module for getting feeds

handler/follow.py: handler module for following and unfollowing

handler/status.py: handler module for health check

handler/tweet.py: handler module for posting tweet

handler/user.py: handler module for adding user

Test modules

client_test.py: base module for testing

functional_test.py: module for functional test

scale_test.py: module for scale test

API design

URL                     method      data              parameter         description                             return value
/follow/follower_id     POST        followee_id                         Follower_id follows followee_id         
/follow/follower_id     GET                                             Get followee for follower_id            list of followees
/follow/follower_id     DELETE      followee_id                         Follower_id unfollows followee_id
/tweet/user_id          POST        tweet                               Post tweet for user_id                  tweet id
/tweet/user_id          GET                                             Get user_id's tweets                    list of tweets
/tweet/user_id          DELETE      tweet_id                            Delete tweet_id
/feed/user_id           GET                                             Get feed for user_id                    list of tweets       
/user                   POST        name                                Create user                             user_id
/user                   GET                           user_id           Get user_id                             user name
/user                   DELETE      user_id                             Delete user_id

Database schema

class User(Model):

    __table_name__ = 'user'

    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    name = columns.Text()

class Tweet(Model):

    __table_name__ = 'tweet'

    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    user_id = columns.UUID(index=True)
    time_stamp = columns.Text()
    content = columns.Text()

class Relation(Model):

    __table_name__ = 'relation'

    follower = columns.UUID(primary_key=True)
    followee = columns.UUID(primary_key=True)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published