Tag Info

Hot answers tagged

388

It's important to distinguish here between single instances and the Singleton design pattern. Single instances are simply a reality. Most apps are only designed to work with one configuration at a time, one UI at a time, one file system at a time, and so on. If there's a lot of state or data to be maintained, then certainly you would want to have just one ...


21

In the case you give, it sounds like the use of a Singleton is not the problem, but the symptom of a problem - a larger, architectural problem. Why are the screens querying the cache object for data? Caching should be transparent to the client. There should be an appropriate abstraction for providing the data, and the implementation of that abstraction ...


20

Recording views is very simple, simply add a row to a table that represent the "view" action. This is fast because no locking is required in the database, you're just adding a row onto the end of a heap. Aggregating that into the total number of views requires something like doing SELECT COUNT(*) FROM ... which means you have to lock the table while the ...


14

In addition to Martijn's comments I'd add: Warm up your JVM. Bytecode starts starts off being interpreted for Hotspot and then gets compiled on the server after 10K observations. Tiered Compilation can be a good stop gap. Classloading is a sequential process that involves IO to disk. Make sure all the classes for your main transaction flows are loaded ...


12

Its not global state per se that is the problem. Really you only need to be worried about global mutable state. Constant state is not affected by side affects and thus is less of a problem. The major concern with singleton is that it adds coupling and thus makes things like testing hard(er). You can reduce the coupling by getting the singleton from another ...


12

I wrote a whole chapter on just this question. Mostly in the context of games, but most of it should apply outside of games. tl;dr: The Gang of Four Singleton pattern does two things: give you convenience access to an object from anywhere, and ensure that only one instance of it can be created. 99% of the time, all you care about is the first half of that, ...


11

There are a bunch of things to be aware of yes. I'm in Crete at the moment with limited net access so this will be (fairly) short. Also, I'm not a low-latency expert, but several of my colleagues play one in real life :-). You need to appreciate Mechanical Sympathy (a term coined by Martin Thompson). In other words you need to understand what your ...


10

Here is a list of standard options. Optimize the access to the database to only do what you need, efficiently. A good DBA can help here a lot. This is a basic step that most companies do. Cache data away from the database using something like memcached. This is usually done at the application layer, and is highly effective. Virtually every competent ...


10

If you wan to use a LRU eviction cache (Least Recently Used eviction), then probably a good combination of data structures to use is: Circular linked list (as a priority queue) Dictionary This is why: The linked list has a O(1) insertion and removal time List nodes can be reused when the list is full and no extra allocations need to be performed. ...


10

You need caching when you have a problem that can be solved by caching. That problem may be too much processor usage on the DB; if it is then MySQL caching is going to help you a lot -- but maybe not enough, it depends what else is going on. Or it could be that your network connection from the application instances to the DB are getting overloaded. In that ...


9

The intricacy of this issue has been beyond human comprehension these days. (It has been that way since the last 5 years.) Combine that with short-vector parallelism (SIMD) and you have a sense of hopeless that optimizing code by hand is no longer economically feasible - not that it's not possible, but it would not be cost-effective anymore. The current ...


9

I guess the best answer is that it depends. In my experience there are a lot of factors that go into choosing caching algorithms. Factors to consider Read/Write Balance. (What percentage of accesses are reads vs writes) Amount of cache. Type of media behind the cache. (Are they slow SATA drives or fast SSD drives?) Hits vs Misses. (How often are things ...


8

Most likely the value has been cached somewhere along the way so you are seeing stale data. Because it is not critical for this data to be accurate the developers have decided to favour performance over getting up to date data. You really wouldn't want to go to the database and do a row count for every hit on the site just to update this figure so they ...


8

Unless I'm misunderstanding the question, I don't think that this is an appropriate scenario for caching. Cached data normally has at least one of the following attributes (usually all of them): Expensive to retrieve or compute; Highly static - may change occasionally but very rarely; Non-critical - OK if the requester sees stale data. It doesn't sound ...


7

Keep your data small if possible Keep things that will be accessed together (or right after another) next to each other in memory Learn about your compiler's optimization parameters Read What every programmer should know about memory for more details than you could ever want


6

Assuming you know almost nothing about the application you're going to develop, you should know more about it before actually choosing and implementing a cache system. In other words, there are no default implementations: some are good for some purposes, and are totally bad for others. For example, take just two implementations: Least Recently Used and ...


6

"Are there any reasons not to do this?" Scalability. Sure, when you're using it now, the data is under a few MB. Will that always be the case? I don't think so. Especially if you expect other people to use this system. You're re-inventing the wheel. If the content is stored in files, you should just let Apache (or whatever other web server you're using) ...


6

90% of the problem is optimizing your database queries and configuring your server correctly. Make sure you have proper indexes, that the queries arent doing row scans, etc. Use whatever profiler tools your database offers. You'd be suprised how much this can affect a server. A lot of companies will rush out and buy a new database server when all they ...


6

My recommendation is to look at your usage profile and your requirements for the cache. I can see no reason why you would leave stale data in memcached. I think you have picked the right approach ie: update the DB. In any case, you're going to need a wrapper on your DB update (which you've done). Your code to update the User in the DB and in-RAM should ...


6

Setting the max-age to 8 days instead of 1 year gives Google better control over the way it presents itself to the world. You're right that they can simply use a different URL for temporary changes like the Google doodle, but if they think they might want to change the way their real corporate logo appears, it makes sense to use a shorter expiration time for ...


5

How do you reduce the database's workload? Cache data on the web servers so that the business layer doesn't make redundant calls to the database. [ORM frameworks like Hibernate can help with this.] Easy, until you realize that now you've got a ton of traffic internally as a web server tries to run SELECT TOP 3000 (presumably to crunch the results ...


5

Insanity: doing the same thing over and over again and expecting different results. Albert Einstein Calculate it once store the result in the database. Only purge this result if the underlying data changes. When a user requests the statistics of a given user, fist check the cache. If one exists return this immediately, otherwise do the ...


5

The book touches on subjects like temporaries, constructors/destructors, the overhead of virtual functions, benefits and pitfalls of inlining, memory pooling, caching, CPU cache effects, registers etc. Many of those techniques are still valid - even if the language and compilers will be providing a lot more help now than in 1999, and the hardware you are ...


5

Yes, pulling dynamic data via AJAX is a totally viable solution. Not sure if that pattern has a name or not, but I've done it before. Another option that is supported by some edge cache platforms (both hosted and otherwise) is ESI which works like SSI, but allows you set a custom TTL for page fragments. One thing to consider is whether you will have content ...


5

First you last question: Why Redis/memcached? No, they're not (usually) faster than simple in-process dictionaries. The advantage comes when you have several worker processes, or even many app-layer machines. In that case, instead of each process having its own small cache, they all share a single big (distributed) cache. With bigger caches, you get ...


4

In order for large sites to scale, they have to perform caching at several stages. That can be page caching, sub-page caching, and/or record caching. You might have a combination of all of them in effect. For example, if the youtube page is cached until a new comment is added, you'll see some lag until someone posts a comment. There are several ways of ...


4

If you were to choose a caching system: Did you check the following article available at MSDN: http://msdn.microsoft.com/en-us/library/ee817645.aspx ? It is outdated ("retired" as they say) but still a good read, particularly the second chapter for you since you did not yet made up your mind on the technique/technology you want to use. The following ...


4

The Google Maps API licensing page explains this fairly well, to summarise, there are three types of license: Google Maps API The Google Maps API is a free service that lets you embed Google Maps in your freely accessible web pages or mobile apps. Your service must be freely and publicly accessible to end users. Google Maps API for Business Google ...


4

I have experimented with different approaches to object caching, and I see advantages to an approach where collections are cached as references rather than actual objects. An example: User GetUser(int ID); ICollection<User> GetRecentUsers(int amount); ICollection<User> GetActiveUsers(int amount); void Update(User user); In this example, if ...


4

Caching is a very wide topic, but ultimately the goal is the same : to improve performance and reduce the load of IO, CPU or memory intensive operations. If you can elaborate on which type of 'algorithmic caching' you are referring to, we can answer more specifically. Examples of types of caching: In an application: Data Caching - e.g. caching values, ...



Only top voted, non community-wiki answers of a minimum length are eligible