neblaz
neblaz

Reputation: 733

vert.x - How many Timer possible

With vert.x, how many Timers (one-shot mainly) can I create without thinking about reduced performance, memory issues, etc.? Are there some limits?

The scenario of creating those Timers is not artificial, I don't want to loop and create at a time a few thousand Timers, but rather in a real world scenario over the time Timers might be created continuously, a few at a time or a few 10th in an hour, or 100 or so over a day, but it might happen, that there are a few thousand created overall and waiting to run.

And what to expect if for example a few hundred of them are executed at the same time or are running at almost the same time? How does vert.x handle such load?

Cheers

Upvotes: 4

Views: 1624

Answers (1)

Alexey Soshin
Alexey Soshin

Reputation: 17701

Short answer: nothing much will happen, even if you have million of periodic timers set.

You can try it out for yourself:

    Vertx vertx = Vertx.vertx();
    AtomicLong counter = new AtomicLong(0);

    for (int i = 0; i < 1_000_000; i++) {
        vertx.setPeriodic(1, (l) -> {
            if (counter.incrementAndGet() % 1_000_000 == 0) {
                System.out.print(".");
            }
        });
    }
    System.out.println("Done deploying");

Here you can see that I creat 1M timers, each of them incrementing a counter every millisecond. This will peak your CPU, and consume a lot of memory, but it will work.

And we're talking about actively running timers. The only resource your sleeping timers will consume is memory (since they are couple of objects sitting is a data structure). As long as you don't run of memory (and you'll need hundreds of thousands of them for that to happen), you should be fine.

And if you're really curious how timers in Vert.x work, you can start looking from here: https://github.com/eclipse/vert.x/blob/master/src/main/java/io/vertx/core/impl/VertxImpl.java#L372

Upvotes: 3

Related Questions