0

I am trying to understand how concurrency works at a system level.

Backstory

I have an application and a datastore. The datastore can have several processes running and so can handle multiple requests concurrently. The datastore accepts communication over a single TCP port using a protocol in the format <msg length> <operation code> <operation data>

The existing application code blocks on datastore io. I could spin up several threads to achieve concurrency, but still each thread would block on io. I have some single thread non-blocking IO libraries but using them should require me to do some socket programming.

Question

How would a single-process connection pool to a single non-blocking port work? From what I understand the port maintains a sort of mapping so it can send the response to correct place when a response is ready. But I read that is uses the requestor's ip as the key. If multiple requests to the same port occur from the same process, wouldn't the messages get mixed up / intermingled?

Or, does each connection get assigned a unique key, so to make a connection pool I need only store a list of connection objects and they are guaranteed never to interact with each other?


Edit: don't know why i said TCP, and half the content of this question is unnecessary ... I am embarrassed. Probably ought to delete it, actually. I voted.

jisaacstone
  • 4,234
  • 2
  • 25
  • 39
  • 1
    http://stackoverflow.com/questions/3638953/do-tcp-connections-get-moved-to-another-port-after-they-are-opened – AK_ Jun 22 '14 at 21:35
  • I'm really unclear on the scenario. What datastore is this? Are you network-bound or are you waiting most of the time for the datastore to answer? You can only have requests to the datastore outstanding if the communication protocol supports it. Remember that TCP does not transmit messages, just a boundaryless stream of bytes. Concurrent reads or writes to the same stream are meaningless by principle. – usr Jun 22 '14 at 21:39
  • Also [here](http://en.wikipedia.org/wiki/Transmission_Control_Protocol#Resource_usage) , or in other the answer to your question below is yes. – AK_ Jun 22 '14 at 21:40
  • Or is this question purely about TCP? In that case please remove all references to the datastore and everything else that doesn't matter. What does the concept of a "process" have to do with the question? – usr Jun 22 '14 at 21:41

1 Answers1

1

The datastore accepts communication over a single TCP port

The result of the accept() is a new full-duplex socket which can be read and written to concurrently and independently of all other sockets in the process. The fact that its local port is shared is irrelevant. TCP ports aren't physical objects, only numbers.

Non-blocking mode and data stores have nothing to do with it.

user207421
  • 305,947
  • 44
  • 307
  • 483