# Fastest hashing algorithm?

Discussion in 'Java' started by howa, Jun 12, 2007.

1. ### howaGuest

conside an example like...

you have 5 Servers, one user queue, and you want to distribute the
users to these 5 servers, users come at different time, assume truely
random

a simple algorithm is to use Random to generate index 0..4 and assign
the server.

but think about it, if user come at random, why you need to use radnom

i don't know the implementation of Random, but seems a little bit
overkill the requirements...

a simple approach is to `mod` the current time and assign the server,
what do you abt this suitation?

thanks.

howa, Jun 12, 2007

2. ### Matt HumphreyGuest

"howa" <> wrote in message
news:...
| conside an example like...
|
| you have 5 Servers, one user queue, and you want to distribute the
| users to these 5 servers, users come at different time, assume truely
| random
|
| a simple algorithm is to use Random to generate index 0..4 and assign
| the server.
|
| but think about it, if user come at random, why you need to use radnom
|
| i don't know the implementation of Random, but seems a little bit
| overkill the requirements...
|
| a simple approach is to `mod` the current time and assign the server,
| what do you abt this suitation?

I would assign each incoming user to the least busy server.

Matt Humphrey, Jun 12, 2007

3. ### Tom HawtinGuest

howa wrote:
>
> you have 5 Servers, one user queue, and you want to distribute the
> users to these 5 servers, users come at different time, assume truely
> random
>
> a simple algorithm is to use Random to generate index 0..4 and assign
> the server.
>
> but think about it, if user come at random, why you need to use radnom
>
> i don't know the implementation of Random, but seems a little bit
> overkill the requirements...

Handling a user request is a relatively *HUGE* operation. Think how many
cycles that will cost you, and guess how much creating a psuedo-random
number costs.

You can easily look at the implementation of Random. It's in the src.zip
use a lock. Indeed, you could allocate round-robin. Just use a
java.util.concurrent.atomic.AtomicInteger, and no need for locks.

Tom Hawtin

Tom Hawtin, Jun 12, 2007
4. ### Mark SpaceGuest

Matt Humphrey wrote:

> I would assign each incoming user to the least busy server.

I'm with Matt. I'm sure there's a simple way to get the load from a

A simpler interim solution might just be to keep track of how many user
connections you have currently to each server. (This assumes users
leave their connection "open" I guess.) Assign new users to the server
with the least number of users connected.

Mark Space, Jun 13, 2007
5. ### Roedy GreenGuest

On Tue, 12 Jun 2007 11:40:02 -0700, howa <> wrote,
quoted or indirectly quoted someone who said :

>
>you have 5 Servers, one user queue, and you want to distribute the
>users to these 5 servers, users come at different time, assume truely
>random

Before you waste effort optimising, you must measure the time taken to
make sure you are chewing at the bottleneck.

Almost certainly optimising this will have no noticeable effect unless
you were doing task scheduling inside the OS.
--
The Java Glossary
http://mindprod.com

Roedy Green, Jun 13, 2007
6. ### howaGuest

> I would assign each incoming user to the least busy server.

I agree, but sometime checking the server status introduce some

e.g. the JSP page need to store all the servers status into a
persistent storage and check for every requests.

So that's why many people still using DNS round-robin kind of for load
balacing..

howa, Jun 13, 2007
7. ### EricFGuest

In article <>, Roedy Green <> wrote:
>On Tue, 12 Jun 2007 11:40:02 -0700, howa <> wrote,
>quoted or indirectly quoted someone who said :
>
>>
>>you have 5 Servers, one user queue, and you want to distribute the
>>users to these 5 servers, users come at different time, assume truely
>>random

>
>Before you waste effort optimising, you must measure the time taken to
>make sure you are chewing at the bottleneck.
>
>Almost certainly optimising this will have no noticeable effect unless
>you were doing task scheduling inside the OS.

I missed the original post but I do agree with Roedy. Are all the user tasks
similar in resource usage and run time?

Eric

EricF, Jun 14, 2007
8. ### TwistedGuest

On Jun 14, 12:24 am, (EricF) wrote:
> I missed the original post but I do agree with Roedy. Are all the user tasks
> similar in resource usage and run time?

And are all the servers comparable in capacity (similar RAM, similar
CPU, similar bandwidth...)?

Twisted, Jun 14, 2007
9. ### Roedy GreenGuest

On Tue, 12 Jun 2007 11:40:02 -0700, howa <> wrote,
quoted or indirectly quoted someone who said :

>you have 5 Servers, one user queue, and you want to distribute the
>users to these 5 servers, users come at different time, assume truely
>random

why would random do better than round robin?

If you want to get clever, I think you need a measure of who is most
lightly loaded and give the new user to that server.

On Vista there is a measure of disk and CPU time utilisation that even
desk gadgets can get hold of. You would need a little JNI to gather