6

Let's say I have a REST api endpoint that is getting a resource called a "purchasable" (an item that a user can purchase). Here is an example of what this might look like.

Endpoint: GET /purchasables/253

Responds with:

{
    "id": "253",
    "name": "T shirt",
    "price": 1234,
    "discounts": [
        ....
    ]
}

So this purchasable object has an id, a name, and a price that never changes. But let's say the discounts array is different based on the user who is making the request (so it could have different things for different users). Is that some sort of anti-pattern that has a name?

I see one potential downside is that caching this object is now more difficult, as we can't just store a copy of that item based on its id property now. This pattern just sort of smells to me and I can't find anyone talking about this specifically.

4
  • 2
    If a business decides that a certain thing (e.g. server-side rendering of "discounts") is a fundamental business requirement, it is not up to a software engineer to raise objections by calling it a "smell" or an "anti-pattern". Instacart has made a similar decision to server-side render a lot of decisions because of the need to (1) provide a reliable product availability estimate, and (2) complex pricing computation cannot be entrusted to client-side due to consumer laws (if displayed price is wrong, website may be forced to honor that price). (Reference links follows.) Commented Dec 23, 2020 at 15:44
  • 1
  • 1
    To conclude, there are performance and bandwidth (and cloud computing costs) consequences that result from such business requirements. But requirements are requirements. Commented Dec 23, 2020 at 15:55
  • 1
    If you just need a name (a neutral one, without specifically being a smell or anti-pattern), I'd say "uncachability". Commented Dec 23, 2020 at 15:56

2 Answers 2

3

Since the discounts vary over time with the same inputs, the output is not idempotent. And since the output varies per user, it is input-dependent. The result is an endpoint which is not cache-friendly. As others have mentioned this is not necessarily an anti-pattern, but one obvious possibility for change if caching is going to be very important would be to separate this output into a /purchasables/$id/discounts endpoint, which would hopefully also mean that the /purchasables/$id endpoint no longer needs the user input.

5

I am not sure if it has a name, but it is a fairly common pattern to let the resource representation depend on input parameters. The most common parameter in that context is the currently authenticated user.

To give some examples

  • If I access https://softwareengineering.stackexchange.com/users/5099/, then I get my full (editable) profile. But when another user visits that link, they only get the public profile in read-only mode.
  • Similarly, some sites use the endpoint /users/me to let you access your own profile. Obviously, that differs for different users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.