Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many timeouts can a Netty HashedWheelTimer handle?

Tags:

netty

The doc says "The default number of ticks per wheel (i.e. the size of the wheel) is 512. You could specify a larger value if you are going to schedule a lot of timeouts."

Does it mean by default it can only handle 512 timeouts? If I want 100 thousand timeouts of 25 seconds (for SockJS), what value should I set to number of ticks per wheel?

like image 998
Ngoc Dao Avatar asked Jan 14 '23 13:01

Ngoc Dao


1 Answers

The wheel is basically a hashtable with separate chaining whose hash function is 'time to notification'. The separate chaining is implemented as an unbounded ordered set, so a wheel can practically hold unlimited number of timeouts.

If you schedule a timeout that times out in a distant future (i.e. large delay), the large delay will be divided by wheelSize * tickDuration, and use its remainder as the hash of the timeout. Therefore, the current slot in a wheel can hold both the timeouts that will expire within the next tickDuration and the timeouts that will expire in (tickDuration * wheelSize * n) ms, where the variable n will decrease as the timer thread iterates over the wheel. The latter will cost some CPU time when the timer thread visits the slot because it's not really their turn to get expired. (This is similar to collisions in traditional hashtables). To reduce the chance of the collisions, you can increase the size of the wheel.

For example, if you are sure most timeouts scheduled are going to expire within a minute, you can make wheelSize * tickDuration a minute (e.g. 600 slots * 100 ms).

For the detailed information about hashed wheels, please read this.

like image 187
trustin Avatar answered Jan 21 '23 19:01

trustin