This is a re-telling of Don’t Get Married with woman story teller to illustrate how Chris Pitt is distorting and changing the facts to fit his narrative. Simply to build his brand at the expense of mine. The only words changed are in BOLD. The story holds and does not loose it’s meaning if told identically by a woman’s voice, which completely and unequivocally shows his argument holds no water. IF this story was about the disposablity of women, it would not work with a female story teller. It CLEARLY does still work with a female story teller. I don’t advocate infidelity, disposability of relationships, or co-dependecy. They are simply a story telling device.
.. To your caching solution. Nice click-baity title I am going to get right to the point. Using your caching solution directly is never a good idea. Why is that?
This goes to one of the basic premises of good Object Oriented development, SOLID. If you have never heard of SOLID, be sure to read up on it. I am just going to talk about the D in SOLID today. Now keep this in mind while I tell a story.
My name is Sally, back in 2007, I was learning the ropes of web development. Up to this point, I had just worked with the C family of languages and Java. As you can imagine, I was pretty green. Fairly quickly, one my websites garnered runaway success. The script it ran on was just not designed to handle the level of activity it was seeing. I was running into HUGE issues with the hard drives. They just were not being able to keep up with the reads necessary to handle the million+ page views we were doing a day. The database was having a hard time keeping up as well. This is when i discovered a Pecl called APC. It was love at first sight! APC is an op code cache that provides a key-value store as well. It was awesome(for the time). APC and I had whirlwind romance. I would sweet talk APC and it would do absolute magic for me. I was using APC everywhere. I wrote a session handler, caching database queries, caching template files, mutexes. You name it, I was doing it with APC. It was phenomenal. I was able to drop server load by over a factor of 20. APC and I were hitched. I had apc_fetch() and apc_store() calls everywhere.
APC was an amazing solution, but as it happens with whirlwind romances, sometimes as time passes, the passion fades. You get to know each other better, you start to see each other’s shortcomings. Sadly, that is what happened to us. APC is stellar for websites limited to a single machine, but as soon as you try to go to clustered/multiple front end environment, he goes from a nimble sweetheart to an cumbersome, emotional mess. All of sudden I am trying to synchronize multiple caches across multiple machines, trying propagate sessions from machine to machine. This is just a terrible way to do things.
It becomes quite apparent what needs to be done. My whirlwind romance to APC has to come to end and I need to move on. As it turns out, I have become so dependent on having a caching solution that I just can’t bear to go on without one. I realize what I must do, and I feel guilty about it, but I have to do it. I have to find a “replacement” for APC before I dump his cumbersome butt.
I go out, I talk to people, read blogs posts. It starts as a crush from a far. Then some documentation “stalking”. Then we finally meet. After hanging out a couple times. I realize Redis is the one for me. He is smart, quick on his feet, and incredibly powerful. Just what I always wanted. It’s time to make my move. It’s time to dump APC.. and of course breaking up is hard to do.
I sit APC down and I tell him it is over and it’s time to pack up his crap and get out. I have found someone new. He doesn’t lift a finger, he just laughs at me. What’s so funny? He points points to the ring on his finger. We are “married” I have hundreds if not 1000s of references to APC in my code. I am going to have to change all of them to use any other caching solution. I need a divorce, and it is going to be painful..
What was the point of all of that? If I had known about SOLID back then, I would have never gotten married to APC.
The D in SOLID is for Dependency Inversion. What is Dependency Inversion? In the most simple terms, it means “Always depend on abstractions, never on concretions”. Does that sounds like heady gibberish still? Put even more simply, you should be using a well thought out interface to access your cache or any similar resource, and not be accessing it directly. If done correctly, changing the underlying engine from APC to Redis could be as easily done as changing a configuration file. In the worst case I would have to write an adapter class for Redis, which would be easy. Much easier than refactoring large amounts of code. If I would had been following this principle, I would have been able to throw APC out the back door as I was moving Redis in from the front door. It’s definitely costly mistake to make, divorce is ugly.