High CPU usage with single user on production server

Hello, My app showing very high CPU usage with single user only, whenever i do any insert or update cpu goes 70-80% or sometimes 100%.

Meteor Version: 1.6.0.1

I am using reywood:publish-composite, matb33:collection-hooks for db relations and doing some subscription using Iron router controller as follow:

waitOn (){
    if( Meteor.userId() && typeof navigator != 'undefined'){
      let userId = Meteor.userId();
      if(navigator && navigator.onLine )
      {
        return [
          Meteor.subscribe('events', userId,{
            onReady: function () {  }
          }),
          Meteor.subscribe('notificationlist',userId,{
            onReady: function () {  }
          }),
        ];
      }
    }
  }

Publications:

Meteor.publishComposite('events', userId => {
  return {
    find (){
      let userId = this.userId || "";
      return Invites.find({userId});
    },
    children: [{
      find (invite){
        return Events.find({_id: invite.eventId});
      },
      children: [{
        find (event){
          return Posts.find({eventId: event._id});
        },
         children: [{
            find (post){
              return Meteor.users.find({_id: post.userId}, {fields: {
                'profile.firstName': 1,
                'profile.lastName': 1
              }});
            },
          }, {
            find (post){
              return EventImages.find({postId: post._id,imageId: {$exists : true}});
            },
          }, {
            find (post){
              return Comments.find({postId: post._id});
            },
            children: [{
              find (comment){
                return Meteor.users.find({_id: comment.userId}, {fields: {
                  'profile.firstName': 1,
                  'profile.lastName': 1
                }});
              },
            }],
          }],
      },{
        find (event){
          return Groups.find({eventId: event._id});
        },
      }, {
        find (event){
          return Invites.find({eventId: event._id});
        },
      }, {
        find (event){
          return EventImages.find({eventId: event._id});
        },
      }, {
        find (event){
          return Activities.find({eventId: event._id});
        }
      }, {
        find (event){
          return EventImages.find({eventId: event._id,imageId: {$exists : true}});
        },
      }],
    }],
  };
});

Meteor.publishComposite('notificationlist', userId => {
  var status = ['no reply'];
  var userId = this.userId || "";
  inviteListData = Events.find({creating: true});
  var eventListRecords = inviteListData.map(function(p) {
    return p._id;
  });

  var inviteList = Invites.find({userId: userId, inviteStatus: {$in: status},eventId:{$nin: eventListRecords}});
  var eventList = inviteList.map(function(p) {
    return p.eventId;
  });

  return {
    find() {
      return Notificationscenter.find({
            eventId: {
                $in: eventList
            },
            cleredUsers: {
                $nin: [userId]
            },
            senderId: {
                $nin: [userId]
            },
            $or: [{
                userId: "0"
            }, {
                userId: userId
            }]
        });
    },
    children: [{
      find (notification){
        return Invites.find({_id: notification.inviteId});
      },
    }, {
      find (notification){
        return Meteor.users.find({_id: notification.senderId});
      },
    }],
  }
});

I have other publications as well, I am rendering all my templates under events template except the login and login page is working correctly as it have no relational subscription but when i logged in all templates and subscription get executed that making cpu high usage. Whenever i do any update/insert into Invites, cpu go high with single user.

I have routing for login and events page. For events inner pages i am using params to display active content like all templates get rendered once and changing the views of app based on params.

App is working fine on local machine(MAC) but when i deployed on Galaxy with 2GB container size, it showing High CPU usage or 100% that causing app unresponsive/crashed.

Please advice.

Thanks

1 Like

Did you install the galaxy APM? That is generally the first place to look for performance issues. Also you should ensure all the things you’re querying on have an index via Collection._collection._ensureIndex

Yes, i have installed APM and index is also done. I think the problem is with publishComposite(‘events’), it runs on every changes how i can optimise this or should i switched to another package?

You can try this package, paying close attention to the section about performance. I’m not sure if publish composite is bulking the queries or not. Likely you’re not reusing observers with that package. The publish relation one creates observers on the collections and publishes changed messages over the wire. I believe the one you’re seeing is actually requerying on every change and then running a transform on the server side over the results.