Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST service with load balancing

I've been considering the advantages of REST services, the whole statelessness and session affinity "stuff". What strikes me is that if you have multiple deployed versions of your service on a number of machines in your infrastructure, and they all act on a given resource, where is the state of that resource stored?

Would it make sense to have a single host in the infrastructre that utilises a distributed cache, and any state that is change inside a service, it simply fetches/puts to the cache? This would allow any number of deployed services for loading balancing reasons to all see the same state views of resources.

like image 948
Martin Blore Avatar asked Sep 03 '09 20:09

Martin Blore


People also ask

What is load balancing in REST API?

Load Balancer REST API Load balancer allows fine-grained configuration of how incoming traffic is distributed across VM instances hosted in Microsoft Azure. A load balancer has two main parts: a frontend and a backend configuration. The frontend configuration describes the exposed IP address of the load balancer.

Which service is used for load balancing?

LBaaS can be used in cloud computing in OpenStack private cloud implementations. While Load Balancer as a Service refers to distributing client requests across multiple application servers in OpenStack environments, cloud load balancers follow a similar model as LBaaS.

Does API gateway have load balancing?

API Gateway can manage and balance out network traffic just as a Load Balancer, just in a different way. Instead of distributing requests evenly to a set of backend resources (e.g. a cluster of servers), an API Gateway can be configured to direct requests to specific resources based on the endpoints being requested.

Which load balancer is best for microservices?

#1) Nginx. Best for: Load balancing, content caching, web server, API gateways, and microservices management for modern cloud web and mobile applications.


2 Answers

If you're designing a system for high load (which usually implies high reliability), having a single point of failure is never a good idea. If the service providing the consistent view goes down, at best your performance decreases drastically as the database is queried for everything and at worst, your whole application stops working.

In your question, you seem to be worried about consistency. If there's something to be learned about eBay's architecture, it's that there is a trade-off to be made between availability/redundancy/performance vs consistency. You may find 100% consistency is not required and you can get away with a little "chaos".

A distributed cache (like memcache) can be used as a backing for a distributed hashtable which have been used extensively to create scalable infrastructures. If implemented correctly, caches can be redundant and caches can join and leave the ring dynamically.

REST is also inherently cacheable as the HTTP layer can be cached with the appropriate use of headers (ETags) and software (e.g. Squid proxy as a Reverse proxy). The one drawback of specifying caching through headers is that it relies on the client interpreting and respecting them.

However, to paraphrase Phil Karlton, caching is hard. You really have to be selective about the data that you cache, when you cache it and how you invalidate that cache. Invalidating can be done in the following ways:

  1. Through a timer based means (cache for 2 mins, then reload)
  2. When an update comes in, invalidating all caches containing the relevant data.

I'm partial to the timer based approach as its simpler to implement and you can say with relative certainty how long stale data will live in the system (e.g. Company details will be updated in 2 hours, Stock prices will be updated in 10 seconds).

Finally, high load also depends on your use case and depending on the amount of transactions none of this may apply. A methodology (if you will) may be the following:

  1. Make sure the system is functional without caching (Does it work)
  2. Does it meet performance criteria (e.g. requests/sec, uptime goals)
  3. Optimize the bottlenecks
  4. Implement caching where required

After all, you may not have a performance problem in the first place and you may able to get away with a single database and a good back up strategy.

like image 123
Richard Nienaber Avatar answered Nov 16 '22 02:11

Richard Nienaber


I think the more traditional view of load balancing web applications is that you would have your REST service on multiple application servers and they would retrieve resource data from single database server.

However, with the use of hypermedia, REST services can easily vertically partition the application so that some resources come from one service and some from another service on a different server. This would allow you to scale to some extent, depending on your domain, without have a single data store. Obviously with REST you would not be able to do transactional updates across these services, but there are definitely scenarios where this partitioning is valuable.

If you are looking at architectures that need to really scale then I would suggest looking at Greg Young's stuff on CQS Architecture (video) before attempting to tackle the problems of a distributed cache.

like image 31
Darrel Miller Avatar answered Nov 16 '22 02:11

Darrel Miller