查看: 1560| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[学习资料] Cache

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
1. what is caching? A copy of real data with faster access. A cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (owing to longer access time) or to compute, compared to the cost of reading the cache.
2. Where are caches used? at hardware level, operating systems, web stack, applications, your own short term vs long term memory, browser cache, DNS cache, CDN, Proxy servers. Application level: full output caching, opcode cache, query cache, storing denormalized results in the database, object cache, storing values in php objects/classes
3. Efficiency of caching: the earlier in the process, the closer to the original request, the faster. Browser cache will be faster than cache on a proxy. But probably also the harder to get it right, the closer to the requester, the more parameters the cache depends on.
4. What to cache on the server-side? Expensive operations, database access, reading files, API calls, Heavy computations, XML, user sessions, user data, homepage data
5. Where to cache on the server-side? Database(computed values, generated htmls), Static files(generated html or serialized php values)
6. Memcached: it's a server, client access over TCP or UDP, servers can run in pools: eg. 3 servers with 64 GB mem each give you a single pool of 192GB storage for caching, servers are independent, clients manage the pool
7. Memcached principles: Fast network access(memcached servers close to other application servers), No persistency(if your server goes down, data in memcached is gone), No redundancy/Fail-over, No replication(Single item in cache lives on one server only), No authentication, 1 key is maximum 1MB, keys are strings of 250 chars, No enumeration of keys, no active clean-up
8."There are only two hard things in Computer Science: cache invalidation and naming things." Phil Karlton
9 Invalidation: 9.1 caching for a certain amount of time, eg. 10 minutes, don't delete caches, thus you can't trust that data coming from cache is correct. This is great for summaries: overview or pages where it's not that big a problem if data is a little bit out of date(search results). 9.2 Store forever and expire on certain events(eg. store user data forever, when user changes any of his preferences, throw cache away )
10. Query caching: queries with JOIN and WHERE statements are harder to cache, so we can do join in PHP
11. Use of Multiget in memcached to reduce access cost. eg. find UID from array: SELECT ... FROM USERS WHERE uid IN (...), for each fetched user, store in cache and return merged cache/db results

评分

参与人数 2大米 +6 收起 理由
pikado + 5 欢迎分享你知道的情况,会给更多积分奖励!
rocketman2023 + 1 给你点个赞!

查看全部评分


上一篇:Github 上system-design-interview,获益匪浅,分享给大家
下一篇:系统设计算法集合
🔗
 楼主| crazybadboy 2020-3-9 07:42:40 | 只看该作者
全局:
Cache and Memchached
回复

使用道具 举报

🔗
 楼主| crazybadboy 2020-3-9 08:55:17 | 只看该作者
全局:
Redis
1. What is Redis: Memcache-ish in-memory key/value store, but its also persistent! And it also has very cool value types: lists, sets, sorted sets, hash tables, append-able buffers. Open source; very helpful and friendly community. Development is very active and responsive to requests. Sponsored by VMWare, Used in the real world: github, craigslist, engineyard, ... Used heavily in front-end database, search, geo resolving
2. Key Features and Cool Stuff, All data is in memory (almost), All data is eventually persistent (But can be immediately), Handles huge workloads easily, Mostly O(1) behavior, Ideal for write-heavy workloads, Support for atomic operations, Supports for transactions, Has pub/sub functionality, Tons of client libraries for all major languages, Single threaded, uses aync. IO, Internal scripting with LUA coming soon
3. Scaling: Master-slave replication out of the box; Slaves can be made masters on the fly; You can manually shard it client side; Single threaded - run num_cores/2 instances on the same machine
4. Persistence: All data is synchronized to disk - eventually or immediately; Pick your risk level Vs. performance; Data is either dumped in a forked process, or written as a append-only change-log (AOF); Append-only mode supports transactional disk writes so you can lose no data (cost: 99% speed loss :) ); AOF files get huge, but redis can minimize them on the fly; You can save the state explicitly, background or blocking; Default configuration:  Save after 900 sec (15 min) if at least 1 key changed, Save after 300 sec (5 min) if at least 10 keys changed, Save after 60 sec if at least 10000 keys changed
5. Virtual Memory: If your database is too big - redis can handle swapping on its own.Keys remain in memory and least used values are swapped to disk. Swapping IO happens in separate threads. But if you need this - don't use redis, or get a bigger machine😉
6. Use cases: Geo Resolving with geohashing; Implemented and opened by yours truly https://github.com/doat/geodis; Real time analytics; use ZSET, SORT, INCR of values; API Key and rate management; Very fast key lookup, rate control counters using INCR; Real time game data; ZSETs for high scores, HASHES for online users, etc; Database Shard Index; map key => database id. Count size with SETS; Comet - no polling ajax; use BLPOP or pub/sub; Queue Server; resque - a large portion of redis user base
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表