业务背景描述:
时间:2016.6.-2016.9
发布产品类型:互联⽹动态站点 商城
⽤户数量: 10000-12000(⽤户量猛增)
PV : 100000-500000(24⼩时访问次数总和)
QPS: 50-100*(每秒访问次数)
DAU: 2000(每⽇活跃⽤户数)
随着业务增加,访问量越来越大,用户在访问某些页面的时候,通过慢查询日志发现慢查询SQL,
经过优化后,还是不够明显,而此类数据发生变动的频率又很小,故提出使用缓存中间件(一般会将数据存储到内存中)的方式,降低MySQL的读压力,提高整个业务架构集群的稳定性和快速响应能力。
增加缓存集群。
在本次架构中,使用缓存中间件解决一下两个问题:
1)session共享;
2)缓存热点数据,首页面的分类信息。
memcached介绍和安装启动:
1)介绍:
memory cache 把缓存放到内存中。
缓存:把访问到的东西放到内存。
浏览器缓存:从服务器取回的资源,没有过期,下次就不用取了。存储的这个资源,缓存文件,缓存资源。
服务器缓存:连接后端服务器,解析动态资源为静态资源,下次访问的资源就不需要解析了。缓存动态资源为静态资源。需要就设置为过期,如果不需要,就不需要设置为过期。
数据库缓存:SQL解析执行,查找数据 遍历。查询了一遍,就不需要再进行查找。
缓存:需要经过多个环节步骤获取到的结果,把这个结果储存起来,之后获取就不需要再经过一系列的步骤,直接返回储存的结果。这个结果就是缓存。缓存就是结果。
Memcached:高性能的分布式内存缓存服务器,一般的使用目的是,通过缓存数据库查询结果,减少数据库的访问次数,以提高动态Web应用的速度,提高可扩展性。
缓存:就具有储存功能,可以把这个看成是一个数据库。
关于缓存的过期,有效期的问题,由业务编程代码实现。
memcached的编译安装:
看下memcached的官网:
这个版本有点老,所以我们采用编译安装的方式进行。
1)上传软件版本到服务器:
软件包名:memcached-1.6.21.tar.gz
解压并编译安装:
#memcache依赖libevent 安装libevent-devel解决
shell > yum -y install libevent-devel
#编译安装memcached
shell > tar xvf memcached-1.5.8.tar.gz
shell > cd memcached-1.5.8
shell > ./configure --prefix=/usr/local/memcached
shell > make && make install
查看下memcache的目录:
bin是管理软件路径
include是调用的库
share是man手册
[root@server08 bin]# ./memcached -h
memcached 1.6.21
-p, --port= TCP port to listen on (default: 11211)
-U, --udp-port= UDP port to listen on (default: 0, off)
-s, --unix-socket= UNIX socket to listen on (disables network support)
-a, --unix-mask= access mask for UNIX socket, in octal (default: 700)
-A, --enable-shutdown enable ascii "shutdown" command
-l, --listen= interface to listen on (default: INADDR_ANY)
-d, --daemon run as a daemon
-r, --enable-coredumps maximize core file limit
-u, --user= assume identity of (only when run as root)
-m, --memory-limit= item memory in megabytes (default: 64)
-M, --disable-evictions return error on memory exhausted instead of evicting
-c, --conn-limit= max simultaneous connections (default: 1024)
-k, --lock-memory lock down all paged memory
-v, --verbose verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/responses)
-vvv extremely verbose (internal state transitions)
-h, --help print this help and exit
-i, --license print memcached and libevent license
-V, --version print version and exit
-P, --pidfile= save PID in , only used with -d option
-f, --slab-growth-factor= chunk size growth factor (default: 1.25)
-n, --slab-min-size= min space used for key+value+flags (default: 48)
-L, --enable-largepages try to use large memory pages (if available)
-D Use as the delimiter between key prefixes and IDs.
This is used for per-prefix stats reporting. The default is
":" (colon). If this option is specified, stats collection
is turned on automatically; if not, then it may be turned on
by sending the "stats detail on" command to the server.
-t, --threads= number of threads to use (default: 4)
-R, --max-reqs-per-event maximum number of requests per event, limits the
requests processed per connection to prevent
starvation (default: 20)
-C, --disable-cas disable use of CAS
-b, --listen-backlog= set the backlog queue limit (default: 1024)
-B, --protocol= protocol - one of ascii, binary, or auto (default: auto-negotiate)
-I, --max-item-size= adjusts max item size
(default: 1m, min: 1k, max: 1024m)
-F, --disable-flush-all disable flush_all command
-X, --disable-dumping disable stats cachedump and lru_crawler metadump
-W --disable-watch disable watch commands (live logging)
-Y, --auth-file= (EXPERIMENTAL) enable ASCII protocol authentication. format:
user:pass\nuser2:pass2\n
-e, --memory-file= (EXPERIMENTAL) mmap a file for item memory.
use only in ram disks or persistent memory mounts!
enables restartable cache (stop with SIGUSR1)
-o, --extended comma separated list of extended options
most options have a 'no_' prefix to disable
- maxconns_fast: immediately close new connections after limit (default: enabled)
- hashpower: an integer multiplier for how large the hash
table should be. normally grows at runtime. (default starts at: 0)
set based on "STAT hash_power_level"
- tail_repair_time: time in seconds for how long to wait before
forcefully killing LRU tail item.
disabled by default; very dangerous option.
- hash_algorithm: the hash table algorithm
default is murmur3 hash. options: jenkins, murmur3, xxh3
- no_lru_crawler: disable LRU Crawler background thread.
- lru_crawler_sleep: microseconds to sleep between items
default is 100.
- lru_crawler_tocrawl: max items to crawl per slab per run
default is 0 (unlimited)
- read_buf_mem_limit: limit in megabytes for connection read/response buffers.
do not adjust unless you have high (20k+) conn. limits.
0 means unlimited (default: 0)
- no_lru_maintainer: disable new LRU system + background thread.
- hot_lru_pct: pct of slab memory to reserve for hot lru.
(requires lru_maintainer, default pct: 20)
- warm_lru_pct: pct of slab memory to reserve for warm lru.
(requires lru_maintainer, default pct: 40)
- hot_max_factor: items idle > cold lru age * drop from hot lru. (default: 0.20)
- warm_max_factor: items idle > cold lru age * this drop from warm. (default: 2.00)
- temporary_ttl: TTL's below get separate LRU, can't be evicted.
(requires lru_maintainer, default: 61)
- idle_timeout: timeout for idle connections. (default: 0, no timeout)
- slab_chunk_max: (EXPERIMENTAL) maximum slab size in kilobytes. use extreme care. (default: 512)
- watcher_logbuf_size: size in kilobytes of per-watcher write buffer. (default: 256)
- worker_logbuf_size: size in kilobytes of per-worker-thread buffer
read by background thread, then written to watchers. (default: 64)
- track_sizes: enable dynamic reports for 'stats sizes' command.
- no_hashexpand: disables hash table expansion (dangerous)
- modern: enables options which will be default in future.
currently: nothing
- no_modern: uses defaults of previous major version (1.4.x)
- sock_cookie_id: attributes an ID to a socket for ip filtering/firewalls
- External storage (ext_*) related options (see: https://memcached.org/extstore)
- ext_path: file to write to for external storage.
ie: ext_path=/mnt/d1/extstore:1G
- ext_page_size: size in megabytes of storage pages. (default: 64)
- ext_wbuf_size: size in megabytes of page write buffers. (default: 4)
- ext_threads: number of IO threads to run. (default: 1)
- ext_item_size: store items larger than this (bytes, default 512)
- ext_item_age: store items idle at least this long (seconds, default: no age limit)
- ext_low_ttl: consider TTLs lower than this specially (default: 0)
- ext_drop_unread: don't re-write unread values during compaction (default: disabled)
- ext_recache_rate: recache an item every N accesses (default: 2000)
- ext_compact_under: compact when fewer than this many free pages
(default: 1/4th of the assigned storage)
- ext_drop_under: drop COLD items when fewer than this many free pages
(default: 1/4th of the assigned storage)
- ext_max_frag: max page fragmentation to tolerate (default: 0.80)
- ext_max_sleep: max sleep time of background threads in us (default: 1000000)
- slab_automove_freeratio: ratio of memory to hold free as buffer.
(see doc/storage.txt for more info, default: 0.010)
-N, --napi_ids number of napi ids. see doc/napi_ids.txt for more details
[root@server08 bin]#
-p是设置Memcache监听的端口,最好是1024以上的端口;默认端口是11211
-d是启动一个守护进程;
-m是分配给Memcache使用的内存数量,单位是MB;
-u是运行Memcache的用户;
-l是监听的服务器IP地址,可以有多个地址;
-c是最大运行的并发连接数,默认是1024;
-P是设置保存Memcache的pid文件
[root@server08 bin]# ./memcached -uroot -d
[root@server08 bin]# ps aux |grep memcached
root 3999 0.0 0.0 413968 1092 ? Ssl 15:38 0:00 ./memcached -uroot -d
root 4010 0.0 0.0 112812 972 pts/0 S+ 15:39 0:00 grep --color=auto memcached
[root@server08 bin]# netstat -atunlp |grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 3999/./memcached
tcp6 0 0 :::11211 :::* LISTEN 3999/./memcached