Question

I am maintaining a system which can ADD and UPDATE products in a shopping cart. The only difference between ADD and UPDATE is that during UPDATE a product has an OrderId.

The front-end communicates with the back-end using web services.

Should I write a web service with a common method for ADD and UPDATE? Or should the web service have two different methods, one for ADD and one for UPDATE?

What are advantages / disadvantages for each pattern?

Was it helpful?

Solution

I do realize that you aren't asking about SQL, but it can be informative to look at comparable problem domains. Combinding INSERT and UPDATE operations is common enough to be a standard feature of SQL called MERGE (sometimes, popularly, UPSERT), so by that token, it’s certainly acceptable practice.

Still, it’s good to be aware of possible consequences. One type of problem that can arise from this is when you have concurrent writers.

If you have an explicit distinction between ADD and UPDATE, two concurrent writers both attempting to perform an ADD on the same key should cause a failure in the second writer. Depending on the scenario, such an explicit failure might be a good thing, because it explicitly informs you about a concurrency conflict.

On the other hand, if you invoke a MERGE method, the second write operation (which was originally intended by the writer to be an ADD) will automatically succeed as an UPDATE. Sometimes, this is what you want, while in other scenarios, this may be undesirable.

In general, concurrency conflicts should be addressed with a concurrency token (such as a timestamp).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top