I am working in Laravel and presenting you a simplified version of what I am trying to achieve.
Assume a JSON file called site.json
containing the following:
{
usedDiscounts: 10
}
Every time a user subscribes with a discount, I decrement the shown usedDiscounts
property. Now, I have a dashboard from where I can set this property to a given integer to begin with (just like 10
in this case).
For every user, this site.json
file is read in order to showcase the remaining discounted seats. And obviously it’s written to whenever a user subscribes, or when I set a given value from my dashboard.
My concern is as follows: Is there any chance of any errors because of reading this file while it’s being written. Or for example, what if two users (somehow) subscribe at the same time, resulting in the decrementing being done simultaneously? What effects could this approach of using the JSON file possibly have?
I am not well-versed with knowledge of file handling at the lower system level, so obviously I am not sure how this would work. And remember that I am using Laravel, in particular Storage::json()
and Storage::put()
to write to the file. So would that be safe in these extreme cases mentioned above?