You are on page 1of 13

Nhng k thut v cng ngh c s dng trong sn phm Ming

Cng ngh Memcached( c s dng rng ri trong cc d n cng ty) Redis(Phn I) K thut Open Authentication(Phn II) SSO(Single Sign On) Big4(Facebook, Yahoo, Twitter, Google)

I. Redis
Redis l mt cch lu tr d liu kiu key/value, n thc thi trn server ANSI C, redis cung cp nhiu cch lm vic khc nhau thc hin mt vic n gin : lu tr value(aaa) ti key(redis), trong vi cc keys ch h tr kiu string th vi values s h tr nhiu kiu nh dng khc nhau nh : Strings, Lists, Sets, Sortedsets(zsets), Hashes. V mi mt loi kiu nh dng khc nhau s c mt tp hp cc command lm vic vi n nh: Thm mi, loi b mt phn t trong values... khc nhau. C th xem Redis nh l mt cu trc d liu (data structures) cp cao trn my server, khi mt ngi dng redis ch cn cung cp mt interface Abstract Data Types(kiu d liu tru tng), t ngi dng khng phi chy cc data structures hay cc thut ton trn data structures m c th thc thi cc command ca data type . Redis is free software released under the very liberal BSD license. Written in C(C99 standard). Redis c nhng c im ging nh Memcached nh: Lu Key value (Key value store). Tt c data c lu trn Memory(RAM) Key c th ht hn(expire) hoc khng Nhanh(Fast), nh nhng(light-weight)

Nhng Redis c nhiu c im, chc nng khc mng li li ch khi s dng v trin khai. Bao gm: Persistence Multiple databases Queryable keyspace Support for interger counters Higher level data structures Atomic operations Ability to paginate lists without mutating them Master-slave replication Optional VM feature

I.1 So snh Redis, Memcached, Tokyo Tyrant and MySQL


Redis Memcached Tokyo Tyrant / Tokyo Cabinet MySQL 5.1.40 (MyISAM) MySQL 5.1.40 (with Innodb Plugin 1.0.4), compiled into source of MySQL 2 client boxes All clients connecting to the server using Python Used Python's threads to create concurrency Each thread made 10,000 open-close connections to the server The server was
o o o o o Intel(R) Pentium(R) D CPU 3.00GHz Fedora 10 32bit Intel(R) Pentium(R) D CPU 3.00GHz 2.6.27.38-170.2.113.fc10.i686 #1 SMP 1GB RAM

Used a md5 as key and a value that was saved


Team Ming

Created an index on the key column of the table Each server had SET and GET requests as a different test at same concurrency

Team Ming

Team Ming

I.2. c im ca Redis
I.2.1. Persistence, higher level data structures
Redis ly v np d liu trn Memory(RAM), nhng ti mt thi im th d liu c th c lu tr trn disk(Data in memory, but saved on disk). C 2 kiu persistence c supported: Semi persistent mode (Snapshotting): Thi gian lu tr data trn Memory v Disk l khng ng b

Team Ming

Sau mi N s thay i hoc N thi gian no th data mi c lu tr trn disk. Do vic lu tr data l khng ng b nn data c th b mt khi server gp s c(restart or start) Fully persistent mode (Append Only File) Mi s thay i c vit thm vo mt tp tin. M hnh ny c gi l Append only file, mi command nhn s thay i th c vit thm vo file c nh dng l ASAP. Nhng command ny s c chy li khi server restart hoc start np li d liu ln memory.

I.2.2. Multiple databases v Data structures


im khc bit d nhn thy ca Redis l: Key l mt string nhng value th khng gii hn mt string m c th l List, Sets, Sorted sets, .... Cu trc d liu ca Redis gm : Strings String-as-integers List of Strings Set of Strings(unique list) Sorted Set of Strings(ZSET) Redis h tr Multiple database vi nhiu commands t ng remove key t mt database ti database khc. Mc nh th DB 0 s c la chn cho mi ln kt ni(connection), nhng khi s dng lnh SELECT(SELECT command) th n c th select/create mt database khc. Thao tc MOVE(MOVE operation) c th chuyn mt item t mt DB ti DB khc mt cch t ng. l s khc bit ca Redis vi Mysql. Vi Mysql, truy sut data t nhiu database khc nhau th phi to ra by nhiu connection ti nhng database .

I.2.3. Atomic Operations


Redis rt nhanh trong cc thao tc ly v np d liu do redis h tr nhiu lnh mang tnh cht chuyn bit nh:
Team Ming

LPUSH/PPUSH: Thm vo u/cui ca danh sch. LPOP/RPOP: Return v remove phn t u/cui ca danh sch. RPOPLPUSH: Return v remove phn t cui ca source list v y vo u ca destination list GETSET: Np mt key vi mt gi tr mi v tr v gi tr c. MGET/MSET: Ly/np nhiu key vi nhiu data. SMOVE: Chuyn mt value t one set to another. .. Rt nhiu tnh nng u vit v chuyn bit na c redis h tr.

I.2.4. Replication
Redis h tr m rng master-slave nu chng ta mun s an ton hoc m rng, co gin trong vic lu tr data. Slaves can be used for scalability or redundancy Mt master c th c nhiu slave Slaves c th nhn nhiu kt ni ca salve khc Slave c th kt ni li khi b outage

I.3. Thay th Memcached hoc kt hp Memcached vi Redis


Redis c th thay th c Memcache v: Nhng g memcache lm c th redis cng c th, nhng redis c nhiu tnh nng m memcache ko th c c. Many databases from a single instance of Redis C th backup d liu trn redis khi server ang hot ng: Khi Redis lu DB n to ra mt tp tin tm thi, sau i tn (2) tn tp tin tm thi vi tn file ch. C th s dng master-slave nhn rng c s d liu d phng Watch live commands on a running instance with the MONITOR command C th s dng redis nh l mt database, queue, server cache hoc tt c cc kt hp.

Team Ming

Redis v Memcache u c u nhc im ring, nhng nu kt hp c Redis v Memcache song hnh trong cng vic th l mt iu l tng.

I.4. Redis nhanh nh tha no?(How fast is Redis)


Redis c tin tch redis-benchmark c thc hin vi b m phng gm N clients trong cng mt thi im gi M yu cu. Di y l kt qu ca redis-benchmark: 50 Client thc hin ng thi vi 100.000 request. Thc hin SET v GET mt String c kch thc 256 bytes. About 110.000 SETs per second About 81.000 GETs per second.

====== SET ======


100.007 requests completed in 0.88 seconds 50 parallel clients 3 bytes payload keep alive: 1 58.50% <= 0 milliseconds 99.17% <= 1 milliseconds 99.58% <= 2 milliseconds 99.85% <= 3 milliseconds 99.90% <= 6 milliseconds
100.00% <= 9 milliseconds 114.293.71 requests per second

====== GET ======


100.000 requests completed in 1.23 seconds 50 parallel clients 3 bytes payload keep alive: 1
Team Ming

43.12% <= 0 milliseconds 96.82% <= 1 milliseconds 98.62% <= 2 milliseconds 100.00% <= 3 milliseconds 81.234.77 requests per second

II. Open Authentication(OAuth) II.1 Gii thiu


OAuth l mt protocol cho php user cp php cho mt web application truy cp thng tin ca mnh trn mt site khc m khng cn cung cp thng tin login cho web application . Cn OpenID cho php user s dng mt account c mt web site h tr nh Google, Yahoo!, MySpace, ... login vo mt web site khc.

II.2 Ti sao dng OAuth


II.2.1 Nhu cu ngi dng
Ngi dng khng mun nh nhiu username/password

II.2.2 Nhu cu chia s ti nguyn m rng


Ngi dng c nhiu kiu ti nguyn nm trn trang x hi: Thng tin c bn, thng tin mt, cc mi quan h, cc hnh ng(activities) ca bn thn v bn b. Mt trang x hi khng t mnh lm ht cc dch v Ngc li dch v ng mt mnh s khng tn dng thng tin sn c Sinh ra cc ng dng(apps) kt ni trang x hi. Mi app ch nn c quyn truy cp gii hn loi ti nguyn V d App in nh ch c truy cp album nh ca ngi dng, khng ly thng tin v activities bn thn hay thng tin bn b. Cha kha xe Mercedes, c cha ph cho li xe ch c th khi ng xe, khng m c cp hay ngn an ton.

Team Ming

10

II.3 c t OAuth 1.0(http://oauth.net/core/1.0/)


II.3.1 Cc khi nim
service provider - SP: Ni cung cp dch v OAuth consumer/app: Bhch hng s dng dch v OAuth user: ngi dng request_token: B kha(key & secret) kh to phin lm vic gia consumer v SP access_token: B kha(key & secret) consumer dng ly thng tin(ti nguyn) ca ngi dng SP oauth_key: Kha nh danh oauth_secret: M bo mt consumer_key: Kha nh danh app consumer_secret: M bo mt app callback: ng dn tr v site consumer

II.3.2 Lung s lc
SP v Consumer thng nht b consumer_key v consumer_secret 1. Consumer Service Provider Consumer s dng nh danh consumer gi yu cu ly request_token ti SP, bo mt phin lm vic bt u. SP kim tra nh danh consumer, tr li request_token ngu nhin (oauth_key, oauth_secret). 2. Consumer user Consumer chuyn ngi dng ti trang ng nhp ca SP 3. User - SP User ng nhp bng ti khon ca mnh SP vi thng bo cho php dch v consumer ly thng tin. Ngi dng tip theo s c chuyn v trang i sn ca consumer (callback) vi m oauth_verifier(SP sinh ngu nhin) chng nhn rng ngi dng (ng nhp v) ng chia s thng tin.
Team Ming

11

4. Consumer-SP Tip theo bc 3 th SP gi v cho consumer mt m gi l oauth_verifier thng bo cho Consumer bit l user ng nhp xong trn SP. Consumer gi yu cu ly access_token, s dng cc nh danh consumer, request_token v oauth_verifier chng thc. Lc ny, consumer s phi gi li oauth_verifier thng bo cho SP l consumer nhn c thng bo user ng nhp trn SP. SP kim tra, tr v cho consumer access_token c sn trong h thng hoc to mi. 5. Consumer-SP Vi cc api SP cung cp, consumer s dng b thng tin nh danh consumer (key v secret) v access_token gi yu cu ti SP ly thng tin ngi dung cn thit. Di y l hnh v m t lung OAuth trn:

Team Ming

12

II.4 OAuth server Ming


hon tt y cc bc theo c t 1.0 D dng cho bn th ba pht trin consumer S dng cng c chng thc an ton l ch k Secret c gi kn An ninh bo m trn ng non-https Nonce duy nht ng vi mi request km theo timestamp, khng b gi mo request d xy ra trng hp b xm nhp tri php trc . Thng nht quy chun ch k, do c t oauth mi pht trin, mi th vin s dng c li nh khc nhau: th vin php m ha ng dn 2
Team Ming

13

ln, th vin c# thiu nhng param nh ngha thm Vn gy nhiu li nht trong qu trnh thc hin OAuth.
One of the most frustrating things about working with OAuth is getting back an error that the signature is invalid (http://support.mashery.com/docs/tips_and_tricks/OAuth) The signature base string is often the most difficult part of OAuth for newcomers to construct. (http://dev.twitter.com/pages/auth)

Xc thc domain M xc thc ng nhp SP ch tr v trang callback nm trn domain consumer ng k.

Team Ming

You might also like