title: Tcp socket server freezes after some time
body: <p>I have data coming from different GPS tracker devices. Its a unidirectional data which means I am receiving the data and pushing it into the MySQL DB and firebase. I have a total of 300 Devices connected which are sending data to my server every 10 seconds.</p>
  <p>My Server Specs are</p>
  <pre><code>AWS t2.xlarge
  CPU: 4
  Ram: 16GB
  </code></pre>
  <p>what happens is that after 3 days, It stops sending the data into the database. It doesn't stop the server. It just freezes. If I do this</p>
  <pre><code>sudo netstat -tulnap | grep :8050
  </code></pre>
  <p>It does show the process and all that but I do not see any data pushing into the DB. It just freezes. I had to reboot the server or I had to stop it using forever and restart it again</p>
  <pre><code>forever stop  --minUptime 36000000000  server.js
  </code></pre>
  <p>And when I go to my <code>PHPMyAdmin</code> and check the monitor screen what I can see that I have very little free memory left and cache memory is into GBs. It seems like all the memory went into cached memory which left my server freezes. I have no idea where I am doing wrong which is causing it to freeze. For example, at the moment as I am posting a question this is my server current status</p>
  <p><a href="https://i.stack.imgur.com/oWsOG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oWsOG.png" alt="enter image description here" /></a><a href="https://i.stack.imgur.com/QwovQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QwovQ.png" alt="enter image description here" /></a></p>
  <p>As you can see above that in 19 hours cache has been increased and its keep growing. Below is the code</p>
  <pre><code>if (cluster.isMaster) {
      // Fork workers.
      for (var i = 0; i &lt; numCPUs; i++) {
          cluster.fork();
      }
  
      cluster.on('death', function(worker) {
         // console.log('worker ' + worker.pid + ' died');
          cluster.fork();
      });
  } else {
      net.createServer(function(socket) {
         // console.log('received connection...');
  
          socket.on(&quot;error&quot;, function(err) {
           //   console.log(&quot;socket error: &quot;)
             // console.log(err.stack);
              socket.destroy();
          });
  
          socket.on('data', function(data) {
          });
      });
  }
  </code></pre>
  <p>Recently I have made one change in the code but still it didn't work out which was to close the socket after receiving the data every 5 seconds</p>
  <pre><code> socket.on('data', function(data) {
  
      //parse data and  push data into db and firebase
      socket.end();
   
   });
  </code></pre>
  <p>That's how I am doing MySQL queries</p>
  <p><strong>database.js</strong></p>
  <pre><code>var mysql = require('mysql');
  
  
  var pool  = mysql.createPool({
      connectionLimit    : 8,
      waitForConnections : true,
      queueLimit         : 300,
      host     : 'localhost',
      user     : 'username',
      password : '123456',
      database : 'dummy'
  
  
  });
  
  
  module.exports = pool;
  </code></pre>
  <p><strong>Server.js file</strong> (only database code I have pasted as full code has 1400 lines of code)</p>
  <pre><code>    const db = require('./database');
      function getCarDetails(car_id,callback) {
      db.getConnection((err, connection) =&gt; {
          if(err) throw err;
      console.log('connected as id ' + connection.threadId);
      let selectQuery = 'SELECT * FROM ?? join user_info ON car.user_id = user_info.user_id WHERE ?? = ?';
      let query = mysql.format(selectQuery, [&quot;car&quot;, &quot;id&quot;, car_id]);
      // query = SELECT * FROM `todo` where `user` = 'shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }
      // rows fetch
      if(data.length &gt; 0){
  
          return callback(data[0]);
  
  
      }else{
  
          return callback(false);
  
      }
  
  
  });
  });
  }
  
  
  function updateIgnitionNotification(car_id,acc_on,acc_off,acc,speed,updated,callback) {
      db.getConnection((err, connection) =&gt; {
          let updateQuery =  'UPDATE car SET notification_acc_on = ?,notification_acc_off = ?,acc = ?,speed = ?,updated = ? Where id = ?';
      let query = mysql.format(updateQuery, [acc_on, acc_off,acc,speed,updated, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  function updateLastUpdatedData(car_id,current_datetime,status,acc,monitoring,max_speed,callback){
      db.getConnection((err, connection) =&gt; {
          let updateQuery =  'UPDATE car SET updated = ?,status = ?,acc = ?,monitoring = ?, max_speed = ? Where id = ?';
      let query = mysql.format(updateQuery, [current_datetime,status,acc.toUpperCase(),monitoring, max_speed, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  
  function updateCommand(car_id,command,callback){
      db.getConnection((err, connection) =&gt; {
          let updateQuery =   'UPDATE car SET command = ? Where id = ?';
      let query = mysql.format(updateQuery, [command, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  
  
  function updateCarLockNotification(car_id,lock_notification,callback){
      db.getConnection((err, connection) =&gt; {
          let updateQuery =   'UPDATE car SET lock_notification = ? Where id = ?';
      let query = mysql.format(updateQuery, [lock_notification, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  
  function updateOverSpeedNotification(car_id,notification_over_speed,callback){
  
      db.getConnection((err, connection) =&gt; {
          let updateQuery =     'UPDATE car SET notification_over_speed = ? Where id = ?';
      let query = mysql.format(updateQuery, [notification_over_speed, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  
  
  function updateGeoFenceOutsideAttempt(car_id,geofence_attempt,callback){
  
      db.getConnection((err, connection) =&gt; {
          let updateQuery = 'UPDATE car SET geofence_outside_attempt = ? Where id = ?';
      let query = mysql.format(updateQuery,  [geofence_attempt, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  
  
  function updateGeoFenceInsideAttempt(car_id,geofence_attempt,callback){
  
      db.getConnection((err, connection) =&gt; {
          let updateQuery = 'UPDATE car SET geofence_inside_attempt = ? Where id = ?';
      let query = mysql.format(updateQuery, [geofence_attempt, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  function updateBatteryNotification(car_id,battery,callback){
  
      db.getConnection((err, connection) =&gt; {
          let updateQuery = 'UPDATE car SET notification_battery = ? Where id = ?';
      let query = mysql.format(updateQuery, [battery, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  
  
  
  function saveNotificationLog(log,callback){
  
      db.getConnection((err, connection) =&gt; {
          //   let insertQuery = 'INSERT INTO ?? (??,??) VALUES (?,?)';
          //let query = mysql.format(insertQuery,[&quot;log&quot;,&quot;user&quot;,&quot;notes&quot;,data.user,data.value]);
  
          // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
          connection.query('INSERT INTO log SET ?', log, (err, res) =&gt; {
          connection.release();
      if(err) {
          return callback(res);
      }else{
  
          return callback(res);
      }
  
  
  });
  });
  }
  
  
  function saveHistory(history,callback){
  
      db.getConnection((err, connection) =&gt; {
          //   let insertQuery = 'INSERT INTO ?? (??,??) VALUES (?,?)';
          //let query = mysql.format(insertQuery,[&quot;log&quot;,&quot;user&quot;,&quot;notes&quot;,data.user,data.value]);
  
          // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
          connection.query('INSERT INTO car_history SET ?', history, (err, res) =&gt; {
          connection.release();
      if(err) {
          return callback(res);
      }else{
  
          return callback(res);
      }
  
  
  });
  });
  }
  
  
  function updateGeoFenceNotification(car_id,notification_inside,notification_outside,callback){
  
      db.getConnection((err, connection) =&gt; {
          let updateQuery = 'UPDATE car SET notification_inside = ?,notification_outside = ? Where id = ?';
      let query = mysql.format(updateQuery, [notification_inside, notification_outside, car_id]);
      // query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
      connection.query(query, (err, data) =&gt; {
          connection.release();
      if(err) {
          console.error(err);
          //throw err;
          return;
      }else{
  
          return callback(data);
      }
  
  
  });
  });
  }
  </code></pre>
  <p>Please help me to resolve this issue</p>