Load Balancers & Caches
What is Load Balancer?
Load Balancer evenly distributes incoming traffic/load among webservers/workers that are defined in a load-balanced set. Users connect to the public IP of the load balancer, and web-servers are reachable over private IP from load balancer. Ex: Ngnix, Amazon ELB
If Web-Server1 goes offline, all the traffic will be routed to Web-Server2. This prevents the website from going offline. If the website traffic grows rapidly, and two servers are not enough to handle the traffic, new web servers can be added to pool
Types of LB
Places where LB can be kept
Scheduling Algorithms for LB
What is Cache?
Cache is temporary storage that stores the result of frequently accessed data in memory so that subsequent requests are served more quickly What data should be cached? Mostly used, less frequently changing data is stored in cache. (Eg: Metadata, configuration data) What should not be cached? Data that is frequently/actively changing. Cache lies near to requester so that requester need not to go hard-disk to retrieve data. This reduces latency.
Types of Caches
Where Caches can be placed?
1. Web Browser Cache
Browser stores local copies of web resources for faster retrieval the next time on Hard-disk. Ex: Once browser downloads CSS, JS files, images (for a session) there is no need again to download from same server and can be cached
CACHE-CONTROL HEADER: HTTP server instructs browser(http client) to cache a particular content for TTL duration in cache control header. Format: Response Header {cache-control: public, max-age: 45223}
2. Between Application Server and Database
Cache Between AppServer & DB
Cache Eviction Policies
No eviction returning an error the memory limit is reached All keys LRU removing keys by the least recently used first Volatile LRU removing keys, that have an expiration time set, by the least recently used first. All keys random removing keys randomly Volatile random removing keys, that have an expiration time set, randomly Volatile TTL removing keys, that have an expiration time set, by the shortest time to live first.
Redis vs Memcached