Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spoon's avatar

Designing an activity feed and fetching results from it

Hello.

'm trying to design an activity feed, but I have a small problem.

Here's my current table.

activity

id
user_id
status
activitytype_id (status, video, photo, etc)
created_at
location
video
I didn't add some unrelated columns to make it short.

This works when I want to create a feed for the friends' posts of a user. I basically create an array that contains id's of friends of the user and check for user_id's in the activity table and sort them by date.

But a user also follows groups and events, this is the tricky part.

What's the best way to include them (groups and events) in the feed? I can add a column to the activity called event_post and group_post and add posts id's as foreign keys to the activity table (null by default), but I'd like to hear your opinions.

If I query the community_user table (user_id and community_id as foreign keys) and get the list of groups a user follows, I will need to create a second query for group activity from the activities table in addition to user activity. Can I merge them together (they both have created_at columns since they're in the same table) and order by date? I'm new to laravel and I want to learn whether it's possible or not, also a good way or not.

I have also another question, should I keep group posts and event posts in another table like community_post and reference it on activities table or it is okay to hold them in activities table?

communities

id
communitytype_id (group or event)
name
created_at
user_id (FK from users table, owner of the group or event)
0 likes
8 replies
spoon's avatar

I will be using polymorphic relations for likes, shares, comments and images. That part is handled in theory for now, but I don't know how to include groups and events (since a user can follow a group, not a user?) and order them by date along with user updates, as I've explained above.

If I need to write more than one query (one for friends statuses, one for group and event updates) for the feed, can I order all of them by date, that's my actual question.

ohffs's avatar

Possibly copy the way twitter does (or did?) it and put the activity into a user-specific feed? So instead of looking up a global activity feed for things this user is interested in, when a new activity is posted it's added to all the subscribers own feeds? Kind of reverse direction from how you're doing it just now? So I guess you might end up with a table that has :

id
subscriber_id
activity_id
created_at
updated_at

And just populate a new entry when an activity is posted? Off the top of my head :

function storeActivity($request) {
  ...
  $activity->save();
  $followers = $activity->creator()->followers();
  foreach($followers as $follower) {
   $follower->addSubscribedActivity($activity->id);
  }
}

Something like that anyway. I may well be talking rubbish though - it's been a long day ;-)

2 likes
spoon's avatar

@ohffs, I'm trying to understand your answer, it's been a long day for me, too.

The way you're saying, I need to create an additional table called feeds and relate activity_id's with user_id's, is that correct?

But there's a small problem, if there are 5000 followers of a group, there'll be 5000 new records in the feeds table for each update (when the group owner post something) of a group, right? It seems logical to create a feeds table, but I want to make sure I understood correct.

Thank you for your answer, by the way.

ohffs's avatar
ohffs
Best Answer
Level 50

@sid405 it's more twitters idea than mine, but I'll take the credit anyway ;-)

@razerdeathadder yeah - it's a problem for twitter too ;-) There's an old post about their 'fanout' process here : http://highscalability.com/blog/2013/7/8/the-architecture-twitter-uses-to-deal-with-150m-active-users.html . I seem to remember they are heavy users of redis for this kind of thing, but I might be getting it muddled with another part of their architecture.

2 likes
sid405's avatar

Yea in the scalability sense would be an issue. I mean you're either stuck with a massive table or a plethora of user tables. Like you do say redis would come in handy or converting mysql to nosql like Fb does.

Either way the fact is that passed a certain scalability point, your plaform/app has hit the big time and you can afford to make some technical considerations in another light :)

1 like
mazinoukah's avatar

i am trying to implement something similar, however i have issues. If i have to loop through 5000 or 50000 followers of a user/users in order to update their feeds, it would definitely take a long time and i would not want the user to wait while this happens, so i would want to use laravel queues right ? okay right, what if 100 or 10000 people post a status or comment on a post or infact just create some sort of activity at about the same time ? the queue becomes very long and if they all have lots of followers it could take hours before the last persons activity is pushed to all his followers.

i am really confused about how to implement this stuff, any help would be deeply appreciated. thanks @sid405 @ohffs @spoon

Please or to participate in this conversation.