Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9078637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:40:26+00:00 2026-06-16T19:40:26+00:00

I have a client-server application that send user data to the cloud (Amazon EC2

  • 0

I have a client-server application that send user data to the cloud (Amazon EC2 + RDS + S3).

  1. Every user can have multiple devices connecting to the cloud & sending data at the same time
  2. Client application installed on each device is multi-threaded and eventually upload multiple data snippets at the same time.

I’d like to reliably track disk usage used in this context and I wonder how to do this in this context?

I have two ideas so far, but I’m not even sure they are correct:

Option 1: Add a trigger to mysql table? ie.

CREATE TRIGGER DiskUsage AFTER UPDATE OF Fully_Updated_File_Flag ON Files
BEGIN
    for each row
    begin
        UPDATE Users SET SpaceUsed = SpaceUsed + new.Size WHERE (new.Fully_Updated_File_Flag = 1) And UserID=
    end
END;

If I opt to use triggers, how am I supposed to dynamically inject the user id?


Option 2: Update mysql table via PHP? ie.

<?php

  SendFileToS3($file_name);
  mysql_query('UPDATE Stats SET Value = Value + ' . filesize($file_name) . ' WHERE user_id=' . $user_id);

?>

What if two instances are trying to update the same record? (I’m using Mysql 5.5.27-log / MyISAM), would this still work.


Note #1 Although I didn’t yet release my application, I still need something that scales well. Even if it means changing db engine all together.

Note #2 DB-related code is encapsulated in modular functions (ie. InsertIntoDB(), UpdateDB() & DeleteFromDB()). Plus all of these routines relies on CodeIgniter 2.1 with active record class.

This is to say that I could always make the switch if I have to (although I’d like to avoid that)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:40:28+00:00Added an answer on June 16, 2026 at 7:40 pm

    You should use MySQL Triggers instead of PHP code and you have to store the related user_id into diskusage table.

    I use InnoDB engine because of the CONSTRAINT. You can also use MyISAM, but you should remove the CONSTRAINT.

    REMARK

    I would use InnoDB because of Transactions and (more important here) Row-Locking.

    Table Structure (InnoDB)

    -- ----------------------------
    --  Table structure for `users`
    -- ----------------------------
    DROP TABLE IF EXISTS `users`;
    CREATE TABLE `users` (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `Name` VARCHAR(10) NOT NULL DEFAULT '',
      `SpaceUsed` BIGINT(20) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    --  Table structure for `diskusage`
    -- ----------------------------
    DROP TABLE IF EXISTS `diskusage`;
    CREATE TABLE `diskusage` (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `Filename` VARCHAR(50) NOT NULL DEFAULT '',
      `Size` BIGINT(20) NOT NULL,
      `user_id` INT(11) UNSIGNED DEFAULT NULL,
      `Fully_Updated_File_Flag` TINYINT(4) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `fk_diskusage_user` (`user_id`),
      CONSTRAINT `fk_diskusage_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    

    Table Structure (MyISAM)

    -- ----------------------------
    --  Table structure for `users`
    -- ----------------------------
    DROP TABLE IF EXISTS `users`;
    CREATE TABLE `users` (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `Name` VARCHAR(10) NOT NULL DEFAULT '',
      `SpaceUsed` BIGINT(20) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    --  Table structure for `diskusage`
    -- ----------------------------
    DROP TABLE IF EXISTS `diskusage`;
    CREATE TABLE `diskusage` (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `Filename` VARCHAR(50) NOT NULL DEFAULT '',
      `Size` BIGINT(20) NOT NULL,
      `user_id` INT(11) UNSIGNED DEFAULT NULL,
      `Fully_Updated_File_Flag` TINYINT(4) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `fk_diskusage_user` (`user_id`),
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    

    Thats all, together with some triggers on table diskusage.

    INSERT TRIGGER

    -- ----------------------------
    --  AFTER INSERT TRIGGER for `diskusage`
    -- ----------------------------
    delimiter ;;
    CREATE TRIGGER `diskusage_after_insert` AFTER INSERT ON `diskusage` FOR EACH ROW BEGIN
      IF NEW.Fully_Updated_File_Flag = 1 THEN
        UPDATE users
        SET
          SpaceUsed = SpaceUsed + NEW.Size
        WHERE
          id = NEW.user_id;
      END IF;
    END;
     ;;
    delimiter ;
    

    UPDATE TRIGGER

    -- ----------------------------
    --  AFTER UPDATE TRIGGER for `diskusage`
    -- ----------------------------
    delimiter ;;
    CREATE TRIGGER `diskusage_after_update` AFTER UPDATE ON `diskusage` FOR EACH ROW BEGIN
    
      -- same to DELETE TRIGGER
    
      -- decrease SpaceUsed with OLD Size for OLD user
    
      IF OLD.Fully_Updated_File_Flag = 1 THEN
        UPDATE users
        SET
          SpaceUsed = SpaceUsed - OLD.Size
        WHERE
          id = OLD.user_id;
      END IF;
    
      -- same to INSERT TRIGGER
    
      -- increase SpaceUsed with NEW Size for NEW user
    
      IF NEW.Fully_Updated_File_Flag = 1 THEN
        UPDATE users
        SET
          SpaceUsed = SpaceUsed + NEW.Size
        WHERE
          id = NEW.user_id;
      END IF;
    
    END;
     ;;
    delimiter ;
    

    DELETE TRIGGER

    -- ----------------------------
    --  AFTER DELETE TRIGGER for `diskusage`
    -- ----------------------------
    delimiter ;;
    CREATE TRIGGER `diskusage_after_delete` AFTER DELETE ON `diskusage` FOR EACH ROW BEGIN
    
      IF OLD.Fully_Updated_File_Flag = 1 THEN
        UPDATE users
        SET
          SpaceUsed = SpaceUsed - OLD.Size
        WHERE
          id = OLD.user_id;
      END IF;
    
    END;
     ;;
    delimiter ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a client application that needs to send messages to a server for
I have a client-server application that utilises MSMQ and NServiceBus for messaging. During some
I have a server application that receives some special TCP packet from a client
I have an application that runs on a client's server built on a SQL
Hi I have an application that operations like this.. Client <----> Server <----> Monitor
I have a client/server application where data is exchanged in XML format. The size
I am building an application where I have a server and a client that
I have to write simple client-server application that uses Unix datagram socket. Client may
I have client/server application where the client app will open files. Those files get
I have some client-server application. And as one of its part, I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.