Is the Redis a single thread or a two-threaded one? Applicable scenario and experience summary Road

Source: Internet
Author: User
Tags redis

respect for the success of Labor, reproduced please attach this article link and author: jianfeng_soft@163.com (road)

Recently (Friday) in chat with friends, chat to their game service to use a list of functions, naturally next we talked to Redis, but due to threading problems, the Internet is generally seen is a single thread, whether it is single-threaded or even two-thread, hashes. Hgetall ... Also appear CPU bottleneck, to solve this bottleneck, now can think of is in front of a layer of MC (midnight Commander) to reduce the number of requests

Personal summary, before the trial Redis, the first need to accurately determine whether you are using Redis cache or use her storage.

Cache is practical for the list mentioned above, the statistics of the short data volume of the scene, because the Redis list to save the list data, if not enough memory can t out.

In addition, I would like to briefly summarize the redis scenes and considerations, hoping to give some help to small partners who have not had practical experience, Redis only suitable for a large number of writing complex data structures, or simple structure but to persist, and the key is The data capacity must be less than memory,

Therefore, it is necessary to do hardware memory capacity planning before use, such as future growth budget, currently deposited in a hashes kv need to occupy how much space, in order to do the budget.

Even with persistence, memory fragmentation is serious, meaning that even if the data is moved to disk (RDB or AOF), all data will always remain in memory (so you need to consider the need for persistence). To do persistence is to choose Rdb or AOF. )

RDB: The advantage is that it will only affect the speed of reading and writing in a short time, but may lose data;

AOF: Each write will lead to performance subordinates, and recovery time is longer, than the MC delay is higher, especially the persistence, even if not to do the persistence is more delay than the MC.

Comprehensive evaluation Redis Advantages and disadvantages
Advantages: Memory operation is very fast, advanced data structure is available,
Disadvantage: Slow disk operation, occasional delay, memory consumption


Finally, the following is an excerpt from the network to the Redis thread (single double puzzle) for the post-analysis:

Multi-Process Analysis:[HTML]  View plain copy int rdbsavebackground (char *filename)  {        pid_t childpid;       long long start;          if  (server.rdb_child_pid != -1)  return REDIS_ERR;          server.dirty_before_bgsave = server.dirty;        server.lastbgsave_try = time (NULL);           start = ustime ();       if  (Childpid = fork ())  == 0)  {           int retval;               /* Child */            if  (server.ipfd > 0)  close (SERVER.IPFD);            if  (server.sofd > 0)  close (SERVER.SOFD);            retval = rdbsave (filename);            if  (RETVAL == REDIS_OK)  {                size_t private_dirty =  Zmalloc_get_private_dirty ();                   if  (private_dirty)  {                    redislog (redis_notice,                         "Rdb: %zu  mb of memory used by copy-on-write ",                         private_dirty/(1024*1024));                }            }           exitfromchild (retval ==  REDIS_OK)  ? 0 : 1);       } else {            /* Parent */            server.stat_fork_time = ustime ()-start;            if  (childpid == -1)  {                server.lastbgsave_status = REDIS_ERR;               redislog (REDIS_WARNING, "Can" t  Save in background: fork: %s ",                    strerror (errno));                return REDIS_ERR;           }            redislog (Redis_notice, "background saving  started by pid %d ", childpid);            Server.rdb_save_time_start = time (NULL);            server.rdb_child_pid = childpid;            Updatedictresizepolicy ();           return REDIS_OK;        }       return REDIS_OK; /*  unreached */  }&NBsp; 

Redis on the cache when there are save and bgsave two ways. Bgsave will fork () a background process. The key is this line of code: if ((Childpid = fork ()) = = 0)

And Redis multithreading is how to achieve it.

There are a few lines of key code in the bio.h. [HTML] view plain copy/* Background job Opcodes */#define REDIS_BIO_CLOSE_FILE 0/* Deferred close (2) sys Call. * * #define REDIS_BIO_AOF_FSYNC 1/* D

Related Article

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.