Practice of memcached cache system (8) solutions to memcached asynchronous real-time read/write problems sac

Source: Internet
Author: User

Respect knowledge, reprinted please indicate this article from: programming artist poechant csdn blog http://blog.csdn.net/poechant

"Back-end server development series-" Practical memcached memory cache system "series blog: http://blog.csdn.net/poechant/article/category/1060687

Memcached is rarely used in real-time read/write scenarios. However, after memcached is written, read operations are performed only after a certain period of time. But what if there is an instant read operation after writing in the Application Scenario? There seems to be nothing special. We can still write it like this:

Note: The spymemcached client is used here.

MemcachedClient cache = new MemcachedClient(cacheServerAddr);cache.set("key", 3600, bigData);return cache.get("key");

After the data is written to the cache, if other clients perform the read operation immediately, the read will fail because the set operation is asynchronous (async) and may not be completed yet.

A feasible method is synchronous write operations. The common set method does not use this method. Therefore, you must use write operations that comply with the memcached CAS (check and set) protocol. This write operation is generally based on the cas id obtained after reading (similar to the version ID in SVN). According to this cas ID, there is no "Duplicate write" conflict with other write operations. Therefore, the CAS protocol can be used as follows:

(1) initial write: write a simple initial value (set, asynchronous operation );

(2) Get version: Obtain the cas id asynchronously;

(3) Synchronous write: Write Data in synchronous mode to ensure that the write has ended before reading.

MemcachedClient cache = new MemcachedClient(cacheServerAddr);cache.set(“key”, 3600, "");long casId = cache.asyncGets("key").get().getCas();cache.cas("key", casid, bigData);return cache.get("key");

The "Set-asyncgets-cas" method is used to solve the problem of asynchronous real-time read/write in cache. We call it "sac" (you must have thought of zookeeper ).

Respect knowledge, reprinted please indicate this article from: programming artist poechant csdn blog http://blog.csdn.net/poechant

-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.