Memcached distributed cache results in high CPU usage 1

Source: Internet
Author: User
Tags high cpu usage

 

The company's website uses memcached for Distributed caching. Recently, some people have reported that the memcached client occupies too much CPU and suspected that the performance of a third-party client is not good, which in turn leads to a text protocol problem, the Department is required to develop the memcached client to support the binary protocol. Because the workload for re-development of the client is relatively large, and in daily development, I have never heard of the bottleneck encountered by the memcached client. Therefore, the problem is rectified. The result shows that the client deserialization and the class design are unreasonable. Share the troubleshooting process and hope to help others.

The first thought is: memcached server memory is full. In the memory cleaning process, client socket connection fails and exceptions occur continuously. Check the memory usage and connections of memcached on the server, and find that the utilization is low. Remove server issues for the moment.

The second possible cause is that when a third party uses the socket connection pool, the resource is not closed or the deadlock occurs. The third-party client code is roughly read and relevant documents are searched. No Exception Code is found. Third-party client issues are temporarily excluded.

The last thought was whether the developer encountered a problem in code writing. Troubleshooting is conducted for the two products that reflect the problem. The following code is found.

 

Code snippet 1:

Code

<! --

Code highlighting produced by actipro codehighlighter (freeware)

Http://www.CodeHighlighter.com/

--> Static serializer SER = new serializer (typeof (list <usermodule>); // using jsonexserializer;
Public static list <usermodule> getallusermodule (INT userid)
{
String cache = cachemanager. Current. Get <string> (getcachekey (userid ));
If (! String. isnullorempty (cache ))
{
Return Ser. deserialize (cache) as list <usermodule>;
}
Else
{
Return NULL;
}
}

Public static list <usermodule> setallusermodule (INT userid, list <usermodule> modules)
{
If (modules! = NULL)
{
String cache = Ser. serialize (modules );
Cachemanager. Current. Add (getcachekey (userid), cache );
}
Else
{
Cachemanager. Current. Remove (getcachekey (userid ));
}
Return modules;
}
Copy code

 

Code snippet 2:

Code

<! --

Code highlighting produced by actipro codehighlighter (freeware)

Http://www.CodeHighlighter.com/

--> // <Summary>
/// Chat room
/// </Summary>
[Serializable]
Public class room
{
// The room has viewer data
List <viewer >_viewers = NULL;
List <string> _ blackips = NULL;
List <viewer> _ blackviewers = NULL;
List <notice> _ notice = NULL;
List <speaker> _ speakers = NULL;
List <content> _ content = NULL;

/// <Summary>
/// Add new chatbot
/// </Summary>
/// <Returns> return to the newly added chatbot </returns>
Public viewer addviewer ()
{
Viewer Vi = new viewer ();
// Maxviewerid + = 1;

// Int id = maxviewerid;
Int id = getviewerid ();
Vi. Name = getviewername ("Tourist" + id );
// VI. IP = system. Web. httpcontext. Current. Request. userhostaddress;
Vi. IP = "127.0.0.1 ";
Vi. viewid = ID;
Viewers. Add (VI );
Return VI;
}

/// <Summary>
/// Add chat content
/// </Summary>
/// <Param name = "content"> chat content </param>
/// <Param name = "viewid"> speaker's ID </param>
/// <Returns> return the newly added object </returns>
Public content addcontent (string content, int viewid)
{
Maxcontentid + = 1;
Content con = new content (datetime. Now, content, viewid, maxcontentid );
Contents. Add (CON );
Return con;
}
......
}

The call code is:
Room room = livesys. Get (key );
Lock (Room)
{
If (room. maxcontentid = 0)
{
// Chatcontentop CPO = new chatcontentop ();
// Room. maxcontentid = CPO. getmaxcontentid ();

Room. maxcontentid = 300;
}
Int viewerid = 123124123;
Room. addcontent (chatcontent, viewerid );
// Determine whether there are more than 100 entries. If there are more than 100 data records, delete the last 100 data records.
System. io. file. appendalltext (@ "D: \ haha.txt", "maximum value:" + room. limitcontentcount + "############## Number of chat records:" + room. contents. count + "\ r \ n ");
If (room. Contents. Count> room. limitcontentcount)
{
Room. Contents. removerange (0, room. Contents. Count-room. limitcontentcount );
}
}
Livesys. Set (Key, room );
Copy code

 

 

Code 1 has the following problems:

The parameter type stored in the cache is object. There is no need to serialize it once before it is stored. Serialization consumes a lot of CPU.

 

Code 2:

Code 2 implements an online chat room, which contains visitors, speeches, and other content. When speaking, the chat room content is judged, only the last 30 items are displayed. New visitors are directly added to the visitor table. On the surface, there is no problem. However, there are two problems:

1. the chatroom design is complicated. Each time data is obtained from the memcached server, the type conversion is required.

2. There is no guest cleanup mechanism. As visitors enter, the volume of objects increases.

 

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.