Question

I've built a server application in java, where clients can connect . I've implemented a heartbeat system where the client is sending every x seconds a small message. On the server side I save in a HashMap the time the client has sent the message , and I use a TimerTask for every client to check every x seconds if I received any message from the client.

Everything works ok for a small amount of client, but after the number of clients increase (2k+) the memory amount is very big, plus the Times has to deal with a lot of TimerTask and the program start to eat a lot of CPU.

Is there a better way to implement this? I thought about using a database and make a select the clients that didn't sent any update in a certain amount of time. Do you think this will work better, or is a better way of doing this.

Was it helpful?

Solution

Few random suggestions:

  • Instead of one timer per each client, have only one global timer that examines the map of received heartbeats quite often (say 10 times per second). Iterate over that map and find dead clients. Remember about thread-safety of shared data structure!

  • If you want to use database, use a lightweight in-memory DB like . But still sounds like an overkill.

  • Use cache or some other expiring map and be notified every time something is evicted. This way you basically put something in the map when a client sends a heartbeat and if nothing happened with that entry within given amount of time, the map implementation will remove it, calling some sort of listener.

  • Use actor-based system like Akka (has Java API). You can have one actor on the server side that handles one client. It's much more efficient than one thread/timer.

  • Use a different data structure, e.g. a queue. Every time you receive a heartbeat, you remove client from the queue and put it back at the end. Now periodically check only the head of the queue, which should always contain the client with oldest heartbeat.

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