1

I am looking for some advice. I will be working on user follow system (like twitter) for my site. I expect it to have a large number of users using this system.

So, I would like to know what is better to use to store all the data for this system. Should I use user meta, custom taxonomies or should I work directly with the database?

I was thinking about creating some user meta, so each user has meta for followers and a meta for following, each would be filled with users' ids. Is there a better way from the stanpoint of scalability?

Thanks!

closed as too broad by markratledge, Tom J Nowell, shea, tfrommen, Nicolai Feb 1 '14 at 11:16

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • please define what exactly is "better" for you. – Mark Kaplun Jan 26 '14 at 9:13
  • better for scalability, in case the site grows to a large number of users. As of now, I decided to create a new table in the database with two columns, one for users id and the other for who the user is following. – Gixty Jan 27 '14 at 2:16
  • if you are still interested in an answer please edit your question and change/add the tags to something performance related. – Mark Kaplun Jan 27 '14 at 7:23
  • This question is too broad, and the appropriate answer is context dependent, namely what kind of query will you need to do. E.g. what will need to be queried most often? Who is following you? Or who are you following? Do you want speed of query or speed of deletion/insertion/edit? How accurate do you want the data to be? Who's following you at this very moment? Approximate to within the last hour? When talking about scale, it's not as simple as which data structure is best – Tom J Nowell Jan 27 '14 at 15:29
1

In the case where you want to store each piece of data as a unique entry (row) in the database, then a custom table, as you've already done, is a perfectly fine solution.

On the other hand...

You could use the usermeta table to do the exact same thing, storing multiple meta keys with the same name as unique entries,

Entries in the usermeta table would look like the following:

umeta_id   user_id  meta_key   meta_value
    1         1     follower     user_1
    2         1     follower     user_2
    3         1     follower     user_3
    4         1     randomkey    abcdef
    5         1     follower     user_4

As you can see in the example above, the fourth entry in the usermeta table is for a piece of completely unrelated metadata, so that is what you will contend with, all kinds of data mixed into the one table. Overtime, if this table contains a lot of extra meta information in addition to your follow-data then it can possibly effect the performance of your queries.

Personally I would create a custom table and only store user_id and follower_id (possibly the follower_username too).

primary_id  user_id  follower_id follower_username
    1          1         2            name2
    2          1         2            name2 
    3          2         5            name5  
    4          1         8            name8 
    5          1         3            name3 

Storing the username along with the ID is optional but useful.

Then I'd also store a serialized value of all people following a user against the individual user within usermeta table which you can do periodically via wp_cron. If you need to show a given user who is following them, you can retrieve an individual usermeta meta_key, unserialize the data and present it accordingly.

If you happen to also store the username of the follower, the serialized data for a user will also contain the username which will avoid you needing to run a lookup on the returned ID if all you need to show is a username and possibly a link to the user.

The ID is returned if you need to perform any additional data retrieval and manipulation which you can do from the username alone but, an ID is always handy and for me, it's preferred.

Of course all of this is highly generalized because it really depends on your extended use case as to what you may do.

  • This is is great, I did create a custom table where I am storing user_id and follower_id, but I didnt think of storing a serialized value of all people following a user. I am using usermeta for storing how many followers/following a user has. However, I am running the query every time a user follows/unfollows someone. Would I go with something like that for storing the serialized data? Another thing, I still dont see why I would need the follower_username in the custom table, cant I simple use its id? – Gixty Jan 27 '14 at 15:59
  • You don't need to store the username, but at the time of following, the lookup is cheap to perform and if you needed to show a user who (by name) is following them, you would already have that data stored in a serialized value against their user_id in the usermeta table. If you are only showing counts, then omit the username (I personally would store it, although not neccessary). continued... – userabuser Jan 27 '14 at 16:13
  • ... Everytime a user followed someone, I would query the user who was just followed, grab all their followers and update their metadata. OR better still, if its just a follow (not unfollow), grab ONLY the usermeta and update it with new follower id, no need to query the custom table (just add data to the custom table, then get usermeta key and then update it). – userabuser Jan 27 '14 at 16:14
  • 1
    Great, thanks for the advice. I just marked your answer. – Gixty Jan 27 '14 at 16:51
  • No problem, all the best. – userabuser Jan 27 '14 at 16:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.