Implementing Cached Data for C++ CGI
We have an Apache CGI application written in C++ (using cgicc extensions) that currently accesses some in-memory MySQL (using HEAP engine) tables. We are to the point now where the sheer number of transactions we are processing per day have exceeded the server's ability to service the queries required to service these requests.
Our next step we had discussed for improving performance was to make these in-memory tables more "local" to the CGI process and use some form of shared memory for caching the data. Memcached or Membase seem like possible options though what we would use it for doesn't seem to agree exactly with that approach (or maybe it does and I simply don't see the implementation/connection).
In an ideal world we could use some form of serialization to load the data into a shared memory object. At startup the CGI would obtain a copy or would be provided an address from which it could make a copy for itself of the shared data structure, deserialize it into a "local" in-memory data structure and then do whatever comparisons it needed to do from that object. We are really hoping this is not going to take a complete redesign of our Apache/CGI architecture but instead might simply be a matter of swapping out the querying of the MySQL in-memory tables (MySQL DB is co-located on same server w/ Apache) to using the new in-memory data structures that are "more local" to the process.
Thanks for any insight someone might have on this and what they have seen done. Many prior threads suggest using Memcached/Membase approach and perhaps that is the best route, but just wanted to see if that is the co开发者_开发知识库nsensus.
I would implement some test code for benchmarking purposes using boost::multi_index
. It is a pretty good replacement for most in-memory database operations.
I don't know if you can use it in a movable shared memory allocation, but if you are using a 64-bit OS it should be possible to choose a fixed point in memory to map your shared space without conflicting with another memory map. In that case you can do some magic with C++ allocators to make all your object allocations come from the shared space and the pointers will have the same values in each process.
Using a shared map you can protect it by placing a pthread_rwlock
inside the shared memory space. I like to place a struct containing information about the shared memory at the beginning of the shared block. The struct will have an access lock, free space pointers for the allocator and probably a version identifier (so you don't try to access it using new or old code at the same time).
精彩评论