Performance
Stormpot is fast. Very fast. However, there are a number of configurations and decisions, where you have to make the right choice, if you want to get the most out of it.
That said, you mustn’t forget the number one rule of performance optimisation: measurement and analysis is king.
Considerations for Custom Expiration Implementations
The Expiration
is one of the most performance critical components of the pool.
The hasExpired
method is called at least once for every call to the claim
method, and may be called several times if objects turn out to have expired.
It is therefore important for the performance of the pool, that the expiration check is as fast as possible.
This can be a challenge if, for instance, you are pooling database connections, and you are checking their validity by running a query. The only way to make an operation faster, is by having it do less work. One way to take this to heart, is by not doing the expensive part of the check – by not sending the validation query – on every check, but only when it’s been a while since the object was last checked. Essentially, we want to amortise the cost of the expensive check.
One way we can accomplish this, is to use the every
combinator method on the Expiration
interface.
Here is an example, where network connections are checked that they are still connected, but the check is only performed at most once every 5 seconds:
Expiration<PooledConnection> checkConnection = new CheckConnectionExpiration()
.every(10, SECONDS); // (1)
Pool<PooledConnection> connectionPool = Pool.from(connectionAllocator)
.setExpiration(checkConnection)
.build();
-
Using the
every
combinator on the expensive expiration, will return a new expiration that only delegates to the expensive one every 5 seconds.
It is also possible to forgo the expiration check entirely, by using the Expiration.never()
expiration.
This way, no check will be done at claim time.
You can instead optimistically try to use the objects, and in your own code detect if they are expired or not.
If they are, you can explicitly expire them by making a call to the relevant Slot.expire()
method.