Chris
Chris

Reputation: 1672

'Followers' and efficiency

I am designing an app that would involve users 'following' each other's activity, in the twitter sense, but I am not very experienced with database/query design/efficiency. Are there best practices for managing this, pitfalls to avoid, etc.? I gather this can create a very large load on the db if not done properly (or maybe even then?).

If it makes a difference it is likely that people will 'follow' only a relatively small number of people (but a person may have many followers). However this is not certain, and I wouldn't want to count on it.

Any advice gratefully received. Thanks.

Upvotes: 9

Views: 3896

Answers (4)

Damir Sudarevic
Damir Sudarevic

Reputation: 22187

The model is fairly simple. The problem is in the size of the Subscription table; if there are 1 million users, and each subscribes to 1000, then the Subscription table has 1 billion rows.

follower_model.png

Upvotes: 4

Christian
Christian

Reputation: 26387

You probably should read http://highscalability.com/ and it's articles on how this is managed by the big sites.

Upvotes: 1

Recurse
Recurse

Reputation: 3585

That depends on how many users you expect to need to support; how many followers you expect users to have; and what sort of funding/development-effort you expect to have access to should your answers to the previous questions prove optimistic.

For a small scale project I would likely ignore the database, design the application as a simple object model with User objects that maintain a List[followers]. Keep it all in RAM for normal operation and use an ORM to persist to a database periodically (probably postgresql or mysql).

For a larger project I would not be using a relational database at all; but exactly what I would use would depend on the specific details of the project.

If you are only trying to spike the concept, go with the ORM approach; but, keep in mind it won't scale.

Upvotes: 1

Tom Gullen
Tom Gullen

Reputation: 61729

Pretty simple and easy to do with full normalisation. If you have a table of users, each with a unique ID, you would have a TABLE_FOLLOWERS table with the columns, USERID and FOLLOWERID which would describe all the followers for each user as a one to one to many relationship.

Even with millions of assosciations on a half decent database server this will perform well and fast as long as you are using a good database (IE, not MS-Access).

Upvotes: 6

Related Questions