0

This question might be naive, so please help me out if I am missing out on some basic stuff.

So if there's an API and multiple users hit the same API simultaneously, are same variables updated? Intuition says this should not be the case, but then what is? I am unable to figure it out by just googling.

Say for example I have an API written in Java which has mutable objects. Now if multiple users are hitting that same API, will the existence of these mutable objects affect the response during simultaneous hits?

Dimi Ansari
  • 310
  • 3
  • 16
  • this question is very vague. Which API? – Tiberiu Dec 11 '19 at 17:18
  • I mean to say any API in general. Say for example I have an API and multiple users are trying to access it. API is restful, to be more precise. – Dimi Ansari Dec 11 '19 at 17:21
  • It depends. In a stateless API no, in a stateful API, probably. REST APIs tend to be stateless because managing state is a huge headache. – JonK Dec 11 '19 at 17:22
  • The API is stateless, but that's what is confusing me. How is it handled internally? Is it something like multiple copies of the same API are run based on the number of users trying to hit it? – Dimi Ansari Dec 11 '19 at 17:24
  • Someone could correct me here, but "simultaneous" requests aren't possible. If for example 2 requests hit at exactly the same time, the back-end will handle them sequentially. Let's say I update resource `number` to `7`, and then you update it to `8` a fraction of a second later, I will get the `OK` that the new resource is now `7`, even though technically it's now `8`, due to that second request. You can think of an API as just a regular piece of code (ie. function, method), that is run whenever a request hits. – Tiberiu Dec 11 '19 at 17:32
  • I agree that it is not very common, but _what if_ two users hit the API at the _exact same time_. My question is less about hitting the API simultaneously, but more about how are mutable objects handled when multiple users access the API. – Dimi Ansari Dec 11 '19 at 17:45
  • @Tiberiu Threading is a thing. I'd imagine that there are vanishingly few single-threaded REST APIs out there. It's entirely possible for two requests to hit a REST API to modify the same resource at the same time. What happens after that depends on how the API has been built to handle concurrent modifications. Poorly-built ones will allow both modifications through, and whichever happens to complete first will be overwritten by the second. Well-written APIs will have some form of version control built in that prevents that scenario. – JonK Dec 11 '19 at 17:48
  • You can use AtomicInt, AtomicReference, etc. in a ServletContext to ensure atomicity with multiple threads in this scenario. – Charles Inwald Dec 11 '19 at 17:54
  • right then it becomes a question of how the code is written. If you have a `HashMap` for example (not `ConcurrentHashMap`) and 2 threads are trying to modify it, then you may have issues. You need to handle `API` requests as you would in a multithreaded environment – Tiberiu Dec 11 '19 at 18:05

1 Answers1

0

Each API request will likely spawn a new servlet, thus variables will be thread local (not shared). If you would like to share variables safely across these servlets, you might use a ServletContext. See this post

  • So object variables that are mutable will remain unaffected across threads as well? – Dimi Ansari Dec 11 '19 at 17:43
  • Ahh, that's more of what I am looking for. Could you please help me with some resources/or keywords with which I can read up about it to make the concept more clear? – Dimi Ansari Dec 11 '19 at 17:52
  • Look into ServletContexts, Here is a simple example: https://stackoverflow.com/a/24469301/7332807. You may also need to ensure atomicity with Java Atomics. – Charles Inwald Dec 11 '19 at 17:57
  • I'm not sure how much I agree with this. You have **one** instance of a program running. If you have a data structure in that program, and 2 threads are spawned due to 2 requests trying to modify that same data structure, then you have to make sure you handle the concurrency yourself. It will not spawn 2 instances of that data structure. Someone please correct me if I'm mistaken – Tiberiu Dec 11 '19 at 18:12
  • 1
    It depends if it is an instance variable or a local variable, see https://stackoverflow.com/a/3106909/7332807 I should've made this distinction in my earlier comment. – Charles Inwald Dec 11 '19 at 18:20