I have a client-server application that send user data to the cloud (Amazon EC2 + RDS + S3).
- Every user can have multiple devices connecting to the cloud & sending data at the same time
- 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)
You should use MySQL Triggers instead of PHP code and you have to store the related
user_idintodiskusagetable.REMARK
I would use
InnoDBbecause of Transactions and (more important here) Row-Locking.Table Structure (InnoDB)
Table Structure (MyISAM)
Thats all, together with some triggers on table
diskusage.INSERT TRIGGER
UPDATE TRIGGER
DELETE TRIGGER