Amazon S3 API - can pseudo "lock" objects be used?

Viewed 1483

I'm thinking about using "lock" S3 objects in order to prevent simultaneous identical operations on a given S3 object. But I have great doubts about the validity of this technical solution.

More precisely, at the beginning of an operation on an object, a lock file corresponding to the name of the object and the type of operation being done would be created.

Example : at the beginning of myOperation on myObject, lck/myObject/myOperation object would be looked for and created if it does not exist.

This "lock" object would be destroyed at the end of the operation, whether it is successful or not.

I know the standard solution would be to use a database, but the application currently does not have one. Thus, if I could handle consistency with S3 only, this would be sufficient.

My concern is that this lock object system will run in a multi-threaded / and possibly multi-nodes architecture. Now, I've read that Amazon S3 support "read after write consistency".

Does it mean that if my application 1 (node 1) / thread 2 puts a lock object on a given bucket through the Java Amazon S3 API, this lock object will be instantaneously visible to other threads and other applications using the same API ?

3 Answers

Normally, I would recommend a database, but you mention that is not an option in this case.

As an alternative, you can use something like lockable. They provide advisory locks which can be used to control e.g. resource access like in your case.

The solution would have to use their HTTPS endpoints (as far as I can't tell they only have a Python client):

1. Acquire lock by making a request to https://lockable.dev/api/acquire/my-lock-name
2. Work on your object (read, edit, write)
3. Release the lock by making a request to https://lockable.dev/api/release/my-lock-name
Related