Design a Pizza Order System:
Each Pizza have different kinds of topping
Pizza Abstract Class{ cost() ; getName() }
Different kinds of pizza extend from the Pizza Class( different material ) : define each of the cost (WheatPizza, CheesePizza)
Then we add toppings
ToppingsDecorator Extend the Pizza
FruitTopping extend TopingDecorator
Although caching can speed up an application and improve scalability, it is important to reason on appropriate algorithms for replacement, coherence and invalidation of cached entries.
The application server backs the HTTP server and is a central component of the architecture, as it is the only component that incorporates most of the other services for providing its logic.
That leads to the first golden rule for scalability: every server contains exactly the same codebase and does not store any user-related data, like sessions or profile pictures, on local disc or memory. Sessions need to be stored in a centralized data store which is accessible to all your application servers.
Cache: In-Memory such as redis and memchached:
(1) cache the once run query 可能需要大规模更改如果table本身变了 不好去除
(2) cache the object: store the result into the class (Prefered Way)
Cache Aside(lazy loading): cache do not talk to storage(database) client manage the update of cache (MemCache)
(-)
Each cache miss results in three trips, which can cause a noticeable delay.
Data can become stale if it is updated in the database. This issue is mitigated by setting a time-to-live (TTL) which forces an update of the cache entry, or by using write-through.
When a node fails, it is replaced by a new, empty node, increasing latency.
Cache Through: The application uses the cache as the main data store, reading and writing data to it, while the cache is responsible for reading and writing to the database:
(-)
Most data written may never read
Asynchronism
(1) 提前准备好 turn dynamic page to static ones..
(2) The basic idea is to have a queue of tasks or jobs that a worker can process and the workers will notify that the job is finished
once it is done? (http://www.rabbitmq.com/getstarted.html\
If queues start to grow significantly, the queue size can become larger than memory, resulting in cache misses, disk reads, and even slower performance. Back pressure can help by limiting the queue size, thereby maintaining a high throughput rate and good response times for jobs already in the queue. Once the queue fills up, clients get a server busy or HTTP 503 status code to try again later. Clients can retry the request at a later time, perhaps with exponential backoff.
(-) May add delays for inexpensive calculations
Trade Off
Latency is the time to perform some action or to produce some result.
Throughput is the number of such actions or results per unit of time.
Consistency - Every read receives the most recent write or an error
VS
Availability - Every request receives a response, without guarantee that it contains the most recent version
http://robertgreiner.com/2014/08/cap-theorem-revisited/
Consistency - Every read receives the most recent write or report an error
VS
Availability - Every request receives a response, without guarantee that it contains the most recent version (更自由的要求 )
Load Balancers
Use load balancers to balance the request, hands to the best server. Use multiple load balancers could prevent single-point-failure but could added complexity!If not configured correctly it could be a bottleneck..
Sharding distributes data across different databases such that each database can only manage a subset of the data. Taking a users database as an example, as the number of users increases, more shards are added to the cluster.
(-) ou'll need to update your application logic to work with shards, which could result in complex SQL queries.
Denormalization: Improve Read at the expense of write.. Denormalization attempts to improve read performance at the expense of some write performance. Redundant copies of the data are written in multiple tables to avoid expensive joins.
SQL Tuning : some things to notice..