Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB for PHP sessions

we are currently using a NAS for PHP session files storage for an array of autoscaling load balanced app servers.

We are interested to replace this with a more robust solution and DynamoDB from Amazon looks interesting. I see one possible issue here, documented here:

http://thwartedefforts.org/2006/11/11/race-conditions-with-ajax-and-php-sessions/

I suspect DynamoDB does not support object locking. Any workarounds you can think of?

If you have any experience from other NoSQL systems used for PHP sessions also feel free to jump in as the learnings might be similar.

Thanks in advance

like image 317
webgr Avatar asked Jan 25 '12 10:01

webgr


People also ask

Can DynamoDB managing Web session?

Using DynamoDB for session storage alleviates issues that occur with session handling in a distributed web application by moving sessions off of the local file system and into a shared location. DynamoDB is fast, scalable, easy to set up, and handles replication of your data automatically.

When should you not use DynamoDB?

Even though the solution has many benefits, one of the major drawbacks is that the solution lacks an on-premise deployment model and is only available on the AWS cloud. This limitation does not allow users to use DynamoDB for applications that require an on-premise database.

Is DynamoDB good for transactional?

Amazon DynamoDB transactions simplify the developer experience of making coordinated, all-or-nothing changes to multiple items both within and across tables. Transactions provide atomicity, consistency, isolation, and durability (ACID) in DynamoDB, helping you to maintain data correctness in your applications.

Is DynamoDB good for time series data?

In this post, I show you how to use such an anti-pattern for DynamoDB, but it is a great fit for time-series data. Unless you opt for on-demand capacity mode, every DynamoDB access pattern requires a different allocation of read capacity units and write capacity units.


3 Answers

Using DynamoDB's conditional writes, you could implement a pessimistic locking scheme similar to the way PHP's default session handler works.

Someone else has also made a request for a DynamoDB session handler: https://forums.aws.amazon.com/thread.jspa?messageID=328060.

Updated: The AWS SDK for PHP now includes a session handler for DynamoDB. See https://github.com/amazonwebservices/aws-sdk-for-php/blob/master/extensions/dynamodbsessionhandler.class.php and http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#i=DynamoDBSessionHandler

Updated: There is an article about the DynamoDB session handler on the AWS blog: http://aws.typepad.com/aws/2012/04/scalable-session-handling-in-php-using-amazon-dynamodb.html

like image 136
Jeremy Lindblom Avatar answered Oct 08 '22 15:10

Jeremy Lindblom


Interesting idea (+1) - and an elaborate article indeed, just skimmed it for now though ;)

Regarding other NoSQL options for PHP session storage you might want to check out MongoSession – A PHP MongoDB Session Handler, which references Race Conditions with Ajax and PHP Sessions as well and seems to address the documented issues in the meantime:

I have recently updated the library to support atomic operations on both session writes and garbage collection to help prevent these race conditions.

A similar approach should be possible with Amazon DynamoDB as well by means of an appropriate combination of Conditional Updates, Atomic Counters and Consistent Reads, see Working with Items in Amazon DynamoDB for details on those concepts and/or check out the following FAQ entries:

  • Does Amazon DynamoDB support conditional operations? - Yes, you can specify a condition that must be satisfied for a PUT, update, or delete operation on an item to be completed. [...] Conditional operations allow users to implement optimistic concurrency control systems on DynamoDB.
  • Does Amazon DynamoDB support increment or decrement operations? - Yes, Amazon DynamoDB allows atomic increment and decrement operations on scalar values.
like image 2
Steffen Opel Avatar answered Oct 08 '22 14:10

Steffen Opel


Your servers are on AWS? Why don't you try the ElastCache feature? http://aws.amazon.com/en/elasticache/

I use CakePHP, witch supports DB and Memcache to store sessions. After some some time studding both, I've opted for Memcache. Actually, I set PHP to store sessions on Memcache and set CakePHP to use php sessions config. This way I can separate my memcache instances for sessions and caching.

Regards.

like image 1
tvdias Avatar answered Oct 08 '22 13:10

tvdias