VJS
VJS

Reputation: 2941

Rest : How to handle thousands of same request

I have rest web service which basically takes long as a number and provide whether the number is prime or not ie plain/text true or false.

millions of users are using this service.

Now one user is sending very big number like (15-20 digit number / big number ) and my service is taking time to calculate whether the number is prime or not. That user is sending thousands of requests continuously for the same number. So how to handle these same thousands of requests.

For Example :

Sending 101221323232324343 number in the request and my service is taking let's say 3 sec to find out whether the number is prime or not. Now he is sending 1000 requests per second.

how to handle this case ?

Upvotes: 2

Views: 773

Answers (3)

Prashant Pandey
Prashant Pandey

Reputation: 4642

@NotThreadSafe 
public CheckIfPrime extends Servelet  {
    private Long lastNumber;
    private Long isLastNumberPrime;

    public Long operation(ServeletRequest req, ServeletResponse res) {
        if(getNumberFromRequest(req) == lastNumber) {
            return isLastNumberPrime;
        } else {
             return checkForPrimeNumber(getNumberFromRequest);
        }
    }

}

Use synchronisation primitives to honour the class' invariants and postconditions (one way can be to use AtomicLong instead of Long)

If you think the number of requests can be too huge (exhausting memory), consider using a distributed cache.

For very high TPS, make your application reactive. The checkForPrime()opeeation can be done asynchronously. Why block the calling thread?

Upvotes: -2

Oleg Sklyar
Oleg Sklyar

Reputation: 10072

You could make use of an evicting in-memory cache tuning its parameters with respect to maximum memory size or TTL. For general info check e.g. LFU cache eviction. There many implementations while I would recommend the caffeine library by Ben Manes for its efficiency. You could use a DB, but it IO may be more expensive than a new computation in this case, so I'd probably restrict caching to memory only.

Upvotes: 3

Lajos Arpad
Lajos Arpad

Reputation: 76414

You could store values which are larger than a given number of digits in a database. Whenever such a number is received, you can store whether it's prime in the database. You could also use a cache to store the last n different values, so, when a number is requested, you check whether it's too long. If not, simply calculate. If it is too long, search for it in the cache. If it's there, return it. If it's not there, search for it in the database. If it's there, add it to the cache (and possibly remove the oldest value) and return it to the user. If it's not even in the database, calculate it, store it in the database, add it to the cache and return the answer to the user.

Upvotes: 2

Related Questions