i'm creating java server quite big simulation , have couple of high level design questions.
some background:
- the server run simulation.
- clients connect server via tcp connections mobile devices , interact data structures in simulation. try use simple polling scheme in clients. find hard maintain long-lived tcp connections between mobile devices , server , i'm not yet sure whether clients try keep open tcp connection or whether set , tear down each transmission.
- when client active on mobile device, have client poll server @ least few times minute.
- the simulation keep running regardless of whether clients connected or not.
- the total number of existing clients large, many thousands.
- clients poll server simulation state, issues control commands simulation.
- all messages small in size.
- i expect server run under linux on multi-core cpu server hardware.
currently have following idea threading model in server:
- the simulation logic executed few threads. simulation logic threads both read , write from/to simulation data structures.
- for each client there java thread performing blocking read call socket client. when poll command received client, corresponding client thread reads info simulation data structures (one client poll typically interested in small subset of total data structures) , sends reply client on client's socket. thus, access data structures need synchronized between client threads , simulation threads (i try have locks on smaller subsets of data). if control command received client, client thread write data structures.
for small number of clients, think work fine.
question 1: threading model hold large number (thousands) of connected clients? i'm not familiar memory/cpu overhead there in such java implementation.
question 2: avoid having server asynchronously send messages clients in scenarios may need have server send "update now" messages asynchronously or many clients , i'm not quite sure how that. having simulation logic thread(s) send messages doesn't seem right... maybe "client notification thread pool" concept?
you ask 2 questions; i'll answer first.
i've written application involved thousands of threads in 1 application. did once run problem maximum number of threads on linux server; us, think limit 1000 threads. affected our java application because java threads use native threads. set limit higher, , application scaled 2000 threads, needed without issue; don't know have happened had needed scale higher.
the fact default maximum number of threads 1000 suggests might not wise run many thousands of threads on single linux server. believe primary issue sufficient memory stack needs allocated each thread.
our intended long term fix change architecture threads thread pool each serviced multiple sockets. isn't of issue; each socket, thread has process pending messages before going on next socket. have careful synchronizing memory access, application needs since simulation interacts multiple threads part not huge change.
Comments
Post a Comment