views:

85

answers:

4

Scenario Imagine a REST service that returns a list of things (e.g. notifications)

Usage A client will continually poll the REST service. The REST service retrieves records from the database. If records are available, they are converted into JSON and returned to the client. And at the same time, the retrieved records are purged from the DB.

Problem How do you handle the problem if the REST endpoints encounters a problem writing the results back to the client ? By that time, the records have been deleted.

A: 

This could be a possible solution. The REST service can query the database for a list of notifications. It marks each item in the list ,by say setting a flag in the database. It then delivers all this notification records to the client.

If the server has successfully sent the results to the client, all marked records are deleted.

In case of failure, the marked records are unmarked,so that they get delivered during the subsequent polling interval.

I hope you got my point.

Prashanth
Adding a flag doesn't actually solve the problem since a failed response to the client (ie. the server sent a response but it never reached the client) would still result in lost notifications from the client perspective.
Paul Alexander
Yes, I see your point.The problem here is from the perspective of the client it is just a GET request on the server. But the server not only does GET, but also DELETE.This problem can be solved if the client sends an acknowledgement to the server, saying that it has successfully received the records. But that would mean two server trips, which I am not sure is a good idea for heavily trafficked services.
Prashanth
+4  A: 

Deleting the records will always be a dangerous proposition. What you could do instead is include a timestamp column on the data. Then have your REST url include a "new since" timestamp. You return all records from that timestamp on.

If the notifications grow to be too large you can always setup an automated task to purge records more than an hour old - or whatever interval works well for you.

Paul Alexander
+3  A: 

It sounds like a strange idea to delete DB records after reading access. Possible problems immediately leap into mind: Network trouble prevent the client reading the data, multiple clients cause each other to see incomplete lists, et.al.

The RESTful apporach might be like this:

  • Give each notification a specific URI. Allow GET and DELETE on these URIs. The client may trigger the record deletion once it successfully received and processed the notification.
  • Provide an URI to the collection of current notifications. Serve a list of notification data (ID, URI, timestamp, (optional:) content) upon GET request. Take a look at the Atom protocol for ideas. Optional: Allow POST to add a new notification.

With this approach all reading requests stay simple GETs. You may instrument the usual HTTP caching mechanisms on proxies and clients for performance improvement.

Anyway: Deleting a DB entry is a state change on the server. You must not do this upon a GET request. So POST will be you primary choice. But this does not help you much, since the communication might still not be reliable. And polling qith POSTs smells a lot more like Web-Services than REST.

mkoeller
A: 

We did this with special timestamp paramter.

Requests

  1. Request with timestamp.min, this return all items, and server timestamp;
  2. Request with timestamp from server, return items from hat time stamp, and delete prevoius, return server time stamp;

Please note that we did all this with post. means that virtually we sent command (not query get).

Mike Chaliy