Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking with S3

Tags:

What's the recommended way to implement a simple locking mechanism to be used in conjunction with S3?

Example of what I want to do:

  • acquire lock by object id
  • read object from S3
  • modify data
  • write object to S3
  • release lock

Ideally looking for a cloud based locking mechanism. I could use memcached locally, but then I have to deal with scaling that. I don't see an obvious way to implement lightweight locking with any AWS APIs, but it seems like a common problem.

I wonder if you could use SimpleDB to do an atomic acquire lock operation. Has anyone tried that?

like image 357
James Cooper Avatar asked Aug 07 '10 17:08

James Cooper


People also ask

Can S3 lock files?

When you lock an object version, Amazon S3 stores the lock information in the metadata for that object version. Placing a retention period or legal hold on an object protects only the version specified in the request. It doesn't prevent new versions of the object from being created.

What is object locking in S3?

Object Lock is an Amazon S3 feature that blocks object version deletion during a user-defined retention period, to enforce retention policies as an additional layer of data protection and/or for strict regulatory compliance.

Is S3 object lock free?

There is no additional charge for using this feature – so go on and add some retention dates to your objects in S3. Many AWS customers use AWS' WORM storage capabilities (S3 Glacier Vault Lock and S3 Object Lock) today.

How do I enable object lock on an existing S3 bucket?

You can only enable Object Lock for new buckets. If you want to turn on Object Lock for an existing bucket, contact AWS Support. When you create a bucket with Object Lock enabled, Amazon S3 automatically enables versioning for the bucket.


1 Answers

Ok, I spent some time this morning playing with boto and I think I have a solution that works using SimpleDB. You need the latest boto release so that conditional puts and consistent reads are supported.

Example code here: http://pastebin.com/3XzhPqfY

Please post comments/suggestions. I believe this code should be fairly safe -- my test in main() tries it with 10 threads.

One thing I haven't addressed is that S3 reads are not consistent (right?), so in theory a thread may be operating on an old copy of the S3 value. It looks like there may be a workaround for that as described here:

http://www.shlomoswidler.com/2009/12/read-after-write-consistency-in-amazon.html

like image 163
James Cooper Avatar answered Sep 25 '22 02:09

James Cooper