A list of top frequently asked Memcached interview questions and answers are given below.
1) What is Memcached?
Memcached is a general-purpose free and open source, high performance, distributed memory object caching system. It is used to speed up database-driven websites by caching data and objects in RAM.
In simple words, you can say that Memcached is a component which stores data temporally for 1hour/ 6 hour/ 12 hours/ 1 day, etc. and you can integrate this component with your applications to increase their performance.
2) In which language Memcached is written and who is the author?
Memcached is written in "C language". It was developed by Brad Fitzpatrick in 2003. Initially, it is developed for LiveJournal, but now it is used by Wikipedia, Twitter, Facebook, Flickr, Netlog, YouTube, etc.
x
3) How Memcached works?
See the following steps to understand how Memcached works:
Memcached first tries to get the detail of the user, and the browser sends the request to the application.
An application calls the Memcached for a particular user.
If Result found in Memcached, Return the result from Memcached.
If Result Not found in Memcached, Application sends the request to the database and save the result in Memcached.
Each Memcached have one unique key.
Get/Set the data work on behalf of the key.
You can also delete one or more keys.
You can also assign tags to one/more keys.
4) When was the first version of Memcached launched?
The first version of Memcached was launched on May 22, 2003.
5) What is the usage of Memcached? In which types of websites, it is generally used?
Memcached is used to increase the speed of dynamic database driven websites. It caches data and objects in RAM to reduce the execution time.
It is generally used:
In social networking sites for profile caching.
For content aggregation i.e. HTML/Page caching.
In E-commerce websites for Session and HTML caching.
In location-based services for database query scaling.
In gaming and entertainment services for session caching.
It can also be used to track cookie/ profile for ad targeting.
6) What is the best usage of Memcached?
Best usage of Memcached:
It is easy to install in Windows as well as in the UNIX operating system.
It provides API integration for all the major languages like Java, PHP, C/C++, Python, Ruby, Perl, etc.
It enhances the performance of web application by caching.
It reduces the burden of the database server.
It facilitates you to delete one or more values.
It facilitates you to update the values of keys.
7) What are the drawbacks/ limitations of Memcached?
A list of the limitations or drawbacks of Memcached:
Memcached cannot store data persistently and permanently.
Memcached is not a database. It stores only temporary data.
Memcached cannot cache large objects.
Memcached is not application specific.
Memcached is not fault-tolerant or highly available.
8) In which conditions cache cannot retain the stored information?
The cache cannot retain the stored information in following conditions:
When the allocated memory for the cache is exhausted.
When an item from the cache is deleted.
When an individual item in the cache is expired.
9) What is the difference between Memcache and Memcached?
Difference between Memcache and Memcached:
Table:
Memcache
Memcached
Memcache module provides handy procedural and object-oriented interface to Memcached.
Memcached is a high-performance, distributed memory object caching system.
Memcache is an extension that allows you to work through handy object-oriented (OOP's) and procedural interfaces.
Memcached is an extension that uses the libMemcached library to provide API for communicating with Memcached servers.
The Memcache module provides a session handler (Memcache).
The Memcached provides a session handler (Memcached).
It is designed to reduce database load in dynamic web applications.
It is used to increase the dynamic web applications by reducing database load. It is the latest API.
10) Can we share a single instance of Memcache between multiple projects?
Yes, we can share a single instance of Memcache between multiple projects because being a memory storage space, Memcache can be run on one or more servers. In Memcache, you can also configure your client to speak to a particular set of instances.
We can also run two different Memcache processes on the same host being completely independent and without any interference. If you partition your data, it is important to know from which instance to get the data from or to put into.
11) Explain the differences between SAP HANA and Memcached?
SAP HANA is an in-memory RDBMS and mostly used for accelerating SAP applications while Memcached is a key/value caching system and used for accessing both RDBMS and NoSQL databases.
12) How to connect Memcached server with telnet command?
By using telnet hostname portNumber command, you can connect Memcached server with telnet command.
Syntax
$telnet HOST PORT
Example
The given example shows that how to connect to a Memcached server and execute a simple set and get command. Let's assume that the server of Memcached is running on host 127.0.0.1 and port 11211.
$telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
// store data and get that data from server
set javatpoint 0 900 9
memcached
STORED
get javatpoint
VALUE javatpoint 0 9
memcached
END
13) How to get the value of key?
By using the get command, you can get the value of the key.
Syntax
get key
Example
In the given example, we use javatpoint as the key and store Memcached in it with an expiration time of 900 seconds.
import net.spy.memcached.MemcachedClient;
public class MemcachedJava {
public static void main(String[] args) {
// Connecting to Memcached server on localhost
MemcachedClient mcc = new MemcachedClient(new
InetSocketAddress("127.0.0.1", 11211));
System.out.println("Connection to server sucessfully");
System.out.println("Get from Cache:"+mcc.get("javatpoint"));
}
}
14) How to set the value of key?
By using set command, you can set the value of the key.
Syntax
set key flags exptime bytes [noreply]
value
Example
In the given example, we use javatpoint as the key and set value Memcached in it with an expiration time of 900 seconds.
set javatpoint 0 900 9
memcached
STORED
get javatpoint
VALUE javatpoint 0 9
Memcached
END
15) How to add value in the key?
By using add command, you can add value in the key.
Syntax
add key flags exptime bytes [noreply]
value
Example
In the given example, we use "key" as the key and add the value Memcached in it with 900 seconds expiration time.
add key 0 900 9
memcached
STORED
get key
VALUE key 0 9
Memcached
END
16) How to replace the value of the key?
By using replace command, you can replace the value of the key.
Syntax
replace key flags exptime bytes [noreply]
value
Example
In the given example, we use "key" as the key and add the value Memcached in it with 900 seconds expiration time. After this, the same key is replaced with the "redis".
add key 0 900 9
memcached
STORED
get key
VALUE key 0 9
memcached
END
replace key 0 900 5
redis
get key
VALUE key 0 5
redis
END
17) How to append the value of the key?
By using append command, you can append the value of the key.
Syntax
append key flags exptime bytes [noreply]
value
Example
In the given example, we are trying to add some data in a key that does not exist. Hence, NOT_STORED is returned by Memcached. After this, we set one key and append data into it.
append javatpoint 0 900 5
redis
NOT_STORED
set javatpoint 0 900 9
memcached
STORED
get javatpoint
VALUE javatpoint 0 14
memcached
END
append javatpoint 0 900 5
redis
STORED
get javatpoint
VALUE javatpoint 0 14
memcachedredis
END
18) How to prepend value of key?
By using prepend command, you can prepend value of the key.
Syntax
prepend key flags exptime bytes [noreply]
value
Example
In the given example, we are trying to add some data in a key that does not exist. Hence, NOT_STORED is returned by Memcached. After this, we set one key and prepend data into it.
prepend tutorials 0 900 5
redis
NOT_STORED
set tutorials 0 900 9
memcached
STORED
get tutorials
VALUE tutorials 0 14
memcached
END
prepend tutorials 0 900 5
redis
STORED
get tutorials
VALUE tutorials 0 14
redismemcached
END
19) How to delete the key?
By using delete command, you can delete the key.
Syntax
delete key [noreply]
Example
In the given example, we use javatpoint as a key and add the value Memcached in it with 900 seconds expiration time. After this, it deletes the stored key.
set javatpoint 0 900 9
memcached
STORED
get javatpoint
VALUE javatpoint 0 9
memcached
END
delete javatpoint
DELETED
get javatpoint
END
delete javatpoint
NOT_FOUND
20) How to show the stats?
By using stats command, you can show the stats.
Syntax
stats
Example
stats
STAT pid 1162
STAT uptime 5022
STAT time 1415208270
STAT version 1.4.14
STAT libevent 2.0.19-stable
STAT pointer_size 64
STAT rusage_user 0.096006
STAT rusage_system 0.152009
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 6
STAT cmd_set 4
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 4
STAT get_misses 2
STAT delete_misses 1
STAT delete_hits 1
STAT incr_misses 2
STAT incr_hits 1
STAT decr_misses 0
STAT decr_hits 1
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 262
STAT bytes_written 313
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 1
STAT evicted_unfetched 0
STAT bytes 142
STAT curr_items 2
STAT total_items 6
STAT evictions 0
STAT reclaimed 1
END
21) How to get the versions?
By using Version command, you can get the versions.
22) How to close the connection?
By using the Quit command, you can close the connection.
23) How would you update Memcached when data changes?
There are two methods to update Memchached when data changes:
By clearing the cache proactively: You can update Memcached by clearing the cache while insertion or updation is made.
By resetting the cache: It is slightly similar to the first method, but it doesn't delete the keys and wait for the next request for the data to refresh the cache, it resets the values after the insert or update.
24) What is Dogpile effect? How can you prevent this effect?
If the cache expires, and websites are hit by multiple requests made by the client at the same time, this effect is known as Dogpile effect.
This effect can be prevented by using a semaphore lock. In this system when value expires, the first process acquires the lock and starts generating new value.
25) What happens to the data stored in Memcached when server accidentally gets shut down?
In Memcached, data is not permanently stored. It is not a durable data so, if the server is shut down or restarted, all the data stored in Memcached will be deleted.
26) If you have multiple Memcache servers and one of the Memcache servers fails which has your data, will it ever try to get key data from that one failed server?
The data in the failed server won't get removed, but there is a provision for auto-failure, which can be configured for multiple nodes. Fail-over can be triggered during any socket or Memcached server level errors and not during normal client errors like adding an existing key, etc.
27) How can you minimize the Memcached server outages?
Following are the methods to minimize the Memcached server outage:
When one instance fails, several of them go down, this situation will put larger load on the database server when the client reloaded the lost data. To avoid this, you should write your code to minimize cache stampedes, and it will leave a comparatively less impact.
You can bring up an instance of Memcached on a new machine using the lost machines IP address.
The Code is another option to minimize server outages as it gives you the liberty to change the Memcached server list with minimal work.
Setting timeout value is another option that some Memcached clients implement for Memcached server outage. When your Memcached server goes down, the client will keep trying to send a request till the time-out limit is reached.
Write a public review