Transcript
Page 1: Using Amazon SimpleDB with Ruby on Rails

Using Amazon SimpleDB with Rails

Martin Rehfeld@ RUG-B 07-Feb-2008

Page 2: Using Amazon SimpleDB with Ruby on Rails

Remember?AWS Rails Scenario

Static Content

SQL Database

Web Server

Application Server

Code

memcached,backgrounDRb,

...

EC2 S3

}HOW TO GET A PERSISTENT DB?

Page 3: Using Amazon SimpleDB with Ruby on Rails

Amazon SimpleDB• Database web service advertised as

• Simple, Flexible, Scalable, Fast,Reliable, Inexpensive

• No RDBMS: no SQL, no joins, no schema,no referential integrity, no transactions

• HTTP-Interface

• Pay per use:

• $0.14 per Machine Hour consumed

• $0.10-0.18 per GB - data transfer

• $1.50 per GB-month storage

limitedbeta

Page 4: Using Amazon SimpleDB with Ruby on Rails

Amazon SimpleDBCommon Terms

• Domain:storage container ~ table

• Item:~ table rows accessed byID ~ primary key

• Attribute:~ table columns; every item may have a different set of up to 256 attributes

• Value:each Attribute may have multiple Values, always varchar(1024)[]

SimpleDB

...my domain B

my domain A

}}

(multiple) values

item

s

attributes

Page 5: Using Amazon SimpleDB with Ruby on Rails

SimpleDB @

(amazon_sdb)rich API*

*) defect release 0.6.5 when evaluated, but newer release available (not re-tested yet)

no codeno codeno codeno codeno codeno code

no code

simply working!

Page 6: Using Amazon SimpleDB with Ruby on Rails

Amazon SimpleDB API

• Domain level

• CREATE, LIST, DELETE

• Item level

• GET, PUT, DELETE attributes with values

• QUERY* for unordered item IDs by attribute values within one domain*) lexicographical =, !=, <, > <=, >=, STARTS-WITH, AND, OR, NOT, INTERSECTION / UNION

• Beware: Eventual Consistency Approach!

Page 7: Using Amazon SimpleDB with Ruby on Rails

Almost RESTful

Page 8: Using Amazon SimpleDB with Ruby on Rails

Mapping RESTful URLs

HTTP/REST

GET /domain?query

GET /domain/itemID

POST /domain/itemID

PUT /domain/itemID

DELETE /domain/itemID

SimpleDB

QUERY

GET ATTRIBUTES

PUT ATTRIBUTES

PUT ATTRIBUTES(replace)

DELETE ATTRIBUTES

Page 9: Using Amazon SimpleDB with Ruby on Rails

Mapping RESTful URLs

HTTP/REST

GET /domain/resource?query

GET /domain/resource/itemID

POST /domain/resource/itemID

PUT /domain/resource/itemID

DELETE /domain/resource/itemID

SimpleDB

QUERY

GET ATTRIBUTES

PUT ATTRIBUTES

PUT ATTRIBUTES(replace)

DELETE ATTRIBUTES

Emulating multiple resources in one domain via _resource attribute

Page 10: Using Amazon SimpleDB with Ruby on Rails

A Bridge to Rails

SimpleDB

ActiveResource

HTTP/REST

HTTP/SDB API

„Rails XML“

„AWS XML“?AWS

SDB ProxyServer*

*) http://inside.glnetworks.de/2008/01/20/bridging-rails-to-amazon-simpledb-using-activeresource/

Page 11: Using Amazon SimpleDB with Ruby on Rails

AWS SDB Proxy Plugin I• Install aws-sdb gem

gem install aws-sdb

• Install aws_sdb_proxy plugin:script/plugin install http://rug-b.rubyforge.org/svn/aws_sdb_proxy

• Configure config/aws_sdb_proxy.ymldevelopment: aws_access_key_id: <your amazon aws key> aws_secret_access_key: <your amazon aws secret key> port: 8888

• Create new domain on Amazon SimpleDBrake aws_sdb:create_domain DOMAIN=ActiveResourceStore

• Start AWS SDB Proxyrake aws_sdb:start_proxy_in_foreground

Page 12: Using Amazon SimpleDB with Ruby on Rails

AWS SDB Proxy Plugin II• Create demo ActiveResource model

class Post < ActiveResource::Base self.site = "http://localhost:8888" self.prefix = "/ActiveResourceStore/"end

• Testdrive in script/console>> p = Post.create(:title => 'My first SimpleDB post')=> #<Post:0x198ceec @prefix_options={}, @attributes={...}>

>> p.body = 'Content is king'=> "Content is king"

>> p.save=> true

>> Post.find(:first, :params => { :title => 'My first SimpleDB post' })=> #<Post:0x18efef8 @prefix_options={}, @attributes={...}>

Page 13: Using Amazon SimpleDB with Ruby on Rails

Vision: Fully Virtualized Scalable Stack...

• Building blocks for AWS-only web app deployment:

• Static assets/content in S3

• As many app servers as needed on EC2

• Structured data persisted in SimpleDB

• All client/server interactions via SQSNote: XmlHttpRequest cross-site restrictions must be examined