views:

16

answers:

1

Hi, I am building REST API in php with memcache layer on top for caching all resources. After some reading/experience it turns out it's best when documents are as simple as posible...mainly due to dropping cache sequences.

So if there is 'building','room' entities for the 'room' document I would only place the id of the 'building' and not the whole data of it. Then on api client side I would merge data as needed.

Problem becomes when I need to update/insert (most cases more than one table). I update one resource but on second update system fails or whatever and there becomes database inconsistancies.

I see several solutions:

  1. Implement rest transactions which I find wrong and complex as idea is to be stateless and easy.

  2. On update/insert actions I pass more complex data (not single entities) so I can force transactions on API level. But this will make it weird that your GET document structure is same as PUT document structure. And again somehow make drop sequences complex.

Any pointers are more than welcome. Cheers,

A: 

What you want to do is something like:

Client <--> Proxy cache (like Squid) <--> REST interface <--> memcached <--> Domain model

If you take full advantage of HTTP caching, you may not need the memcached layer at all. Cache invalidation is a nasty problem to solve and HTTP has lots of rules and mechanisms already in place to keep you sane. I'm not very familiar with memcached but a quick google on the subject seems to indicate that you need to roll your own cache invalidation strategy. HTTP has the added benefit of being able to cache stuff on the client side too.

If you have scenarios where the client needs to update multiple domain objects at once then you need to create resources in the REST interface that represent virtual "composite" objects. The client should only perceive that they are updating a single resource. Behind the scenes you can update multiple resources in a database transaction if you like.

REST systems are easier to create if you map your resources to your PresentationModels. The mistake most people make is trying to strictly map REST resources to domain objects. There may be a close correlation but it will not be a direct mapping unless you have a really boring app!

Darrel Miller