<redis actual combat >5.4.2 use one Redis server per application component

Source: Internet
Author: User
Tags redis server

Original: https://redislabs.com/ebook/redis-in-action/part-2-core-concepts-2/ chapter-5-using-redis-for-application-support/5-4-service-discovery-and-configuration/5-4-2- One-redis-server-per-application-component

As more and more redis is being used, countless developers have found that our first Redis server has grown at some point. We may need to log more logs, may require more cache space, or we have already used one of the advanced services at the end of the next section in advance. Whatever the reason, we need more servers.

To make the transition to more servers easier, I recommend running a Redis server for each individual part of the app-one for the log, one for the statistics, one for the cache, one for the cookie, and so on. Note that you can run multiple Redis servers on a single machine and only need to run on different ports. In addition, if you want to reduce the system load, you can also use a different Redis database, in any case, by recording different data to different key space, you transition to more or larger servers also become easier. Unfortunately, when you have more servers or databases, managing and distributing all of your configuration information becomes more painful.

In the previous section, we used Redis as a configuration source for whether we should provide a maintenance page, and we can use Redis again to tell us about other Redis server information. Further, use a known Redis server as the configuration information directory to discover how to connect to all other Redis servers that provide data to different applications or service components. And we're going to use some way to connect to the correct server when the configuration changes. Our implementation will be more generic than this example invocation, but we must make sure that you use this method to get the configuration information and you can start using it to a non-Redis server or service.

We will build a function to get the configuration information in JSON format from a key named for the type and application component of this service. For example, if we want to obtain the connection information for the Redis server that holds the statistics, we will get the key named Config:redis:statistics. The code is as follows

#https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#l415
def Set_config (conn, type, component, config): conn.set ( 'config:%s:%s '%(type, component), json.dumps (config))

With this set_config () function, we can set any of our configurations to JSON format. With a slight change in semantics, a get_config () function similar to the previous is_under_maintenance () structure replaces the is_under_maintenance () function, referring to the following code that matches Set_config (). will allow us to cache configuration information at intervals of 1 seconds, 10 seconds or 0 seconds, depending on our needs.

#https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#l413
Configs ={}checked= {}defGET_CONFIG (conn, type, component, wait=1): Key='config:%s:%s'%(type, component)#Check if the configuration information for this component needs to be updated. ifChecked.get (Key) < Time.time ()-Wait:#it is necessary to update the configuration to record the last time the connection was checked. Checked[key] =time.time ()#Gets the component configuration for Redis storage. Config = json.loads (conn.get (key)or '{}') #converts a potential Unicode keyword argument to a string keyword argument. Config = dict ((str (k), Config[k]) forKinchconfig)#gets the configuration that the component is using. Old_config =configs.get (Key)#If two configurations are not the same ... ifConfig! =Old_config:#... Then the configuration of the component is updated. Configs[key] =ConfigreturnConfigs.get (Key)

Now that we have a pair of functions to get and set configuration information, we can do more. We start along this storage path and get the configuration to create a variety of different Redis server connections. But so far, the first parameter of most of our functions is a redis connection. Let's create a way to automatically connect those services without having to manually get the server connections.

<redis Combat >5.4.2 Use one Redis server per app component

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.