RDS can be used together with ApsaraDB for Memcache and ApsaraDB for Redis to form storage solutions with high throughput and low delay. This document describes the cached data persistence solution based on the combined use of RDS and ApsaraDB for Memcache.

Background information

Compared with RDS, ApsaraDB for Memcache and the ApsaraDB for Redis have the following two features:

  • High response speed: The request delay of the ApsaraDB for Memcache and the ApsaraDB for Redis is usually within several milliseconds.
  • The cache area can support a higher Requests Per Second (QPS) than the RDS.

System requirements

  • bmemcached (with support of SASL extension) has been installed in the local environment or ECS.

    bmemcached download address: Click Here to download.

    The bmemcached installation command is as follows:

    pip install python-binary-memcached
  • Python is used as an example. Python and pip must be installed in the local environment or ECS.

Sample code

The following sample code realizes the combined use of RDS and ApsaraDB for Memcache:

/usr/bin/env python
import bmemcached
Memcache_client = bmemcached.Client((‘ip:port’), ‘user’, ‘passwd’)
#Search for a value in ApsaraDB for Memcache
res = os.client.get(‘test’)
if res is not None:
    return res #Return the searched value
else:
    #Query RDS if the value is not found
    res = mysql_client.fetchone(sql)
     Memcache_client.put(‘test’, res) #Write cached data to ApsaraDB for Memcache
    return res